File size: 4,302 Bytes
ea99abb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import gradio as gr
from utils.ner_helpers import is_llm_model
from typing import Dict, List, Any
from tasks.translation import text_translation

def translation_ui():
    """Translation UI component"""
    
    # Define models
    TRANSLATION_MODELS = [
        "gemini-2.0-flash"  # Only allow gemini-2.0-flash for now
        # "gpt-4",
        # "claude-2",
        # "Helsinki-NLP/opus-mt-en-vi",
        # "Helsinki-NLP/opus-mt-vi-en"
    ]
    DEFAULT_MODEL = "gemini-2.0-flash"
    
    def translate(text, model, src_lang, tgt_lang, custom_instructions):
        """Process text for translation"""
        if not text.strip():
            return "No text provided"
            
        use_llm = is_llm_model(model)
        result = text_translation(
            text=text, 
            model=model, 
            src_lang=src_lang, 
            tgt_lang=tgt_lang,
            custom_instructions=custom_instructions,
            use_llm=use_llm
        )
            
        return result
    
    # UI Components
    with gr.Row():
        with gr.Column():
            input_text = gr.Textbox(
                label="Input Text", 
                lines=8,
                placeholder="Enter text to translate...",
                elem_id="translation-input-text"
            )
            gr.Examples(
                examples=[
                    ["Vietnam's economy has grown rapidly in the past decade."],
                    ["The football match between Manchester United and Chelsea was very exciting."]
                ],
                inputs=[input_text],
                label="Examples"
            )
            with gr.Row():
                pass
            src_lang = gr.Textbox(
                label="Source Language (e.g., en, vi, ja)", 
                value="en",
                elem_id="translation-src-lang"
            )
            tgt_lang = gr.Textbox(
                label="Target Language (e.g., en, vi, ja)", 
                value="vi",
                elem_id="translation-tgt-lang"
            )
            model = gr.Dropdown(
                TRANSLATION_MODELS,
                value=DEFAULT_MODEL,
                label="Model",
                interactive=True,
                elem_id="translation-model-dropdown"
            )
            custom_instructions = gr.Textbox(
                label="Custom Instructions (optional)",
                lines=2,
                placeholder="Add any custom instructions for the model...",
                elem_id="translation-custom-instructions"
            )

            btn = gr.Button("Translate", variant="primary", elem_id="translation-btn")
        
        with gr.Column():
            output = gr.Textbox(
                label="Translation", 
                lines=10,
                elem_id="translation-output"
            )
            
            # with gr.Accordion("About Translation", open=False):
            #     gr.Markdown("""
            #     ## Text Translation
                
            #     Text translation converts text from one language to another. This tool offers:
                
            #     - **Multiple languages**: Translate between any language pair
            #     - **Multiple models**: Select from LLMs (like Gemini and GPT) or specialized translation models
            #     - **Custom instructions**: Tailor the translation to specific domains or styles
                
            #     ### Language Codes
                
            #     Use standard language codes like:
            #     - `en` for English
            #     - `vi` for Vietnamese
            #     - `ja` for Japanese
            #     - `fr` for French
            #     - `es` for Spanish
            #     - `ko` for Korean
                
            #     ### Tips
                
            #     - LLM models perform better on complex or nuanced translations
            #     - Specialized models might be faster for common language pairs
            #     - Use custom instructions to specify tones (formal/informal) or domains (technical/literary)
            #     """)
    
    # Event handlers
    btn.click(
        translate, 
        inputs=[input_text, model, src_lang, tgt_lang, custom_instructions], 
        outputs=output
    )
    
    return None