Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
import torch
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
translator = pipeline("translation", model="facebook/nllb-200-distilled-600M")
|
| 7 |
+
|
| 8 |
+
languages = {
|
| 9 |
+
"English": "eng_Latn",
|
| 10 |
+
"French": "fra_Latn",
|
| 11 |
+
"Spanish": "spa_Latn",
|
| 12 |
+
"German": "deu_Latn",
|
| 13 |
+
"Arabic": "arb_Arab",
|
| 14 |
+
"Chinese": "zho_Hans",
|
| 15 |
+
"Russian": "rus_Cyrl"
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
def translate_text(text, src_lang, tgt_lang):
|
| 19 |
+
if not text.strip():
|
| 20 |
+
return "Please enter some text to translate."
|
| 21 |
+
translation = translator(text, src_lang=languages[src_lang], tgt_lang=languages[tgt_lang])
|
| 22 |
+
return translation[0]["translation_text"]
|
| 23 |
+
|
| 24 |
+
with gr.Blocks() as demo:
|
| 25 |
+
gr.Markdown("## Translation using NLLB-200")
|
| 26 |
+
gr.Markdown("Select source and target languages, then translate the text.")
|
| 27 |
+
|
| 28 |
+
with gr.Row():
|
| 29 |
+
with gr.Column():
|
| 30 |
+
input_text = gr.Textbox(label="Input Text", placeholder="Type here...", lines=3)
|
| 31 |
+
src_lang = gr.Dropdown(choices=list(languages.keys()), value="English", label="Source Language")
|
| 32 |
+
tgt_lang = gr.Dropdown(choices=list(languages.keys()), value="French", label="Target Language")
|
| 33 |
+
with gr.Row():
|
| 34 |
+
clear_btn = gr.Button("Clear")
|
| 35 |
+
submit_btn = gr.Button("Submit", variant="primary")
|
| 36 |
+
|
| 37 |
+
with gr.Column():
|
| 38 |
+
output_text = gr.Textbox(label="Translated Text", interactive=False, lines=3)
|
| 39 |
+
flag_btn = gr.Button("Flag")
|
| 40 |
+
|
| 41 |
+
submit_btn.click(translate_text, inputs=[input_text, src_lang, tgt_lang], outputs=output_text)
|
| 42 |
+
clear_btn.click(lambda: ("", "English", "French"), outputs=[input_text, src_lang, tgt_lang])
|
| 43 |
+
|
| 44 |
+
if __name__ == "__main__":
|
| 45 |
+
demo.launch(share=True)
|