File size: 2,435 Bytes
1bd219f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import gradio as gr
from collections import OrderedDict
from transformers import pipeline

def get_all_source_languages():
    """

    Returns a human-readable `dict source_languages_names:codes` 

    based on the available models.

    """
    source_languages = {
        "English": "en",
        "French": "fr",
        "Spanish": "es",
        "German": "de",
        "Italian": "it"
    }
    source_languages = OrderedDict(sorted(source_languages.items()))
    return source_languages

source_lang_dict = get_all_source_languages()
target_lang_dict = source_lang_dict  # Reutilizamos el mismo diccionario para los idiomas de destino

def translate(input_text, source, target):
    # Convertir nombres legibles a códigos abreviados
    source_code = source_lang_dict.get(source, source)  # 'en', 'fr', etc.
    target_code = target_lang_dict.get(target, target)  # 'en', 'fr', etc.
    
    try:
        model_name = f"Helsinki-NLP/opus-mt-{source_code}-{target_code}"
        pipe = pipeline("translation", model=model_name)
        translation = pipe(input_text)
        return translation[0]['translation_text'], ""
    except Exception as e:
        return "", f"Error: {str(e)}"

with gr.Blocks() as demo:
    gr.HTML("""

    <html>

        <head><style>h1 { text-align: center; }</style></head>

        <body><h1>Open Translate</h1></body>

    </html>

    """)
    with gr.Row():
        with gr.Column():
            source_language_dropdown = gr.Dropdown(
                choices=list(source_lang_dict.keys()),
                value=list(source_lang_dict.keys())[0],
                label="Source Language"
            )
            input_textbox = gr.Textbox(
                lines=5, placeholder="Enter text to translate", label="Input Text"
            )
        with gr.Column():
            target_language_dropdown = gr.Dropdown(
                choices=list(target_lang_dict.keys()),
                value="English",
                label="Target Language"
            )
            translated_textbox = gr.Textbox(lines=5, placeholder="", label="Translated Text")
    info_label = gr.HTML("")
    btn = gr.Button("Translate")
    btn.click(
        translate,
        inputs=[input_textbox, source_language_dropdown, target_language_dropdown],
        outputs=[translated_textbox, info_label]
    )

if __name__ == "__main__":
    demo.launch()