Spaces:
Sleeping
Sleeping
Claudia Espinosa commited on
Commit ·
d3b7dd0
1
Parent(s): 3d5fb4c
Lazy-load translation pipelines and add deps
Browse files
app.py
CHANGED
|
@@ -1,71 +1,71 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
"
|
| 8 |
-
"
|
| 9 |
-
"
|
| 10 |
-
"Ukrainian": pipeline("translation_en_to_uk", model="Helsinki-NLP/opus-mt-en-uk"),
|
| 11 |
-
"Russian": pipeline("translation_en_to_ru", model="Helsinki-NLP/opus-mt-en-ru"),
|
| 12 |
-
|
| 13 |
}
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
def translate(text, language):
|
|
|
|
| 16 |
if not text:
|
| 17 |
return "Please enter an English sentence."
|
| 18 |
-
|
| 19 |
-
# Perform translation using the selected model
|
| 20 |
-
result = models[language](text)
|
| 21 |
-
return result[0]['translation_text']
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
gr.Markdown("# Short Translation")
|
| 26 |
-
|
| 27 |
with gr.Row():
|
| 28 |
with gr.Column(scale=2):
|
| 29 |
input_text = gr.Textbox(
|
| 30 |
-
label="English Sentence",
|
| 31 |
placeholder="Type your English sentence here...",
|
| 32 |
lines=2
|
| 33 |
)
|
| 34 |
with gr.Row():
|
| 35 |
translate_btn = gr.Button("Translate", variant="primary")
|
| 36 |
clear_btn = gr.Button("Clear")
|
| 37 |
-
|
| 38 |
with gr.Column(scale=1):
|
| 39 |
lang_radio = gr.Radio(
|
| 40 |
-
choices=
|
| 41 |
-
"Russian"],
|
| 42 |
label="Translation Language",
|
| 43 |
value="Spanish"
|
| 44 |
)
|
| 45 |
-
|
| 46 |
-
# Output area styled to resemble the maroon box (using Gradio's color system)
|
| 47 |
output_text = gr.Textbox(
|
| 48 |
-
label="Translation",
|
| 49 |
interactive=False,
|
| 50 |
container=True,
|
| 51 |
elem_id="output-box"
|
| 52 |
)
|
| 53 |
|
| 54 |
-
# Custom CSS to mimic the maroon look from the Tkinter app
|
| 55 |
-
demo.css = """
|
| 56 |
-
#output-box textarea {
|
| 57 |
-
background-color: #800000 !important;
|
| 58 |
-
color: white !important;
|
| 59 |
-
font-size: 1.2em !important;
|
| 60 |
-
font-weight: bold !important;
|
| 61 |
-
text-align: center !important;
|
| 62 |
-
}
|
| 63 |
-
"""
|
| 64 |
-
|
| 65 |
-
# Define button actions
|
| 66 |
translate_btn.click(fn=translate, inputs=[input_text, lang_radio], outputs=output_text)
|
| 67 |
-
clear_btn.click(fn=lambda: ("", ""), inputs=None, outputs=[input_text, output_text])
|
| 68 |
|
| 69 |
-
# Launch the app
|
| 70 |
if __name__ == "__main__":
|
| 71 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
MODEL_SPECS = {
|
| 5 |
+
"Spanish": ("translation_en_to_es", "Helsinki-NLP/opus-mt-en-es"),
|
| 6 |
+
"German": ("translation_en_to_de", "Helsinki-NLP/opus-mt-en-de"),
|
| 7 |
+
"Japanese": ("translation_en_to_ja", "staka/fugumt-en-ja"),
|
| 8 |
+
"Ukrainian": ("translation_en_to_uk", "Helsinki-NLP/opus-mt-en-uk"),
|
| 9 |
+
"Russian": ("translation_en_to_ru", "Helsinki-NLP/opus-mt-en-ru"),
|
|
|
|
|
|
|
|
|
|
| 10 |
}
|
| 11 |
|
| 12 |
+
CACHED_MODELS = {}
|
| 13 |
+
|
| 14 |
+
def get_model(language: str):
|
| 15 |
+
if language not in CACHED_MODELS:
|
| 16 |
+
task, model_name = MODEL_SPECS[language]
|
| 17 |
+
CACHED_MODELS[language] = pipeline(task, model=model_name)
|
| 18 |
+
return CACHED_MODELS[language]
|
| 19 |
+
|
| 20 |
def translate(text, language):
|
| 21 |
+
text = (text or "").strip()
|
| 22 |
if not text:
|
| 23 |
return "Please enter an English sentence."
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
model = get_model(language)
|
| 26 |
+
result = model(text)
|
| 27 |
+
return result[0]["translation_text"]
|
| 28 |
+
|
| 29 |
+
CSS = """
|
| 30 |
+
#output-box textarea {
|
| 31 |
+
background-color: #800000 !important;
|
| 32 |
+
color: white !important;
|
| 33 |
+
font-size: 1.2em !important;
|
| 34 |
+
font-weight: bold !important;
|
| 35 |
+
text-align: center !important;
|
| 36 |
+
}
|
| 37 |
+
"""
|
| 38 |
+
|
| 39 |
+
with gr.Blocks(title="Short Translation Web App", css=CSS) as demo:
|
| 40 |
gr.Markdown("# Short Translation")
|
| 41 |
+
|
| 42 |
with gr.Row():
|
| 43 |
with gr.Column(scale=2):
|
| 44 |
input_text = gr.Textbox(
|
| 45 |
+
label="English Sentence",
|
| 46 |
placeholder="Type your English sentence here...",
|
| 47 |
lines=2
|
| 48 |
)
|
| 49 |
with gr.Row():
|
| 50 |
translate_btn = gr.Button("Translate", variant="primary")
|
| 51 |
clear_btn = gr.Button("Clear")
|
| 52 |
+
|
| 53 |
with gr.Column(scale=1):
|
| 54 |
lang_radio = gr.Radio(
|
| 55 |
+
choices=list(MODEL_SPECS.keys()),
|
|
|
|
| 56 |
label="Translation Language",
|
| 57 |
value="Spanish"
|
| 58 |
)
|
| 59 |
+
|
|
|
|
| 60 |
output_text = gr.Textbox(
|
| 61 |
+
label="Translation",
|
| 62 |
interactive=False,
|
| 63 |
container=True,
|
| 64 |
elem_id="output-box"
|
| 65 |
)
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
translate_btn.click(fn=translate, inputs=[input_text, lang_radio], outputs=output_text)
|
| 68 |
+
clear_btn.click(fn=lambda: ("", "Spanish", ""), inputs=None, outputs=[input_text, lang_radio, output_text])
|
| 69 |
|
|
|
|
| 70 |
if __name__ == "__main__":
|
| 71 |
demo.launch()
|