Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
MODEL_ID = "adcelis/opus-books-en-es-flan-t5-small"
|
| 5 |
+
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 7 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_ID)
|
| 8 |
+
|
| 9 |
+
def translate_text(text):
|
| 10 |
+
if not text or not text.strip():
|
| 11 |
+
return ""
|
| 12 |
+
|
| 13 |
+
prompt = f"translate English to Spanish: {text}"
|
| 14 |
+
|
| 15 |
+
inputs = tokenizer(prompt, return_tensors="pt", truncation=True)
|
| 16 |
+
outputs = model.generate(
|
| 17 |
+
**inputs,
|
| 18 |
+
max_new_tokens=128
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 22 |
+
return result
|
| 23 |
+
|
| 24 |
+
demo = gr.Interface(
|
| 25 |
+
fn=translate_text,
|
| 26 |
+
inputs=gr.Textbox(
|
| 27 |
+
lines=5,
|
| 28 |
+
label="Texto en ingl茅s",
|
| 29 |
+
placeholder="Write here the English sentence you want to translate..."
|
| 30 |
+
),
|
| 31 |
+
outputs=gr.Textbox(
|
| 32 |
+
lines=5,
|
| 33 |
+
label="Traducci贸n al espa帽ol"
|
| 34 |
+
),
|
| 35 |
+
title="English to Spanish Translation",
|
| 36 |
+
description="Demo del modelo fine-tuned con opus_books para traducir texto del ingl茅s al espa帽ol.",
|
| 37 |
+
examples=[
|
| 38 |
+
["The house was quiet and the children were sleeping."],
|
| 39 |
+
["I have never seen such a beautiful garden."],
|
| 40 |
+
["She opened the window and looked at the sea."]
|
| 41 |
+
],
|
| 42 |
+
allow_flagging="never"
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
if __name__ == "__main__":
|
| 46 |
+
demo.launch()
|