Saanvi12011 commited on
Commit
67dec20
·
verified ·
1 Parent(s): 3bd2481

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -8
app.py CHANGED
@@ -1,18 +1,34 @@
1
  import streamlit as st
2
  from transformers import pipeline
3
 
4
- # Initialize translation pipeline
 
 
 
 
 
 
5
  translator = pipeline("translation_en_to_es", model="Helsinki-NLP/opus-mt-en-es")
6
 
7
- st.title("Language Translation App")
8
- st.write("Translate text from English to Spanish.")
 
 
 
 
9
 
10
- text = st.text_area("Enter text in English:", placeholder="Type your text here...")
11
  if st.button("Translate"):
12
  if not text.strip():
13
- st.error("Please provide some text to translate.")
14
  else:
15
  with st.spinner("Translating..."):
16
- translation = translator(text)
17
- st.subheader("Translated Text:")
18
- st.write(translation[0]["translation_text"])
 
 
 
 
 
 
 
1
  import streamlit as st
2
  from transformers import pipeline
3
 
4
+ # Ensure sentencepiece is installed
5
+ try:
6
+ import sentencepiece
7
+ except ImportError:
8
+ st.error("The 'sentencepiece' library is required. Install it using 'pip install sentencepiece'.")
9
+
10
+ # Initialize the translation pipeline
11
  translator = pipeline("translation_en_to_es", model="Helsinki-NLP/opus-mt-en-es")
12
 
13
+ # Streamlit app
14
+ st.title("English-to-Spanish Translation App")
15
+ st.write("Translate your text from English to Spanish with ease using Hugging Face models.")
16
+
17
+ # Input text box for user
18
+ text = st.text_area("Enter text in English:", placeholder="Type your text here...", height=150)
19
 
20
+ # Button to trigger translation
21
  if st.button("Translate"):
22
  if not text.strip():
23
+ st.error("Please provide text to translate.")
24
  else:
25
  with st.spinner("Translating..."):
26
+ try:
27
+ # Perform translation
28
+ translation = translator(text)
29
+ translated_text = translation[0]["translation_text"]
30
+ st.subheader("Translated Text (Spanish):")
31
+ st.write(translated_text)
32
+ except Exception as e:
33
+ st.error(f"An error occurred during translation: {e}")
34
+