AreesaAshfaq commited on
Commit
2486975
1 Parent(s): 4438826

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -28
app.py CHANGED
@@ -1,34 +1,26 @@
1
  import streamlit as st
2
  from transformers import MarianMTModel, MarianTokenizer
3
 
4
- # Load the MarianMT model and tokenizer
5
- model_name = "Helsinki-NLP/opus-mt-en-many-to-many-mmt"
6
- model = MarianMTModel.from_pretrained(model_name)
7
- tokenizer = MarianTokenizer.from_pretrained(model_name)
8
-
9
- # Define target language codes directly
10
- language_codes = {
11
- 'French': 'fr',
12
- 'German': 'de',
13
- 'Italian': 'it',
14
- 'Portuguese': 'pt',
15
- 'Spanish': 'es',
16
- 'Chinese': 'zh',
17
- 'Japanese': 'ja',
18
- 'Korean': 'ko',
19
- 'Russian': 'ru',
20
- 'Arabic': 'ar',
21
- 'Turkish': 'tr',
22
- 'Hindi': 'hi',
23
- 'Swahili': 'sw',
24
- # Add more languages if needed
25
  }
26
 
27
- def translate_text(text, target_lang_code):
28
- # Prepare the input and translate
 
 
 
 
 
 
29
  inputs = tokenizer.encode(text, return_tensors="pt")
30
- # Generate the translation
31
- translated = model.generate(inputs, decoder_start_token_id=tokenizer.lang_code_to_id[target_lang_code])
32
  translated_text = tokenizer.decode(translated[0], skip_special_tokens=True)
33
  return translated_text
34
 
@@ -39,12 +31,15 @@ def main():
39
  text_to_translate = st.text_area("Enter text in English:")
40
 
41
  # Language options
42
- selected_language = st.selectbox("Select target language:", list(language_codes.keys()))
43
 
44
  if st.button("Translate"):
45
  if text_to_translate:
46
- target_lang_code = language_codes[selected_language]
47
- translated_text = translate_text(text_to_translate, target_lang_code)
 
 
 
48
  st.write(f"**Translation in {selected_language}:**")
49
  st.write(translated_text)
50
  else:
 
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
+ 'Portuguese': 'Helsinki-NLP/opus-mt-en-pt',
11
+ # Add more language models if needed
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  }
13
 
14
+ def load_model(model_name):
15
+ """Load the model and tokenizer based on the selected model name."""
16
+ model = MarianMTModel.from_pretrained(model_name)
17
+ tokenizer = MarianTokenizer.from_pretrained(model_name)
18
+ return model, tokenizer
19
+
20
+ def translate_text(text, model, tokenizer):
21
+ """Translate text using the provided model and tokenizer."""
22
  inputs = tokenizer.encode(text, return_tensors="pt")
23
+ translated = model.generate(inputs)
 
24
  translated_text = tokenizer.decode(translated[0], skip_special_tokens=True)
25
  return translated_text
26
 
 
31
  text_to_translate = st.text_area("Enter text in English:")
32
 
33
  # Language options
34
+ selected_language = st.selectbox("Select target language:", list(models.keys()))
35
 
36
  if st.button("Translate"):
37
  if text_to_translate:
38
+ # Load the selected model
39
+ model_name = models[selected_language]
40
+ model, tokenizer = load_model(model_name)
41
+
42
+ translated_text = translate_text(text_to_translate, model, tokenizer)
43
  st.write(f"**Translation in {selected_language}:**")
44
  st.write(translated_text)
45
  else: