TANVEERMAKHDOOM
commited on
Commit
•
334c0b0
1
Parent(s):
8b5752e
Update app.py
Browse files
app.py
CHANGED
@@ -1,53 +1,50 @@
|
|
1 |
-
|
2 |
-
try:
|
3 |
-
from transformers import MarianMTModel, MarianTokenizer
|
4 |
-
print("Transformers imported successfully")
|
5 |
-
except ImportError as e:
|
6 |
-
print(f"ImportError: {e}")
|
7 |
-
|
8 |
import streamlit as st
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
20 |
model = MarianMTModel.from_pretrained(model_name)
|
21 |
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
22 |
return model, tokenizer
|
23 |
|
24 |
-
# Function to translate text
|
25 |
def translate_text(text, model, tokenizer):
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
29 |
return translated_text
|
30 |
|
31 |
-
|
32 |
-
st.title("
|
33 |
|
34 |
-
#
|
35 |
-
|
36 |
|
37 |
-
# Language
|
38 |
-
|
39 |
-
"Select target language",
|
40 |
-
["fr", "de", "es", "it", "pt", "ru", "zh", "ja", "ar"]
|
41 |
-
)
|
42 |
|
43 |
-
# Load model and tokenizer based on selected language
|
44 |
-
if target_language:
|
45 |
-
model, tokenizer = load_model_and_tokenizer(target_language)
|
46 |
-
|
47 |
if st.button("Translate"):
|
48 |
-
if
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
51 |
st.write(translated_text)
|
52 |
else:
|
53 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from transformers import MarianMTModel, MarianTokenizer
|
3 |
+
|
4 |
+
# Define a dictionary to map language names to model identifiers
|
5 |
+
models = {
|
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',
|
11 |
+
'Arabic': 'Helsinki-NLP/opus-mt-en-ar',
|
12 |
+
# Add more language models if needed
|
13 |
+
}
|
14 |
+
|
15 |
+
def load_model(model_name):
|
16 |
+
"""Load the model and tokenizer based on the selected model name."""
|
17 |
model = MarianMTModel.from_pretrained(model_name)
|
18 |
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
19 |
return model, tokenizer
|
20 |
|
|
|
21 |
def translate_text(text, model, tokenizer):
|
22 |
+
"""Translate text using the provided model and tokenizer."""
|
23 |
+
inputs = tokenizer.encode(text, return_tensors="pt")
|
24 |
+
translated = model.generate(inputs)
|
25 |
+
translated_text = tokenizer.decode(translated[0], skip_special_tokens=True)
|
26 |
return translated_text
|
27 |
|
28 |
+
def main():
|
29 |
+
st.title("Multilingual Translator")
|
30 |
|
31 |
+
# User input for English text
|
32 |
+
text_to_translate = st.text_area("Enter text in English:")
|
33 |
|
34 |
+
# Language options
|
35 |
+
selected_language = st.selectbox("Select target language:", list(models.keys()))
|
|
|
|
|
|
|
36 |
|
|
|
|
|
|
|
|
|
37 |
if st.button("Translate"):
|
38 |
+
if text_to_translate:
|
39 |
+
# Load the selected model
|
40 |
+
model_name = models[selected_language]
|
41 |
+
model, tokenizer = load_model(model_name)
|
42 |
+
|
43 |
+
translated_text = translate_text(text_to_translate, model, tokenizer)
|
44 |
+
st.write(f"**Translation in {selected_language}:**")
|
45 |
st.write(translated_text)
|
46 |
else:
|
47 |
+
st.warning("Please enter text to translate.")
|
48 |
+
|
49 |
+
if __name__ == "__main__":
|
50 |
+
main()
|