Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,37 +1,34 @@
|
|
1 |
-
streamlit
|
2 |
-
transformers
|
3 |
-
|
4 |
import streamlit as st
|
5 |
from transformers import pipeline
|
6 |
|
7 |
# Function to load the translation pipeline based on the target language
|
8 |
@st.cache_resource
|
9 |
def load_translation_pipeline(target_language):
|
10 |
-
if target_language ==
|
11 |
-
model_name =
|
12 |
-
elif target_language ==
|
13 |
-
model_name =
|
14 |
-
elif target_language ==
|
15 |
-
model_name =
|
16 |
else:
|
17 |
-
st.error(
|
18 |
return None
|
19 |
-
return pipeline(
|
20 |
|
21 |
# Streamlit app layout
|
22 |
-
st.title(
|
23 |
|
24 |
# Input text to translate
|
25 |
-
text = st.text_area(
|
26 |
|
27 |
# Select target language
|
28 |
target_language = st.selectbox(
|
29 |
-
|
30 |
-
[
|
31 |
)
|
32 |
|
33 |
# Translate button
|
34 |
-
if st.button(
|
35 |
if text:
|
36 |
# Load the translation pipeline based on selected language
|
37 |
translation_pipeline = load_translation_pipeline(target_language)
|
@@ -39,7 +36,7 @@ if st.button("Translate"):
|
|
39 |
# Perform translation
|
40 |
translation = translation_pipeline(text)
|
41 |
translated_text = translation[0]['translation_text']
|
42 |
-
st.write(f
|
43 |
st.write(translated_text)
|
44 |
else:
|
45 |
-
st.error(
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
from transformers import pipeline
|
3 |
|
4 |
# Function to load the translation pipeline based on the target language
|
5 |
@st.cache_resource
|
6 |
def load_translation_pipeline(target_language):
|
7 |
+
if target_language == 'French':
|
8 |
+
model_name = 'Helsinki-NLP/opus-mt-en-fr'
|
9 |
+
elif target_language == 'Spanish':
|
10 |
+
model_name = 'Helsinki-NLP/opus-mt-en-es'
|
11 |
+
elif target_language == 'German':
|
12 |
+
model_name = 'Helsinki-NLP/opus-mt-en-de'
|
13 |
else:
|
14 |
+
st.error('Target language not supported!')
|
15 |
return None
|
16 |
+
return pipeline('translation', model=model_name)
|
17 |
|
18 |
# Streamlit app layout
|
19 |
+
st.title('Language Translator')
|
20 |
|
21 |
# Input text to translate
|
22 |
+
text = st.text_area('Enter text in English to translate:')
|
23 |
|
24 |
# Select target language
|
25 |
target_language = st.selectbox(
|
26 |
+
'Select target language:',
|
27 |
+
['French', 'Spanish', 'German'] # Add more languages if needed
|
28 |
)
|
29 |
|
30 |
# Translate button
|
31 |
+
if st.button('Translate'):
|
32 |
if text:
|
33 |
# Load the translation pipeline based on selected language
|
34 |
translation_pipeline = load_translation_pipeline(target_language)
|
|
|
36 |
# Perform translation
|
37 |
translation = translation_pipeline(text)
|
38 |
translated_text = translation[0]['translation_text']
|
39 |
+
st.write(f'**Translated text in {target_language}:**')
|
40 |
st.write(translated_text)
|
41 |
else:
|
42 |
+
st.error('Please enter text to translate.')
|