File size: 1,321 Bytes
1398925
 
 
 
 
1887d77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af1aba9
07a4801
 
 
41f74ad
dd4e5c1
07a4801
dd4e5c1
13081ab
af1aba9
07a4801
5be9305
07a4801
 
59e6f0a
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
import gradio as gr
from transformers import MarianMTModel, MarianTokenizer

# Define the translator function
def translate_text(text, target_language):
    model_name = {
        "Spanish": "Helsinki-NLP/opus-mt-en-es",
        "French": "Helsinki-NLP/opus-mt-en-fr",
        "Italian": "Helsinki-NLP/opus-mt-en-it",
        "Ukrainian": "Helsinki-NLP/opus-mt-en-uk",
    }

    tokenizer = MarianTokenizer.from_pretrained(model_name[target_language])
    model = MarianMTModel.from_pretrained(model_name[target_language])

    # Tokenize the input text
    input_tokens = tokenizer(text, return_tensors="pt")

    # Generate translated tokens
    translated_tokens = model.generate(**input_tokens)

    # Decode the translated tokens
    translated_text = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)

    return translated_text

# Define the Gradio interface
language_options = ["Spanish", "French", "Italian", "Ukrainian"]
inputs = [
    gr.inputs.Textbox(lines=5, label="Enter text to translate:"),
    gr.inputs.Dropdown(choices=language_options, label="Select target language:"),
]
outputs = gr.outputs.Textbox(label="Translated text:")


iface = gr.Interface(fn=translate_text, inputs=inputs, outputs=outputs, title="Language Translator")

# Launch the Hugging Face Spaces app
iface.launch()