File size: 1,219 Bytes
3bd2481
 
 
67dec20
 
 
 
 
 
 
3bd2481
 
67dec20
 
 
 
 
 
3bd2481
67dec20
3bd2481
 
67dec20
3bd2481
 
67dec20
 
 
 
 
 
 
 
 
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
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}")