File size: 1,487 Bytes
1398925
 
 
6908aad
bf83ac1
 
d86acb9
 
bf83ac1
 
d86acb9
 
bf83ac1
3bc002c
 
 
 
 
 
bf83ac1
 
 
6908aad
 
 
 
 
 
 
 
d86acb9
 
 
07a4801
13081ab
6908aad
 
 
 
 
 
 
 
af1aba9
07a4801
59e6f0a
6908aad
3bc002c
d86acb9
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
import gradio as gr
from transformers import MarianMTModel, MarianTokenizer

def translate(text, target_language):
    language_codes = {
        "Spanish": "es",
        "French (European)": "fr",
        "French (Canadian)": "fr",
        "Italian": "it",
        "Ukrainian": "uk",
        "Portuguese (Brazilian)": "pt_BR",
        "Portuguese (European)": "pt",
        "Russian": "ru",
        "Chinese": "zh",
        "Dutch": "nl",
        "German": "de",
        "Arabic": "ar",
        "Hebrew": "he",
        "Greek": "el"
    }
    target_language_code = language_codes[target_language]
    model_name = f'helsinki-nlp/opus-mt-en-{target_language_code}'
    tokenizer = MarianTokenizer.from_pretrained(model_name)
    model = MarianMTModel.from_pretrained(model_name)
    inputs = tokenizer(text, return_tensors="pt")
    outputs = model.generate(**inputs)
    translation = tokenizer.decode(outputs[0], skip_special_tokens=True)
    return translation

language_options = [
    "Spanish", "French (European)", "French (Canadian)", "Italian", "Ukrainian",
    "Portuguese (Brazilian)", "Portuguese (European)", "Russian", "Chinese",
    "Dutch", "German", "Arabic", "Hebrew", "Greek"
]

iface = gr.Interface(
    fn=translate,
    inputs=[
        gr.inputs.Textbox(lines=5, label="Enter text to translate:"),
        gr.inputs.Dropdown(choices=language_options, label="Target Language"),
    ],
    outputs=gr.outputs.Textbox(label="Translated Text"),
)

iface.launch()