Spaces:
Sleeping
Sleeping
import streamlit as st | |
from transformers import pipeline | |
# Load the translation pipeline from Hugging Face | |
def load_translation_pipeline(model_name): | |
return pipeline("translation", model=model_name) | |
# Dictionary of available models and target languages | |
models = { | |
"French": "Helsinki-NLP/opus-mt-en-fr", | |
"Spanish": "Helsinki-NLP/opus-mt-en-es", | |
"German": "Helsinki-NLP/opus-mt-en-de", | |
"Chinese": "Helsinki-NLP/opus-mt-en-zh", | |
"Hindi": "Helsinki-NLP/opus-mt-en-hi", | |
} | |
# Streamlit app | |
def main(): | |
st.title("Language Translator") | |
# Input text from the user | |
text_to_translate = st.text_area("Enter the text in English:") | |
# Select the target language | |
target_language = st.selectbox("Select the target language:", list(models.keys())) | |
# Translate button | |
if st.button("Translate"): | |
# Load the appropriate translation model | |
translation_pipeline = load_translation_pipeline(models[target_language]) | |
# Translate the text | |
if text_to_translate.strip(): | |
translated_text = translation_pipeline(text_to_translate)[0]['translation_text'] | |
st.success(f"Translated Text ({target_language}):") | |
st.write(translated_text) | |
else: | |
st.error("Please enter some text to translate.") | |
if __name__ == "__main__": | |
main() | |