File size: 2,539 Bytes
496fcc7
 
 
 
 
 
 
 
966d1ab
e9174ad
496fcc7
966d1ab
496fcc7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Run the script and open the link in the browser.

import gradio as gr
import os
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline

# scratch with latbert tokenizer
CHECKPOINT_PATH= 'scratch_2-nodes_tokenizer_latbert-original_packing_fcocchi/model.safetensors'
CHECKPOINT_PATH= 'itserr/latin_llm_alpha'

print(f"Loading model from: {CHECKPOINT_PATH}")
tokenizer = AutoTokenizer.from_pretrained(CHECKPOINT_PATH)
model = AutoModelForCausalLM.from_pretrained(CHECKPOINT_PATH)

description="""
This is a Latin Language Model (LLM) based on GPT-2 and it was trained on a large corpus of Latin texts and can generate text in Latin. 
Please enter a prompt in Latin to generate text.
"""
title= "(L<sup>3</sup>) - Latin Large Language Model"
article= "hello world ..."
examples= ['Accidere ex una scintilla', 'Audacter calumniare,', 'Consolatium misero comites']
logo_image= '/homes/fcocchi/itserr/itserr_latin_llm/ITSERR_row_logo.png'

def generate_text(prompt):
    if torch.cuda.is_available(): device = torch.device("cuda")      
    else: 
        device = torch.device("cpu")
        print("No GPU available")
    
    print("***** Generate *****")
    text_generator = pipeline("text-generation", model=model, tokenizer=tokenizer, device=device)
    #generated_text = text_generator(prompt, max_length=100)
    generated_text = text_generator(prompt, max_length=50, do_sample=True, temperature=1.0, repetition_penalty=2.0, truncation=True)
    return generated_text[0]['generated_text']

custom_css = """
#logo {
    display: block;
    margin-left: auto;
    margin-right: auto;
    width: 512px;
    height: 256px;
}
"""

with gr.Blocks(css=custom_css) as demo:
    gr.Image(logo_image, elem_id="logo")
    gr.Markdown(f"<h1 style='text-align: center;'>{title}</h1>")
    gr.Markdown(description)
    
    with gr.Row():
        with gr.Column():
            input_text = gr.Textbox(lines=5, placeholder="Enter latin text here...", label="Input Text")
        with gr.Column():
            output_text = gr.Textbox(lines=5, placeholder="Output text will appear here...", label="Output Text")
    
    clean_button = gr.Button("Generate Text")
    clean_button.click(fn=generate_text, inputs=input_text, outputs=output_text)
    
    gr.Examples(examples=examples, inputs=input_text)
    gr.Markdown(article)

demo.launch(share=True)
# iface = gr.Interface(fn=generate_text, inputs="text", outputs="text", 
#                      description=description, title=title, examples=examples, article=article)