Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,6 @@ from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, pipeline
|
|
| 5 |
model_name = "Helsinki-NLP/opus-mt-en-fr" # Example model for English to French
|
| 6 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 7 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 8 |
-
translator = pipeline("translation", model=model, tokenizer=tokenizer)
|
| 9 |
|
| 10 |
# Define the translation function
|
| 11 |
def translate_text(text, target_language):
|
|
@@ -23,17 +22,22 @@ languages = {
|
|
| 23 |
"ru": "Russian"
|
| 24 |
}
|
| 25 |
|
| 26 |
-
# Set up the Gradio interface
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
iface.launch()
|
|
|
|
| 5 |
model_name = "Helsinki-NLP/opus-mt-en-fr" # Example model for English to French
|
| 6 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 7 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
|
|
|
| 8 |
|
| 9 |
# Define the translation function
|
| 10 |
def translate_text(text, target_language):
|
|
|
|
| 22 |
"ru": "Russian"
|
| 23 |
}
|
| 24 |
|
| 25 |
+
# Set up the Gradio interface with a submit button
|
| 26 |
+
with gr.Blocks() as iface:
|
| 27 |
+
gr.Markdown("# Text Translator")
|
| 28 |
+
gr.Markdown("Translate text into multiple languages using Hugging Face models.")
|
| 29 |
+
|
| 30 |
+
# Input components
|
| 31 |
+
text_input = gr.Textbox(label="Enter text to translate")
|
| 32 |
+
language_dropdown = gr.Dropdown(list(languages.keys()), label="Target Language", type="value")
|
| 33 |
+
|
| 34 |
+
# Output component
|
| 35 |
+
translation_output = gr.Textbox(label="Translation")
|
| 36 |
+
|
| 37 |
+
# Button to submit
|
| 38 |
+
submit_button = gr.Button("Translate")
|
| 39 |
+
|
| 40 |
+
# When button is clicked, trigger the translation
|
| 41 |
+
submit_button.click(fn=translate_text, inputs=[text_input, language_dropdown], outputs=translation_output)
|
| 42 |
|
| 43 |
iface.launch()
|