File size: 1,256 Bytes
4ca439a
 
 
785ddc4
 
 
4ca439a
 
eeb1fec
 
 
 
 
 
4ca439a
 
 
 
 
 
eeb1fec
 
 
 
 
4ca439a
 
c44a2cb
eeb1fec
 
c44a2cb
eeb1fec
 
785ddc4
 
4ca439a
 
785ddc4
 
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
import gradio as gr
from transformers import pipeline

# Initialize the translation pipelines
translator_en_fi = pipeline("translation_en_to_fi", model="Helsinki-NLP/opus-mt-en-fi")
translator_fi_en = pipeline("translation_fi_to_en", model="Helsinki-NLP/opus-mt-fi-en")

def translate(text, direction):
    text = text.strip()
    if not text:
        return "Please enter some text for translation."
    if len(text) > 2000:
        return "Input text too long. Please shorten it."
    
    if direction == 'en-fi':
        result = translator_en_fi(text)[0]['translation_text']
    else:
        result = translator_fi_en(text)[0]['translation_text']
    return result

examples = [
    ["Hello, how are you?", "en-fi"],
    ["Mitä kuuluu?", "fi-en"]
]

iface = gr.Interface(
    fn=translate,
    inputs=[
        gr.Textbox(lines=3, placeholder="Enter text here..."),
        gr.Radio(choices=["en-fi", "fi-en"], label="Translation Direction", value="en-fi")
    ],
    outputs=gr.Textbox(label="Translated Text"),
    title="English-Finnish Translation App",
    description="This application uses Helsinki-NLP translation models to translate text between English and Finnish.",
    examples=examples
)

if __name__ == "__main__":
    iface.launch()