Spaces:
Sleeping
Sleeping
Moezulhaq24
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,42 +1,40 @@
|
|
1 |
import streamlit as st
|
2 |
from transformers import pipeline
|
3 |
|
4 |
-
#
|
5 |
-
|
6 |
-
def load_translation_pipeline(model_name):
|
7 |
-
return pipeline("translation", model=model_name)
|
8 |
-
|
9 |
-
# Dictionary of available models and target languages
|
10 |
-
models = {
|
11 |
"French": "Helsinki-NLP/opus-mt-en-fr",
|
12 |
"Spanish": "Helsinki-NLP/opus-mt-en-es",
|
13 |
"German": "Helsinki-NLP/opus-mt-en-de",
|
14 |
-
"
|
15 |
-
"
|
16 |
}
|
17 |
|
18 |
# Streamlit app
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
#
|
26 |
-
|
27 |
-
|
28 |
-
#
|
|
|
|
|
|
|
29 |
if st.button("Translate"):
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
38 |
else:
|
39 |
-
st.
|
40 |
-
|
41 |
-
if __name__ == "__main__":
|
42 |
-
main()
|
|
|
1 |
import streamlit as st
|
2 |
from transformers import pipeline
|
3 |
|
4 |
+
# Define model mapping for each target language
|
5 |
+
model_mapping = {
|
|
|
|
|
|
|
|
|
|
|
6 |
"French": "Helsinki-NLP/opus-mt-en-fr",
|
7 |
"Spanish": "Helsinki-NLP/opus-mt-en-es",
|
8 |
"German": "Helsinki-NLP/opus-mt-en-de",
|
9 |
+
"Italian": "Helsinki-NLP/opus-mt-en-it",
|
10 |
+
"Urdu": "Helsinki-NLP/opus-mt-en-ur" # Added Urdu translation
|
11 |
}
|
12 |
|
13 |
# Streamlit app
|
14 |
+
st.title("Language Translator")
|
15 |
+
|
16 |
+
# Sidebar for language selection and translation output
|
17 |
+
with st.sidebar:
|
18 |
+
st.header("Translate Text")
|
19 |
+
|
20 |
+
# Input text
|
21 |
+
text = st.text_area("Enter text in English:")
|
22 |
+
|
23 |
+
# Target language selection
|
24 |
+
selected_language = st.selectbox("Select target language:", list(model_mapping.keys()))
|
25 |
+
|
26 |
+
# Translation button
|
27 |
if st.button("Translate"):
|
28 |
+
if text:
|
29 |
+
# Get the appropriate model based on selected language
|
30 |
+
model_name = model_mapping[selected_language]
|
31 |
+
translator = pipeline("translation", model=model_name)
|
32 |
+
|
33 |
+
# Perform translation
|
34 |
+
translation = translator(text, max_length=400)[0]['translation_text']
|
35 |
+
|
36 |
+
# Display translation
|
37 |
+
st.write(f"*Translation in {selected_language}:*")
|
38 |
+
st.write(translation)
|
39 |
else:
|
40 |
+
st.write("Please enter some text to translate.")
|
|
|
|
|
|