Spaces:
Sleeping
Sleeping
# Run the script and open the link in the browser. | |
import os | |
import json | |
import gradio as gr | |
import streamlit as st | |
import torch | |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline | |
# scratch with latbert tokenizer | |
CHECKPOINT_PATH= 'scratch_2-nodes_tokenizer_latbert-original_packing_fcocchi/' | |
CHECKPOINT_PATH= 'itserr/latin_llm_alpha' | |
print(f"Loading model from: {CHECKPOINT_PATH}") | |
tokenizer = AutoTokenizer.from_pretrained(CHECKPOINT_PATH, token=os.environ['HF_TOKEN']) | |
model = AutoModelForCausalLM.from_pretrained(CHECKPOINT_PATH, token=os.environ['HF_TOKEN']) | |
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. \n | |
Demo instructions: | |
- Enter a prompt in Latin in the Input Text box. | |
- Select the temperature value to control the randomness of the generated text (higher value produce a more creative and unstable answer). | |
- Click the 'Generate Text' button to trigger model generation. | |
- (Optional) insert a Feedback text in the box. | |
- Click the 'Like' or 'Dislike' button to judge the generation correctness. | |
""" | |
title= "(L<sup>2</sup>) - Latin Language Model" | |
article= "hello world ..." | |
examples= ['Accidere ex una scintilla', 'Audacter calumniare,', 'Consolatium misero comites'] | |
logo_image= 'ITSERR_row_logo.png' | |
def generate_text(prompt, slider): | |
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=slider, repetition_penalty=2.0, truncation=True) | |
return generated_text[0]['generated_text'] | |
# Function to handle user preferences | |
def handle_preference(preference, input, output, feedback, temp_value, preferences_file="preferences.json"): | |
""" | |
Format values stored in preferences: | |
- input text | |
- output generated text | |
- user feedback | |
- float temperature value | |
""" | |
if os.path.exists(preferences_file): | |
with open(preferences_file, "r") as file: | |
preferences = json.load(file) | |
else: | |
preferences = {"like": [], "dislike": [], "count_like": 0, "count_dislike": 0} | |
if input == output: | |
output_tuple= ("", "", feedback) | |
else: | |
output_tuple= (input, output.split(input)[-1], feedback, temp_value) | |
if preference == "like": | |
preferences["like"].append(output_tuple) | |
if output_tuple[1] != "" : | |
preferences["count_like"] += 1 | |
elif preference == "dislike": | |
preferences["dislike"].append(output_tuple) | |
if output_tuple[1] != "" : | |
preferences["count_dislike"] += 1 | |
with open(preferences_file, "w") as file: | |
json.dump(preferences, file) | |
print(f"Admin log: like: {preferences['count_like']} and dislike: {preferences['count_dislike']}") | |
return f"You select '{preference}' as answer of the model generation. Thank you for your time!" | |
custom_css = """ | |
#logo { | |
display: block; | |
margin-left: auto; | |
margin-right: auto; | |
width: 280px; | |
height: 140px; | |
} | |
""" | |
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") | |
gr.Examples(examples=examples, inputs=input_text) | |
temperature_slider = gr.Slider(minimum=0.1, maximum=5.0, step=0.1, value=1.0, label="Temperature") | |
clean_button = gr.Button("Generate Text") | |
clean_button.click(fn=generate_text, inputs=[input_text, temperature_slider], outputs=output_text) | |
feedback_output = gr.Textbox(lines=1, placeholder="If you want to provide a feedback, please fill this box ...", label="Feedback") | |
with gr.Row(): | |
like_button = gr.Button("Like") | |
dislike_button = gr.Button("Dislike") | |
button_output = gr.Textbox(lines=1, placeholder="Please submit your choice", label="Latin Language Model Demo") | |
like_button.click(fn=lambda x,y,z,v: handle_preference("like", x, y, z, v), inputs=[input_text, output_text, feedback_output, temperature_slider], outputs=button_output) | |
dislike_button.click(fn=lambda x,y,z,v: handle_preference("dislike", x, y, z, v), inputs=[input_text, output_text, feedback_output, temperature_slider], outputs=button_output) | |
#gr.Markdown(article) | |
demo.launch(share=True) | |