Update app.py
Browse files
app.py
CHANGED
@@ -1,27 +1,41 @@
|
|
1 |
import streamlit as st
|
2 |
-
from transformers import
|
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 |
|
|
|
1 |
import streamlit as st
|
2 |
+
from transformers import MarianMTModel, MarianTokenizer
|
3 |
+
|
4 |
+
# Supported languages
|
5 |
+
supported_languages = {
|
6 |
+
'English to Urdu': 'Helsinki-NLP/opus-mt-en-ur',
|
7 |
+
'English to French': 'Helsinki-NLP/opus-mt-en-fr',
|
8 |
+
'English to Spanish': 'Helsinki-NLP/opus-mt-en-es',
|
9 |
+
'French to English': 'Helsinki-NLP/opus-mt-fr-en',
|
10 |
+
'Spanish to English': 'Helsinki-NLP/opus-mt-es-en',
|
11 |
+
'Urdu to English': 'Helsinki-NLP/opus-mt-ur-en'
|
12 |
+
}
|
13 |
+
|
14 |
+
# Streamlit App Title
|
15 |
+
st.title("Multi-Language Translator")
|
16 |
+
|
17 |
+
# Input Text
|
18 |
+
input_text = st.text_area("Enter the text you want to translate:")
|
19 |
+
|
20 |
+
# Language selection dropdown
|
21 |
+
language_pair = st.selectbox("Choose the language pair:", list(supported_languages.keys()))
|
22 |
+
|
23 |
+
# Load the model and tokenizer for the selected language pair
|
24 |
+
if input_text:
|
25 |
+
model_name = supported_languages[language_pair]
|
26 |
+
model = MarianMTModel.from_pretrained(model_name)
|
27 |
+
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
28 |
+
|
29 |
+
# Tokenize and translate
|
30 |
+
translated = model.generate(**tokenizer(input_text, return_tensors="pt", padding=True))
|
31 |
+
translated_text = [tokenizer.decode(t, skip_special_tokens=True) for t in translated]
|
32 |
+
|
33 |
+
# Display the translated text
|
34 |
+
st.subheader("Translated Text:")
|
35 |
+
st.write(translated_text[0])
|
36 |
+
|
37 |
+
# Footer
|
38 |
+
st.markdown("Powered by Helsinki-NLP/Opus-MT and Hugging Face")
|
39 |
+
|
40 |
|
41 |
|