Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import gradio as gr
|
|
@@ -14,86 +117,134 @@ def translate_text(text, src_lang, tgt_lang):
|
|
| 14 |
Function to handle translation using the MBART model
|
| 15 |
"""
|
| 16 |
if not text.strip():
|
| 17 |
-
return "Please enter text to translate"
|
| 18 |
-
|
| 19 |
try:
|
| 20 |
# Use the translation method with model-specific parameters
|
|
|
|
|
|
|
|
|
|
| 21 |
result = client.translation(
|
| 22 |
text,
|
| 23 |
model="facebook/mbart-large-50-many-to-many-mmt",
|
| 24 |
-
src_lang=
|
| 25 |
-
tgt_lang=
|
| 26 |
)
|
| 27 |
-
return result
|
| 28 |
-
|
| 29 |
except Exception as e:
|
| 30 |
return f"Error in translation: {str(e)}"
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
# Create the Gradio interface
|
| 33 |
-
with gr.Blocks(
|
| 34 |
-
gr.Markdown("#
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
with gr.Row():
|
| 38 |
with gr.Column():
|
| 39 |
src_lang = gr.Dropdown(
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
value="ru_RU",
|
| 45 |
-
label="Source Language Code"
|
| 46 |
-
)
|
| 47 |
input_text = gr.Textbox(
|
| 48 |
lines=4,
|
| 49 |
placeholder="Enter text to translate...",
|
| 50 |
-
label="
|
| 51 |
-
value="Меня зовут Вольфганг и я живу в Берлине"
|
| 52 |
)
|
| 53 |
-
|
| 54 |
with gr.Column():
|
| 55 |
tgt_lang = gr.Dropdown(
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
value="en_XX",
|
| 61 |
-
label="Target Language Code"
|
| 62 |
-
)
|
| 63 |
output_text = gr.Textbox(
|
| 64 |
lines=4,
|
| 65 |
label="Translation",
|
| 66 |
interactive=False
|
| 67 |
)
|
| 68 |
-
|
| 69 |
# Translate button
|
| 70 |
-
translate_btn = gr.Button("Translate
|
| 71 |
translate_btn.click(
|
| 72 |
fn=translate_text,
|
| 73 |
inputs=[input_text, src_lang, tgt_lang],
|
| 74 |
outputs=output_text
|
| 75 |
)
|
| 76 |
|
| 77 |
-
# Examples for quick testing
|
| 78 |
-
gr.Examples(
|
| 79 |
-
examples=[
|
| 80 |
-
["Меня зовут Вольфганг и я живу в Берлине", "ru_RU", "en_XX"],
|
| 81 |
-
["Hello, how are you today?", "en_XX", "es_XX"],
|
| 82 |
-
["Bonjour, comment ça va?", "fr_XX", "en_XX"],
|
| 83 |
-
["今天天气很好", "zh_CN", "en_XX"]
|
| 84 |
-
],
|
| 85 |
-
inputs=[input_text, src_lang, tgt_lang]
|
| 86 |
-
)
|
| 87 |
-
|
| 88 |
# Clear button
|
| 89 |
-
clear_btn = gr.Button("Clear")
|
| 90 |
clear_btn.click(
|
| 91 |
-
|
| 92 |
-
|
| 93 |
)
|
| 94 |
|
| 95 |
print("hellow")
|
| 96 |
|
| 97 |
# Launch the interface
|
| 98 |
-
if
|
| 99 |
demo.launch(share=True)
|
|
|
|
| 1 |
+
# import os
|
| 2 |
+
# from huggingface_hub import InferenceClient
|
| 3 |
+
# import gradio as gr
|
| 4 |
+
|
| 5 |
+
# # Initialize the client with the correct provider
|
| 6 |
+
# HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
| 7 |
+
# client = InferenceClient(
|
| 8 |
+
# provider="hf-inference",
|
| 9 |
+
# api_key=HF_TOKEN
|
| 10 |
+
# )
|
| 11 |
+
|
| 12 |
+
# def translate_text(text, src_lang, tgt_lang):
|
| 13 |
+
# """
|
| 14 |
+
# Function to handle translation using the MBART model
|
| 15 |
+
# """
|
| 16 |
+
# if not text.strip():
|
| 17 |
+
# return "Please enter text to translate"
|
| 18 |
+
|
| 19 |
+
# try:
|
| 20 |
+
# # Use the translation method with model-specific parameters
|
| 21 |
+
# result = client.translation(
|
| 22 |
+
# text,
|
| 23 |
+
# model="facebook/mbart-large-50-many-to-many-mmt",
|
| 24 |
+
# src_lang=src_lang, # Source language code
|
| 25 |
+
# tgt_lang=tgt_lang # Target language code
|
| 26 |
+
# )
|
| 27 |
+
# return result
|
| 28 |
+
|
| 29 |
+
# except Exception as e:
|
| 30 |
+
# return f"Error in translation: {str(e)}"
|
| 31 |
+
|
| 32 |
+
# # Create the Gradio interface
|
| 33 |
+
# with gr.Blocks(title="Multilingual Translation Chatbot", theme="soft") as demo:
|
| 34 |
+
# gr.Markdown("# 🌍 Multilingual Translation Chatbot")
|
| 35 |
+
# gr.Markdown("Translate between 50 languages using facebook/mbart-large-50-many-to-many-mmt")
|
| 36 |
+
|
| 37 |
+
# with gr.Row():
|
| 38 |
+
# with gr.Column():
|
| 39 |
+
# src_lang = gr.Dropdown(
|
| 40 |
+
# choices=[
|
| 41 |
+
# "ar_AR", "en_XX", "fr_XX", "es_XX", "de_DE",
|
| 42 |
+
# "zh_CN", "hi_IN", "ru_RU", "ja_XX", "pt_XX", "it_IT"
|
| 43 |
+
# ],
|
| 44 |
+
# value="ru_RU",
|
| 45 |
+
# label="Source Language Code"
|
| 46 |
+
# )
|
| 47 |
+
# input_text = gr.Textbox(
|
| 48 |
+
# lines=4,
|
| 49 |
+
# placeholder="Enter text to translate...",
|
| 50 |
+
# label="Input Text",
|
| 51 |
+
# value="Меня зовут Вольфганг и я живу в Берлине"
|
| 52 |
+
# )
|
| 53 |
+
|
| 54 |
+
# with gr.Column():
|
| 55 |
+
# tgt_lang = gr.Dropdown(
|
| 56 |
+
# choices=[
|
| 57 |
+
# "ar_AR", "en_XX", "fr_XX", "es_XX", "de_DE",
|
| 58 |
+
# "zh_CN", "hi_IN", "ru_RU", "ja_XX", "pt_XX", "it_IT"
|
| 59 |
+
# ],
|
| 60 |
+
# value="en_XX",
|
| 61 |
+
# label="Target Language Code"
|
| 62 |
+
# )
|
| 63 |
+
# output_text = gr.Textbox(
|
| 64 |
+
# lines=4,
|
| 65 |
+
# label="Translation",
|
| 66 |
+
# interactive=False
|
| 67 |
+
# )
|
| 68 |
+
|
| 69 |
+
# # Translate button
|
| 70 |
+
# translate_btn = gr.Button("Translate 🌐", variant="primary")
|
| 71 |
+
# translate_btn.click(
|
| 72 |
+
# fn=translate_text,
|
| 73 |
+
# inputs=[input_text, src_lang, tgt_lang],
|
| 74 |
+
# outputs=output_text
|
| 75 |
+
# )
|
| 76 |
+
|
| 77 |
+
# # Examples for quick testing
|
| 78 |
+
# gr.Examples(
|
| 79 |
+
# examples=[
|
| 80 |
+
# ["Меня зовут Вольфганг и я живу в Берлине", "ru_RU", "en_XX"],
|
| 81 |
+
# ["Hello, how are you today?", "en_XX", "es_XX"],
|
| 82 |
+
# ["Bonjour, comment ça va?", "fr_XX", "en_XX"],
|
| 83 |
+
# ["今天天气很好", "zh_CN", "en_XX"]
|
| 84 |
+
# ],
|
| 85 |
+
# inputs=[input_text, src_lang, tgt_lang]
|
| 86 |
+
# )
|
| 87 |
+
|
| 88 |
+
# # Clear button
|
| 89 |
+
# clear_btn = gr.Button("Clear")
|
| 90 |
+
# clear_btn.click(
|
| 91 |
+
# fn=lambda: ["", "ru_RU", "en_XX", ""],
|
| 92 |
+
# outputs=[input_text, src_lang, tgt_lang, output_text]
|
| 93 |
+
# )
|
| 94 |
+
|
| 95 |
+
# print("hellow")
|
| 96 |
+
|
| 97 |
+
# # Launch the interface
|
| 98 |
+
# if __name__ == "__main__":
|
| 99 |
+
# demo.launch(share=True)
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
|
| 104 |
import os
|
| 105 |
from huggingface_hub import InferenceClient
|
| 106 |
import gradio as gr
|
|
|
|
| 117 |
Function to handle translation using the MBART model
|
| 118 |
"""
|
| 119 |
if not text.strip():
|
| 120 |
+
return "Please enter any text to translate 😃"
|
| 121 |
+
|
| 122 |
try:
|
| 123 |
# Use the translation method with model-specific parameters
|
| 124 |
+
src_code = lang_map[src_lang]
|
| 125 |
+
tgt_code = lang_map[tgt_lang]
|
| 126 |
+
|
| 127 |
result = client.translation(
|
| 128 |
text,
|
| 129 |
model="facebook/mbart-large-50-many-to-many-mmt",
|
| 130 |
+
src_lang=src_code,
|
| 131 |
+
tgt_lang=tgt_code
|
| 132 |
)
|
| 133 |
+
return result.translation_text
|
| 134 |
+
|
| 135 |
except Exception as e:
|
| 136 |
return f"Error in translation: {str(e)}"
|
| 137 |
|
| 138 |
+
custom_theme = gr.themes.Default().set(
|
| 139 |
+
body_background_fill="#D3D3D3", # light grey button
|
| 140 |
+
button_primary_background_fill="#000000", # black background
|
| 141 |
+
button_primary_text_color="#FFFFFF" # white text
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
|
| 145 |
# Create the Gradio interface
|
| 146 |
+
with gr.Blocks(theme=custom_theme) as demo:
|
| 147 |
+
gr.Markdown("# Multilingual Text Translator")
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
lang_map = {
|
| 151 |
+
"Arabic": "ar_AR",
|
| 152 |
+
"Czech": "cs_CZ",
|
| 153 |
+
"German": "de_DE",
|
| 154 |
+
"English": "en_XX",
|
| 155 |
+
"Spanish": "es_XX",
|
| 156 |
+
"Estonian": "et_EE",
|
| 157 |
+
"Finnish": "fi_FI",
|
| 158 |
+
"French": "fr_XX",
|
| 159 |
+
"Gujarati": "gu_IN",
|
| 160 |
+
"Hindi": "hi_IN",
|
| 161 |
+
"Italian": "it_IT",
|
| 162 |
+
"Japanese": "ja_XX",
|
| 163 |
+
"Kazakh": "kk_KZ",
|
| 164 |
+
"Korean": "ko_KR",
|
| 165 |
+
"Lithuanian": "lt_LT",
|
| 166 |
+
"Latvian": "lv_LV",
|
| 167 |
+
"Burmese": "my_MM",
|
| 168 |
+
"Nepali": "ne_NP",
|
| 169 |
+
"Dutch": "nl_XX",
|
| 170 |
+
"Romanian": "ro_RO",
|
| 171 |
+
"Russian": "ru_RU",
|
| 172 |
+
"Sinhala": "si_LK",
|
| 173 |
+
"Turkish": "tr_TR",
|
| 174 |
+
"Vietnamese": "vi_VN",
|
| 175 |
+
"Chinese": "zh_CN",
|
| 176 |
+
"Afrikaans": "af_ZA",
|
| 177 |
+
"Azerbaijani": "az_AZ",
|
| 178 |
+
"Bengali": "bn_IN",
|
| 179 |
+
"Persian": "fa_IR",
|
| 180 |
+
"Hebrew": "he_IL",
|
| 181 |
+
"Croatian": "hr_HR",
|
| 182 |
+
"Indonesian": "id_ID",
|
| 183 |
+
"Georgian": "ka_GE",
|
| 184 |
+
"Khmer": "km_KH",
|
| 185 |
+
"Macedonian": "mk_MK",
|
| 186 |
+
"Malayalam": "ml_IN",
|
| 187 |
+
"Mongolian": "mn_MN",
|
| 188 |
+
"Marathi": "mr_IN",
|
| 189 |
+
"Polish": "pl_PL",
|
| 190 |
+
"Pashto": "ps_AF",
|
| 191 |
+
"Portuguese": "pt_XX",
|
| 192 |
+
"Swedish": "sv_SE",
|
| 193 |
+
"Swahili": "sw_KE",
|
| 194 |
+
"Tamil": "ta_IN",
|
| 195 |
+
"Telugu": "te_IN",
|
| 196 |
+
"Thai": "th_TH",
|
| 197 |
+
"Tagalog": "tl_XX",
|
| 198 |
+
"Ukrainian": "uk_UA",
|
| 199 |
+
"Urdu": "ur_PK",
|
| 200 |
+
"Xhosa": "xh_ZA",
|
| 201 |
+
"Galician": "gl_ES",
|
| 202 |
+
"Slovene": "sl_SI"
|
| 203 |
+
}
|
| 204 |
+
|
| 205 |
+
|
| 206 |
with gr.Row():
|
| 207 |
with gr.Column():
|
| 208 |
src_lang = gr.Dropdown(
|
| 209 |
+
choices=list(lang_map.keys()),
|
| 210 |
+
value="English", # now matches the choices
|
| 211 |
+
label="Source Language"
|
| 212 |
+
)
|
|
|
|
|
|
|
|
|
|
| 213 |
input_text = gr.Textbox(
|
| 214 |
lines=4,
|
| 215 |
placeholder="Enter text to translate...",
|
| 216 |
+
label="Enter Text",
|
|
|
|
| 217 |
)
|
| 218 |
+
|
| 219 |
with gr.Column():
|
| 220 |
tgt_lang = gr.Dropdown(
|
| 221 |
+
choices=list(lang_map.keys()),
|
| 222 |
+
value="English",
|
| 223 |
+
label="Target Language"
|
| 224 |
+
)
|
|
|
|
|
|
|
|
|
|
| 225 |
output_text = gr.Textbox(
|
| 226 |
lines=4,
|
| 227 |
label="Translation",
|
| 228 |
interactive=False
|
| 229 |
)
|
| 230 |
+
|
| 231 |
# Translate button
|
| 232 |
+
translate_btn = gr.Button("Translate ✨", elem_id="tr-btn", variant="primary")
|
| 233 |
translate_btn.click(
|
| 234 |
fn=translate_text,
|
| 235 |
inputs=[input_text, src_lang, tgt_lang],
|
| 236 |
outputs=output_text
|
| 237 |
)
|
| 238 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
# Clear button
|
| 240 |
+
clear_btn = gr.Button("Clear", elem_id="clr-btn", variant="secondary")
|
| 241 |
clear_btn.click(
|
| 242 |
+
fn=lambda: ["", "English", "Russian", ""],
|
| 243 |
+
outputs=[input_text, src_lang, tgt_lang, output_text]
|
| 244 |
)
|
| 245 |
|
| 246 |
print("hellow")
|
| 247 |
|
| 248 |
# Launch the interface
|
| 249 |
+
if _name_ == "_main_":
|
| 250 |
demo.launch(share=True)
|