TANVEERMAKHDOOM commited on
Commit
334c0b0
1 Parent(s): 8b5752e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -39
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
- # Attempt to import the transformers package
11
- try:
12
- from transformers import MarianMTModel, MarianTokenizer
13
- except ImportError as e:
14
- st.error("Failed to import transformers. Please make sure it is installed.")
15
- st.stop()
16
-
17
- # Initialize the model and tokenizer
18
- def load_model_and_tokenizer(target_lang):
19
- model_name = f'Helsinki-NLP/opus-mt-en-{target_lang}'
 
 
 
 
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
- tokens = tokenizer(text, return_tensors="pt", padding=True)
27
- translated_tokens = model.generate(**tokens)
28
- translated_text = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
 
29
  return translated_text
30
 
31
- # Streamlit app
32
- st.title("Language Translator")
33
 
34
- # Input text
35
- input_text = st.text_area("Enter text to translate", "Hello, how are you?")
36
 
37
- # Language selection
38
- target_language = st.selectbox(
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 input_text:
49
- translated_text = translate_text(input_text, model, tokenizer)
50
- st.subheader("Translated text")
 
 
 
 
51
  st.write(translated_text)
52
  else:
53
- st.error("Please enter some text to translate.")
 
 
 
 
 
 
 
 
 
 
 
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()