import streamlit as st from transformers import pipeline # Ensure sentencepiece is installed try: import sentencepiece except ImportError: st.error("The 'sentencepiece' library is required. Install it using 'pip install sentencepiece'.") # Initialize the translation pipeline translator = pipeline("translation_en_to_es", model="Helsinki-NLP/opus-mt-en-es") # Streamlit app st.title("English-to-Spanish Translation App") st.write("Translate your text from English to Spanish with ease using Hugging Face models.") # Input text box for user text = st.text_area("Enter text in English:", placeholder="Type your text here...", height=150) # Button to trigger translation if st.button("Translate"): if not text.strip(): st.error("Please provide text to translate.") else: with st.spinner("Translating..."): try: # Perform translation translation = translator(text) translated_text = translation[0]["translation_text"] st.subheader("Translated Text (Spanish):") st.write(translated_text) except Exception as e: st.error(f"An error occurred during translation: {e}")