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

def translate(text, target_language):
    language_codes = {
        "Spanish": "es",
        "French": "fr",
        "Italian": "it",
        "Ukrainian": "uk",
        "Brazilian Portuguese": "pt",
        "Japanese": "ja",
        "Russian": "ru",
        "Chinese": "zh",
        "Moroccan Arabic": "ary",
        "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", "Italian", "Ukrainian", "Brazilian Portuguese",
    "Japanese", "Russian", "Chinese", "Moroccan Arabic", "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()