Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,97 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import gradio as gr
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
-
|
| 5 |
|
| 6 |
-
|
| 7 |
HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
| 8 |
-
client = InferenceClient(
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
def
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
| 25 |
with gr.Row():
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
|
|
|
| 33 |
if __name__ == "__main__":
|
| 34 |
-
demo.launch(
|
|
|
|
| 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 |
+
# Launch the interface
|
| 96 |
if __name__ == "__main__":
|
| 97 |
+
demo.launch(share=True)
|