Spaces:
Running
Running
Akhil Koduri
commited on
Commit
•
3f8efd6
1
Parent(s):
9b1be4f
Update app.py
Browse files
app.py
CHANGED
@@ -1,154 +1,52 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
|
|
3 |
|
4 |
-
# Initialize the translation pipeline with the
|
5 |
-
text_translator = pipeline("
|
6 |
|
7 |
-
#
|
8 |
-
|
9 |
-
|
10 |
-
"Albanian": "sq",
|
11 |
-
"Amharic": "am",
|
12 |
-
"Arabic": "ar",
|
13 |
-
"Armenian": "hy",
|
14 |
-
"Azerbaijani": "az",
|
15 |
-
"Basque": "eu",
|
16 |
-
"Belarusian": "be",
|
17 |
-
"Bengali": "bn",
|
18 |
-
"Bosnian": "bs",
|
19 |
-
"Bulgarian": "bg",
|
20 |
-
"Catalan": "ca",
|
21 |
-
"Cebuano": "ceb",
|
22 |
-
"Chichewa": "ny",
|
23 |
-
"Chinese (Simplified)": "zh",
|
24 |
-
"Chinese (Traditional)": "zh-TW",
|
25 |
-
"Corsican": "co",
|
26 |
-
"Croatian": "hr",
|
27 |
-
"Czech": "cs",
|
28 |
-
"Danish": "da",
|
29 |
-
"Dutch": "nl",
|
30 |
-
"English": "en",
|
31 |
-
"Esperanto": "eo",
|
32 |
-
"Estonian": "et",
|
33 |
-
"Filipino": "tl",
|
34 |
-
"Finnish": "fi",
|
35 |
-
"French": "fr",
|
36 |
-
"Frisian": "fy",
|
37 |
-
"Galician": "gl",
|
38 |
-
"Georgian": "ka",
|
39 |
-
"German": "de",
|
40 |
-
"Greek": "el",
|
41 |
-
"Gujarati": "gu",
|
42 |
-
"Haitian Creole": "ht",
|
43 |
-
"Hausa": "ha",
|
44 |
-
"Hawaiian": "haw",
|
45 |
-
"Hebrew": "he",
|
46 |
-
"Hindi": "hi",
|
47 |
-
"Hmong": "hmn",
|
48 |
-
"Hungarian": "hu",
|
49 |
-
"Icelandic": "is",
|
50 |
-
"Igbo": "ig",
|
51 |
-
"Indonesian": "id",
|
52 |
-
"Irish": "ga",
|
53 |
-
"Italian": "it",
|
54 |
-
"Japanese": "ja",
|
55 |
-
"Javanese": "jv",
|
56 |
-
"Kannada": "kn",
|
57 |
-
"Kazakh": "kk",
|
58 |
-
"Khmer": "km",
|
59 |
-
"Kinyarwanda": "rw",
|
60 |
-
"Korean": "ko",
|
61 |
-
"Kurdish (Kurmanji)": "ku",
|
62 |
-
"Kyrgyz": "ky",
|
63 |
-
"Lao": "lo",
|
64 |
-
"Latin": "la",
|
65 |
-
"Latvian": "lv",
|
66 |
-
"Lithuanian": "lt",
|
67 |
-
"Luxembourgish": "lb",
|
68 |
-
"Macedonian": "mk",
|
69 |
-
"Malagasy": "mg",
|
70 |
-
"Malay": "ms",
|
71 |
-
"Malayalam": "ml",
|
72 |
-
"Maltese": "mt",
|
73 |
-
"Maori": "mi",
|
74 |
-
"Marathi": "mr",
|
75 |
-
"Mongolian": "mn",
|
76 |
-
"Myanmar (Burmese)": "my",
|
77 |
-
"Nepali": "ne",
|
78 |
-
"Norwegian": "no",
|
79 |
-
"Odia (Oriya)": "or",
|
80 |
-
"Pashto": "ps",
|
81 |
-
"Persian": "fa",
|
82 |
-
"Polish": "pl",
|
83 |
-
"Portuguese": "pt",
|
84 |
-
"Punjabi": "pa",
|
85 |
-
"Romanian": "ro",
|
86 |
-
"Russian": "ru",
|
87 |
-
"Samoan": "sm",
|
88 |
-
"Scots Gaelic": "gd",
|
89 |
-
"Serbian": "sr",
|
90 |
-
"Sesotho": "st",
|
91 |
-
"Shona": "sn",
|
92 |
-
"Sindhi": "sd",
|
93 |
-
"Sinhala": "si",
|
94 |
-
"Slovak": "sk",
|
95 |
-
"Slovenian": "sl",
|
96 |
-
"Somali": "so",
|
97 |
-
"Spanish": "es",
|
98 |
-
"Sundanese": "su",
|
99 |
-
"Swahili": "sw",
|
100 |
-
"Swedish": "sv",
|
101 |
-
"Tagalog (Filipino)": "tl",
|
102 |
-
"Tajik": "tg",
|
103 |
-
"Tamil": "ta",
|
104 |
-
"Tatar": "tt",
|
105 |
-
"Telugu": "te",
|
106 |
-
"Thai": "th",
|
107 |
-
"Turkish": "tr",
|
108 |
-
"Turkmen": "tk",
|
109 |
-
"Ukrainian": "uk",
|
110 |
-
"Urdu": "ur",
|
111 |
-
"Uyghur": "ug",
|
112 |
-
"Uzbek": "uz",
|
113 |
-
"Vietnamese": "vi",
|
114 |
-
"Welsh": "cy",
|
115 |
-
"Xhosa": "xh",
|
116 |
-
"Yiddish": "yi",
|
117 |
-
"Yoruba": "yo",
|
118 |
-
"Zulu": "zu"
|
119 |
-
}
|
120 |
|
121 |
-
def
|
122 |
"""
|
123 |
-
|
124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
Args:
|
126 |
text (str): The text to translate.
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
Returns:
|
131 |
-
str: The translated text
|
132 |
"""
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
return "Unsupported language selected. Please choose valid source and target languages."
|
138 |
|
139 |
try:
|
140 |
# Perform translation using T5 model pipeline
|
141 |
translation = text_translator(text,
|
142 |
-
|
143 |
-
|
144 |
translated_text = translation[0]["translation_text"]
|
145 |
-
|
146 |
-
# Optionally return confidence score
|
147 |
-
score = translation[0].get("score")
|
148 |
-
if score:
|
149 |
-
return f"Translation: {translated_text}\nConfidence Score: {score:.2f}"
|
150 |
-
else:
|
151 |
-
return translated_text
|
152 |
except Exception as e:
|
153 |
return f"Translation error: {str(e)}"
|
154 |
|
@@ -157,14 +55,13 @@ def main():
|
|
157 |
interface = gr.Interface(
|
158 |
fn=translate_text,
|
159 |
inputs=[
|
160 |
-
gr.Textbox(label="Input text to translate", lines=6, placeholder="Enter text in
|
161 |
-
gr.Dropdown(
|
162 |
-
gr.Dropdown(list(language_mapping.keys()), label="Select destination language")
|
163 |
],
|
164 |
outputs=[gr.Textbox(label="Translated text", lines=4)],
|
165 |
title="Multi-Language Translator",
|
166 |
-
description="Translate text
|
167 |
-
theme=
|
168 |
live=True # Enable live updates
|
169 |
)
|
170 |
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
+
import json
|
4 |
|
5 |
+
# Initialize the translation pipeline with the specific model
|
6 |
+
text_translator = pipeline("translation", model="facebook/nllb-200-distilled-600M")
|
7 |
|
8 |
+
# Load the JSON table containing language mappings
|
9 |
+
with open('language.json') as f:
|
10 |
+
language_data = json.load(f)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
+
def get_flores_200_code(language):
|
13 |
"""
|
14 |
+
Retrieves the FLORES-200 code for a given language from the loaded JSON data.
|
15 |
+
|
16 |
+
Args:
|
17 |
+
language (str): The name of the language.
|
18 |
+
|
19 |
+
Returns:
|
20 |
+
str: The FLORES-200 code for the language, or None if not found.
|
21 |
+
"""
|
22 |
+
for code in language_data:
|
23 |
+
if code['Language'] == language:
|
24 |
+
return code['FLORES-200 code']
|
25 |
+
return None
|
26 |
+
|
27 |
+
def translate_text(text, destination_language):
|
28 |
+
"""
|
29 |
+
Translates text from English to the selected destination language using the T5 model.
|
30 |
+
|
31 |
Args:
|
32 |
text (str): The text to translate.
|
33 |
+
destination_language (str): The target language for translation.
|
34 |
+
|
|
|
35 |
Returns:
|
36 |
+
str: The translated text.
|
37 |
"""
|
38 |
+
dest_code = get_flores_200_code(destination_language)
|
39 |
+
|
40 |
+
if not dest_code:
|
41 |
+
return "Unsupported language selected. Please choose a valid language."
|
|
|
42 |
|
43 |
try:
|
44 |
# Perform translation using T5 model pipeline
|
45 |
translation = text_translator(text,
|
46 |
+
src_lang="eng_Latn",
|
47 |
+
tgt_lang=dest_code)
|
48 |
translated_text = translation[0]["translation_text"]
|
49 |
+
return translated_text
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
except Exception as e:
|
51 |
return f"Translation error: {str(e)}"
|
52 |
|
|
|
55 |
interface = gr.Interface(
|
56 |
fn=translate_text,
|
57 |
inputs=[
|
58 |
+
gr.Textbox(label="Input text to translate", lines=6, placeholder="Enter text in English..."),
|
59 |
+
gr.Dropdown([code['Language'] for code in language_data], label="Select destination language")
|
|
|
60 |
],
|
61 |
outputs=[gr.Textbox(label="Translated text", lines=4)],
|
62 |
title="Multi-Language Translator",
|
63 |
+
description="Translate English text to multiple languages using the T5 model. Select the target language from the dropdown menu and enter the text to translate.",
|
64 |
+
theme=gr.themes.Soft(),
|
65 |
live=True # Enable live updates
|
66 |
)
|
67 |
|