Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use a pipeline as a high-level helper
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import torch
|
| 4 |
+
import json
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
text_translator = pipeline("translation", model="facebook/nllb-200-distilled-600M", torch_dtype=torch.bfloat16)
|
| 8 |
+
|
| 9 |
+
# Load the JSON table
|
| 10 |
+
with open('language.json') as f:
|
| 11 |
+
language_data = json.load(f)
|
| 12 |
+
|
| 13 |
+
def get_flores_200_code(language):
|
| 14 |
+
for code in language_data:
|
| 15 |
+
if code['Language'] == language:
|
| 16 |
+
return code['FLORES-200 code']
|
| 17 |
+
return None
|
| 18 |
+
|
| 19 |
+
def translate_text(text, destination_language):
|
| 20 |
+
|
| 21 |
+
# text = "Hello friends how are you?"
|
| 22 |
+
dest_code = get_flores_200_code(destination_language)
|
| 23 |
+
|
| 24 |
+
translation = text_translator(text,
|
| 25 |
+
src_lang="eng_Latn",
|
| 26 |
+
tgt_lang=dest_code)
|
| 27 |
+
return translation[0]["translation_text"]
|
| 28 |
+
|
| 29 |
+
gr.close_all()
|
| 30 |
+
|
| 31 |
+
demo = gr.Interface(fn=translate_text,
|
| 32 |
+
inputs=[gr.Textbox(label="Input text to translate",lines=6), gr.Dropdown(["English", "Bulgarian",
|
| 33 |
+
"Croatian",
|
| 34 |
+
"Czech",
|
| 35 |
+
"Danish",
|
| 36 |
+
"Dutch",
|
| 37 |
+
"English",
|
| 38 |
+
"Estonian",
|
| 39 |
+
"Finnish",
|
| 40 |
+
"French",
|
| 41 |
+
"German",
|
| 42 |
+
"Greek",
|
| 43 |
+
"Hungarian",
|
| 44 |
+
"Irish",
|
| 45 |
+
"Italian",
|
| 46 |
+
"Latvian",
|
| 47 |
+
"Lithuanian",
|
| 48 |
+
"Maltese",
|
| 49 |
+
"Polish",
|
| 50 |
+
"Portuguese",
|
| 51 |
+
"Romanian",
|
| 52 |
+
"Slovak",
|
| 53 |
+
"Slovenian",
|
| 54 |
+
"Spanish",
|
| 55 |
+
"Swedish"],label="Select destination language")],
|
| 56 |
+
outputs=[gr.Textbox(label="Translated text", lines=4)],
|
| 57 |
+
title="Multi Language translator - From European languages to English",
|
| 58 |
+
description="This application uses NLLB model from Facebook trained on 200 languages for machine translation research",
|
| 59 |
+
concurrency_limit=16)
|
| 60 |
+
demo.launch()
|