Spaces:
Sleeping
Sleeping
Hatoku Alicia
commited on
Commit
路
bcf5039
1
Parent(s):
b38e54d
Add application file
Browse files
app.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Crear pipelines de traducci贸n espec铆ficos para cada combinaci贸n de idiomas
|
| 5 |
+
translation_pipelines = {
|
| 6 |
+
"G": pipeline("translation_en_to_de", model="Helsinki-NLP/opus-mt-en-de"),
|
| 7 |
+
"F": pipeline("translation_en_to_fr", model="Helsinki-NLP/opus-mt-en-fr"),
|
| 8 |
+
"S": pipeline("translation_en_to_es", model="Helsinki-NLP/opus-mt-en-es"),
|
| 9 |
+
"I": pipeline("translation_en_to_it", model="Helsinki-NLP/opus-mt-en-it")
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
def translate(text, target_language):
|
| 13 |
+
if not target_language:
|
| 14 |
+
return "Please select a target language."
|
| 15 |
+
|
| 16 |
+
pipe = translation_pipelines[target_language[0]]
|
| 17 |
+
|
| 18 |
+
if not pipe:
|
| 19 |
+
raise ValueError(f"Language '{target_language[0]}' not supported")
|
| 20 |
+
|
| 21 |
+
translation = pipe(text)
|
| 22 |
+
translated_text = translation[0]['translation_text']
|
| 23 |
+
return translated_text
|
| 24 |
+
|
| 25 |
+
with gr.Blocks() as demo:
|
| 26 |
+
inp = gr.Textbox(label="What do you wanna translate?")
|
| 27 |
+
language = gr.Radio(["German", "French", "Spanish", "Italian"], label="Select target language")
|
| 28 |
+
output = gr.Textbox(label="Translation")
|
| 29 |
+
|
| 30 |
+
translate_btn = gr.Button("Translate")
|
| 31 |
+
translate_btn.click(translate, inputs=[inp, language], outputs=output)
|
| 32 |
+
|
| 33 |
+
demo.launch(share=True)
|