Spaces:
Sleeping
Sleeping
import streamlit as st | |
from transformers import pipeline | |
# Install sentencepiece (ensure it is installed) | |
# !pip install sentencepiece | |
def load_translator(target_lang): | |
model_name = f"Helsinki-NLP/opus-mt-en-{target_lang}" | |
translator = pipeline("translation", model=model_name) | |
return translator | |
def translate_text(translator, text): | |
translation = translator(text) | |
return translation[0]['translation_text'] | |
def main(): | |
st.title("Language Translator") | |
st.write("Translate text from English to your desired language.") | |
# Input text | |
input_text = st.text_area("Enter text in English:", "") | |
# Select target language | |
target_lang = st.selectbox("Select target language:", [ | |
"fr", # French | |
"es", # Spanish | |
"de", # German | |
"it", # Italian | |
"zh", # Chinese | |
"hi", # Hindi | |
"ja", # Japanese | |
"ru", # Russian | |
"ar" # Arabic | |
"ur" # Urdu | |
]) | |
if st.button("Translate"): | |
if input_text: | |
translator = load_translator(target_lang) | |
translated_text = translate_text(translator, input_text) | |
st.write(f"**Translated text in {target_lang}:**") | |
st.write(translated_text) | |
else: | |
st.write("Please enter text to translate.") | |
if __name__ == "__main__": | |
main() | |