Spaces:
Sleeping
Sleeping
Initial commit
Browse files
app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
|
| 4 |
+
# Load the model and tokenizer
|
| 5 |
+
model_name = "ruggsea/gpt-ita-fdi_lega"
|
| 6 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
+
|
| 9 |
+
# Define the text completion function
|
| 10 |
+
def complete_tweet(initial_text, temperature=0.7, top_k=50, top_p=0.92, repetition_penalty=1.2):
|
| 11 |
+
# Tokenize the input text
|
| 12 |
+
input_ids = tokenizer.encode(initial_text, return_tensors="pt")
|
| 13 |
+
|
| 14 |
+
# Generate text using the model with custom parameters
|
| 15 |
+
output = model.generate(
|
| 16 |
+
input_ids,
|
| 17 |
+
max_length=140,
|
| 18 |
+
do_sample=True,
|
| 19 |
+
temperature=temperature,
|
| 20 |
+
top_k=top_k,
|
| 21 |
+
top_p=top_p,
|
| 22 |
+
repetition_penalty=repetition_penalty
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
# Decode the generated output
|
| 26 |
+
completed_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 27 |
+
|
| 28 |
+
return completed_text
|
| 29 |
+
|
| 30 |
+
# Create the Gradio interface with a multiline textbox for input and output
|
| 31 |
+
tweet_input_output = gr.Textbox(
|
| 32 |
+
label="Scrivi l'inizio del tweet e premi 'Submit' per completare il tweet",
|
| 33 |
+
type="text"
|
| 34 |
+
)
|
| 35 |
+
interface = gr.Interface(
|
| 36 |
+
fn=complete_tweet,
|
| 37 |
+
inputs=tweet_input_output,
|
| 38 |
+
outputs=tweet_input_output,
|
| 39 |
+
live=False,
|
| 40 |
+
examples=[["I migranti"], ["Il ddl Zan"]],
|
| 41 |
+
title="Twitta come un parlamentare di FDI/Lega"
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
# Start the Gradio interface
|
| 45 |
+
interface.launch(share=True)
|