Spaces:
Sleeping
Sleeping
File size: 1,660 Bytes
4ca439a eeb1fec 4ca439a eeb1fec 4ca439a eeb1fec 4ca439a eeb1fec 4ca439a c44a2cb eeb1fec c44a2cb eeb1fec 4ca439a eeb1fec |
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 53 54 55 56 57 58 |
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()
|