translator / app.py
Rogerjs's picture
Update app.py
eeb1fec verified
raw
history blame
1.66 kB
import gradio as gr
from transformers import pipeline
import requests
# Load translation pipelines
translator_en_fi = pipeline(
"translation_en_to_fi",
model="Helsinki-NLP/opus-mt-en-fi",
# Optional: cache_dir="./model_cache",
# Optional parameters:
# max_length=512, num_beams=5
)
translator_fi_en = pipeline(
"translation_fi_to_en",
model="Helsinki-NLP/opus-mt-fi-en",
# max_length=512, num_beams=5
)
def translate(text, direction):
text = text.strip()
if not text:
return "Please enter some text for translation."
# Simple input validation
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
# Optional: Provide some example texts to guide users.
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,
allow_flagging="never", # Disables any flagging if you don't need it
enable_queue=True # Allows request queuing if concurrency is needed
)
iface.launch()