Spaces:
Sleeping
Sleeping
eaysu
commited on
Commit
•
b5d5b5e
1
Parent(s):
9bc03bc
requirements and app updated
Browse files- app.py +67 -71
- requirements.txt +2 -1
app.py
CHANGED
@@ -19,89 +19,85 @@ def load_model(model_name):
|
|
19 |
|
20 |
def translate_text(model_name, text):
|
21 |
"""
|
22 |
-
Translate text
|
23 |
"""
|
24 |
-
if not model_name or not text
|
25 |
-
return "
|
26 |
-
|
27 |
try:
|
28 |
-
# Load the
|
29 |
model, tokenizer = load_model(model_name)
|
30 |
-
|
31 |
-
# Tokenize the
|
32 |
tokens = tokenizer(text, return_tensors="pt", padding=True)
|
33 |
if torch.cuda.is_available():
|
34 |
tokens = {k: v.to('cuda') for k, v in tokens.items()}
|
35 |
-
|
36 |
-
# Generate
|
37 |
translated = model.generate(**tokens)
|
38 |
-
|
39 |
-
# Decode translation
|
40 |
return tokenizer.decode(translated[0], skip_special_tokens=True)
|
|
|
41 |
except Exception as e:
|
42 |
return f"Error: {str(e)}"
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
"""
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
model_dropdown = gr.Dropdown(
|
64 |
-
label="Select Translation Model",
|
65 |
-
choices=[
|
66 |
-
"Helsinki-NLP/opus-mt-tc-big-en-tr", # English to Turkish
|
67 |
-
"Helsinki-NLP/opus-mt-tc-big-tr-en", # Turkish to English
|
68 |
-
"Helsinki-NLP/opus-mt-tc-big-en-fr", # English to French
|
69 |
-
"Helsinki-NLP/opus-mt-tc-big-fr-en", # French to English
|
70 |
-
"Helsinki-NLP/opus-mt-en-de", # English to German
|
71 |
-
"Helsinki-NLP/opus-mt-de-en", # German to English
|
72 |
-
"Helsinki-NLP/opus-mt-tc-big-en-es", # English to Spanish
|
73 |
-
"Helsinki-NLP/opus-mt-es-en", # Spanish to English
|
74 |
-
"Helsinki-NLP/opus-mt-tc-big-en-ar", # English to Arabic
|
75 |
-
"Helsinki-NLP/opus-mt-tc-big-ar-en", # Arabic to English
|
76 |
-
"Helsinki-NLP/opus-mt-en-ur", # English to Urdu
|
77 |
-
"Helsinki-NLP/opus-mt-ur-en", # Urdu to English
|
78 |
-
"Helsinki-NLP/opus-mt-en-hi", # English to Hindi
|
79 |
-
"Helsinki-NLP/opus-mt-hi-en", # Hindi to English
|
80 |
-
"Helsinki-NLP/opus-mt-en-zh", # English to Chinese
|
81 |
-
"Helsinki-NLP/opus-mt-zh-en", # Chinese to English",
|
82 |
-
],
|
83 |
-
value="Helsinki-NLP/opus-mt-tc-big-en-tr",
|
84 |
-
interactive=True
|
85 |
-
)
|
86 |
-
|
87 |
-
input_textbox = gr.Textbox(
|
88 |
-
label="Enter Text:",
|
89 |
-
placeholder="Type here...",
|
90 |
-
lines=5,
|
91 |
-
interactive=True
|
92 |
-
)
|
93 |
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
)
|
99 |
-
|
100 |
-
|
101 |
-
fn=
|
102 |
-
inputs=[
|
103 |
-
outputs=[
|
104 |
)
|
105 |
|
106 |
-
#
|
107 |
-
|
|
|
19 |
|
20 |
def translate_text(model_name, text):
|
21 |
"""
|
22 |
+
Translate input text using the specified model.
|
23 |
"""
|
24 |
+
if not model_name or not text:
|
25 |
+
return "Please select a model and provide text for translation."
|
26 |
+
|
27 |
try:
|
28 |
+
# Load the model and tokenizer
|
29 |
model, tokenizer = load_model(model_name)
|
30 |
+
|
31 |
+
# Tokenize the text
|
32 |
tokens = tokenizer(text, return_tensors="pt", padding=True)
|
33 |
if torch.cuda.is_available():
|
34 |
tokens = {k: v.to('cuda') for k, v in tokens.items()}
|
35 |
+
|
36 |
+
# Generate translation
|
37 |
translated = model.generate(**tokens)
|
|
|
|
|
38 |
return tokenizer.decode(translated[0], skip_special_tokens=True)
|
39 |
+
|
40 |
except Exception as e:
|
41 |
return f"Error: {str(e)}"
|
42 |
|
43 |
+
# Model options
|
44 |
+
model_options = [
|
45 |
+
("English to Turkish", "Helsinki-NLP/opus-mt-tc-big-en-tr"),
|
46 |
+
("Turkish to English", "Helsinki-NLP/opus-mt-tc-big-tr-en"),
|
47 |
+
("English to French", "Helsinki-NLP/opus-mt-tc-big-en-fr"),
|
48 |
+
("French to English", "Helsinki-NLP/opus-mt-tc-big-fr-en"),
|
49 |
+
("English to German", "Helsinki-NLP/opus-mt-en-de"),
|
50 |
+
("German to English", "Helsinki-NLP/opus-mt-de-en"),
|
51 |
+
("English to Spanish", "Helsinki-NLP/opus-mt-tc-big-en-es"),
|
52 |
+
("Spanish to English", "Helsinki-NLP/opus-mt-es-en"),
|
53 |
+
("English to Arabic", "Helsinki-NLP/opus-mt-tc-big-en-ar"),
|
54 |
+
("Arabic to English", "Helsinki-NLP/opus-mt-tc-big-ar-en"),
|
55 |
+
("English to Urdu", "Helsinki-NLP/opus-mt-en-ur"),
|
56 |
+
("Urdu to English", "Helsinki-NLP/opus-mt-ur-en"),
|
57 |
+
("English to Hindi", "Helsinki-NLP/opus-mt-en-hi"),
|
58 |
+
("Hindi to English", "Helsinki-NLP/opus-mt-hi-en"),
|
59 |
+
("English to Chinese", "Helsinki-NLP/opus-mt-en-zh"),
|
60 |
+
("Chinese to English", "Helsinki-NLP/opus-mt-zh-en"),
|
61 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
+
# Create Gradio interface
|
64 |
+
with gr.Blocks() as demo:
|
65 |
+
gr.Markdown("# 🌍 Real-Time Sentence Translation")
|
66 |
+
|
67 |
+
with gr.Row():
|
68 |
+
model_dropdown = gr.Dropdown(
|
69 |
+
label="Select Translation Model",
|
70 |
+
choices=[option[1] for option in model_options],
|
71 |
+
type="value",
|
72 |
+
)
|
73 |
+
|
74 |
+
with gr.Row():
|
75 |
+
input_text = gr.Textbox(
|
76 |
+
label="Enter text (complete sentences)",
|
77 |
+
lines=5,
|
78 |
+
placeholder="Type here...",
|
79 |
+
)
|
80 |
+
|
81 |
+
with gr.Row():
|
82 |
+
translate_button = gr.Button("Translate")
|
83 |
+
clear_button = gr.Button("Clear")
|
84 |
+
|
85 |
+
output_text = gr.Textbox(label="Translated Text", interactive=False)
|
86 |
+
|
87 |
+
def clear_inputs():
|
88 |
+
return "", ""
|
89 |
+
|
90 |
+
translate_button.click(
|
91 |
+
fn=translate_text,
|
92 |
+
inputs=[model_dropdown, input_text],
|
93 |
+
outputs=output_text,
|
94 |
)
|
95 |
+
|
96 |
+
clear_button.click(
|
97 |
+
fn=clear_inputs,
|
98 |
+
inputs=[],
|
99 |
+
outputs=[input_text, output_text],
|
100 |
)
|
101 |
|
102 |
+
# Run the Gradio app
|
103 |
+
demo.launch()
|
requirements.txt
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
transformers
|
2 |
torch
|
3 |
gradio
|
4 |
-
sentencepiece
|
|
|
|
1 |
transformers
|
2 |
torch
|
3 |
gradio
|
4 |
+
sentencepiece
|
5 |
+
sacremoses
|