File size: 1,563 Bytes
6ceb35a
 
 
 
 
 
 
 
 
 
 
 
c776a71
 
6ceb35a
c776a71
 
 
 
 
6ceb35a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0436755
6ceb35a
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
# from transformers import pipeline
# import gradio as grad
# model_name = "Helsinki-NLP/opus-mt-en-de"
# opus_translator = pipeline("translation", model=model_name)

# def translate(text):
#     response = opus_translator(text)
#     return response
# grad.Interface(translate, inputs=["text",], outputs="text").launch()

#Please make sure you have `sentencepiece` installed in order to use this tokenizer.

from transformers import pipeline
import gradio as grad

model_name = "Helsinki-NLP/opus-mt-en-de"
opus_translator = pipeline("translation", model=model_name)

def translate(text):
    response = opus_translator(text)
    return response[0]['translation_text']

iface = grad.Interface(
    fn=translate,
    inputs=[
        grad.Textbox(
            placeholder="Enter the text in English...",
            label="English Text",
            lines=5
        )
    ],
    outputs=grad.Textbox(label="German Translation"),
    live=True,
    title="English to German Translator",
    description="Type in English and get the translation in German instantly!",
    examples=[
        ["Hello, how are you?"],
        ["The weather is nice today."],
        ["Gradio is great for creating interfaces."]
    ],
    theme="dark",  # Use a dark theme for the interface
    layout="horizontal",  # Use a horizontal layout
    allow_flagging="never",  # Disable the flagging option
    css="""
        .interface { max-width: 800px; margin: auto; }
        .input_textbox { font-size: 16px; }
        .output_textbox { font-size: 16px; }
    """
)

iface.launch()