Spaces:
Sleeping
Sleeping
File size: 2,297 Bytes
821e7c3 |
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 59 60 61 62 63 64 65 66 |
import streamlit as st
from transformers import pipeline
from langdetect import detect
# Title of the app
st.title("Language Translator App")
# Available translation models
MODELS = {
"English to German": "Helsinki-NLP/opus-mt-en-de",
"German to English": "Helsinki-NLP/opus-mt-de-en",
"English to French": "Helsinki-NLP/opus-mt-en-fr",
"French to English": "Helsinki-NLP/opus-mt-fr-en",
"English to Spanish": "Helsinki-NLP/opus-mt-en-es",
"Spanish to English": "Helsinki-NLP/opus-mt-es-en",
"English to Chinese": "Helsinki-NLP/opus-mt-en-zh",
"Chinese to English": "Helsinki-NLP/opus-mt-zh-en",
"English to Russian": "Helsinki-NLP/opus-mt-en-ru",
"Russian to English": "Helsinki-NLP/opus-mt-ru-en",
"English to Arabic": "Helsinki-NLP/opus-mt-en-ar",
"Arabic to English": "Helsinki-NLP/opus-mt-ar-en",
"English to Hindi": "Helsinki-NLP/opus-mt-en-hi",
"Hindi to English": "Helsinki-NLP/opus-mt-hi-en",
}
# Sidebar for model selection
st.sidebar.header("Settings")
translation_direction = st.sidebar.selectbox(
"Choose translation direction",
list(MODELS.keys())
)
# Load the translation pipeline
@st.cache_resource
def load_translator(model_name):
return pipeline("translation", model=model_name)
translator = load_translator(MODELS[translation_direction])
# Input text area
st.header("Enter Text to Translate")
input_text = st.text_area("Input Text", "Hello, how are you?")
# Detect language
if input_text.strip() != "":
try:
detected_lang = detect(input_text)
st.subheader("Detected Language")
st.write(f"The detected language is: **{detected_lang}**")
except Exception as e:
st.error(f"Language detection failed: {e}")
# Translate button
if st.button("Translate"):
if input_text.strip() == "":
st.warning("Please enter some text to translate.")
else:
with st.spinner("Translating..."):
try:
translation = translator(input_text)
translated_text = translation[0]['translation_text']
st.success("Translation Complete!")
st.subheader("Translated Text")
st.write(translated_text)
except Exception as e:
st.error(f"Translation failed: {e}") |