Abdullah-Basar commited on
Commit
2f2de3b
Β·
verified Β·
1 Parent(s): 9b4807e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -12
app.py CHANGED
@@ -1,11 +1,11 @@
1
  import streamlit as st
2
- from transformers import MarianMTModel, MarianTokenizer
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 an open-source multilingual model.
8
- This app supports a wide range of languages with ease of use.
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
- # Universal Model for Language Translation
62
- model_name = f"Helsinki-NLP/opus-mt-mul-mul"
63
- tokenizer = MarianTokenizer.from_pretrained(model_name)
64
- model = MarianMTModel.from_pretrained(model_name)
65
 
66
- # Translation
67
- inputs = tokenizer(f">>{languages[target_language]}<< {source_text}", return_tensors="pt", padding=True, truncation=True)
68
- translated_tokens = model.generate(**inputs)
69
- translated_text = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
 
 
 
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:")