Waseem771's picture
Update app.py
89fc4af verified
raw
history blame
1.37 kB
import streamlit as st
from transformers import pipeline
# Install sentencepiece (ensure it is installed)
# !pip install sentencepiece
@st.cache_resource
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()