Spaces:
Running
on
Zero
Running
on
Zero
Leonard Püttmann
commited on
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
3 |
+
import spacy
|
4 |
+
|
5 |
+
# Load spaCy models
|
6 |
+
nlp_en = spacy.load("en_core_web_sm")
|
7 |
+
nlp_it = spacy.load("it_core_news_sm")
|
8 |
+
|
9 |
+
# Load translation models and tokenizers
|
10 |
+
tokenizer_en_it = AutoTokenizer.from_pretrained("LeonardPuettmann/Quadrifoglio-mt-en-it")
|
11 |
+
model_en_it = AutoModelForSeq2SeqLM.from_pretrained("LeonardPuettmann/Quadrifoglio-mt-en-it")
|
12 |
+
|
13 |
+
tokenizer_it_en = AutoTokenizer.from_pretrained("LeonardPuettmann/Quadrifoglio-mt-it-en")
|
14 |
+
model_it_en = AutoModelForSeq2SeqLM.from_pretrained("LeonardPuettmann/Quadrifoglio-mt-it-en")
|
15 |
+
|
16 |
+
def generate_response_en_it(input_text):
|
17 |
+
input_ids = tokenizer_en_it("translate English to Italian: " + input_text, return_tensors="pt").input_ids
|
18 |
+
output = model_en_it.generate(input_ids, max_new_tokens=256)
|
19 |
+
return tokenizer_en_it.decode(output[0], skip_special_tokens=True)
|
20 |
+
|
21 |
+
def generate_response_it_en(input_text):
|
22 |
+
input_ids = tokenizer_it_en("translate Italian to English: " + input_text, return_tensors="pt").input_ids
|
23 |
+
output = model_it_en.generate(input_ids, max_new_tokens=256)
|
24 |
+
return tokenizer_it_en.decode(output[0], skip_special_tokens=True)
|
25 |
+
|
26 |
+
def translate_text(input_text, direction):
|
27 |
+
if direction == "en-it":
|
28 |
+
nlp = nlp_en
|
29 |
+
generate_response = generate_response_en_it
|
30 |
+
elif direction == "it-en":
|
31 |
+
nlp = nlp_it
|
32 |
+
generate_response = generate_response_it_en
|
33 |
+
else:
|
34 |
+
return "Invalid direction selected."
|
35 |
+
|
36 |
+
doc = nlp(input_text)
|
37 |
+
sentences = [sent.text for sent in doc.sents]
|
38 |
+
|
39 |
+
sentence_translations = []
|
40 |
+
for sentence in sentences:
|
41 |
+
sentence_translation = generate_response(sentence)
|
42 |
+
sentence_translations.append(sentence_translation)
|
43 |
+
|
44 |
+
full_translation = " ".join(sentence_translations)
|
45 |
+
return full_translation
|
46 |
+
|
47 |
+
# Create the Gradio interface
|
48 |
+
iface = gr.Interface(
|
49 |
+
fn=translate_text,
|
50 |
+
inputs=[gr.Textbox(lines=5, placeholder="Enter text to translate..."),
|
51 |
+
gr.Dropdown(choices=["en-it", "it-en"], label="Translation Direction")],
|
52 |
+
outputs=gr.Textbox(lines=5, label="Translation")
|
53 |
+
)
|
54 |
+
|
55 |
+
# Launch the interface
|
56 |
+
iface.launch()
|