Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,11 @@
|
|
1 |
import streamlit as st
|
2 |
-
from transformers import
|
3 |
|
4 |
# App Title and Description
|
5 |
st.title("π Universal Language Translator App")
|
6 |
st.write("""
|
7 |
-
Translate text from any language to any other language using
|
8 |
-
This app supports
|
9 |
""")
|
10 |
|
11 |
# Instructions
|
@@ -41,7 +41,6 @@ languages = {
|
|
41 |
"Vietnamese": "vi",
|
42 |
"Hebrew": "he",
|
43 |
"Swahili": "sw",
|
44 |
-
"Amharic": "am",
|
45 |
"Tamil": "ta",
|
46 |
"Telugu": "te",
|
47 |
"Punjabi": "pa",
|
@@ -58,15 +57,18 @@ if st.button("Translate"):
|
|
58 |
st.error("Please enter some text to translate.")
|
59 |
else:
|
60 |
try:
|
61 |
-
#
|
62 |
-
model_name =
|
63 |
-
tokenizer =
|
64 |
-
model =
|
65 |
|
66 |
-
#
|
67 |
-
|
68 |
-
|
69 |
-
|
|
|
|
|
|
|
70 |
|
71 |
# Display Translated Text
|
72 |
st.subheader("π Translated Text:")
|
|
|
1 |
import streamlit as st
|
2 |
+
from transformers import M2M100ForConditionalGeneration, M2M100Tokenizer
|
3 |
|
4 |
# App Title and Description
|
5 |
st.title("π Universal Language Translator App")
|
6 |
st.write("""
|
7 |
+
Translate text from any language to any other language using the open-source M2M100 multilingual model.
|
8 |
+
This app supports over 100 languages and provides a seamless translation experience.
|
9 |
""")
|
10 |
|
11 |
# Instructions
|
|
|
41 |
"Vietnamese": "vi",
|
42 |
"Hebrew": "he",
|
43 |
"Swahili": "sw",
|
|
|
44 |
"Tamil": "ta",
|
45 |
"Telugu": "te",
|
46 |
"Punjabi": "pa",
|
|
|
57 |
st.error("Please enter some text to translate.")
|
58 |
else:
|
59 |
try:
|
60 |
+
# Load M2M100 model and tokenizer
|
61 |
+
model_name = "facebook/m2m100_418M"
|
62 |
+
tokenizer = M2M100Tokenizer.from_pretrained(model_name)
|
63 |
+
model = M2M100ForConditionalGeneration.from_pretrained(model_name)
|
64 |
|
65 |
+
# Set source and target language
|
66 |
+
tokenizer.src_lang = languages[source_language]
|
67 |
+
encoded_text = tokenizer(source_text, return_tensors="pt")
|
68 |
+
generated_tokens = model.generate(**encoded_text, forced_bos_token_id=tokenizer.get_lang_id(languages[target_language]))
|
69 |
+
|
70 |
+
# Decode the translated text
|
71 |
+
translated_text = tokenizer.decode(generated_tokens[0], skip_special_tokens=True)
|
72 |
|
73 |
# Display Translated Text
|
74 |
st.subheader("π Translated Text:")
|