fargerm commited on
Commit
8a5cce1
1 Parent(s): 6f236a0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -30
app.py CHANGED
@@ -1,42 +1,48 @@
1
  import streamlit as st
2
  from transformers import MarianMTModel, MarianTokenizer
3
 
4
- # Define language options and corresponding language codes
 
 
 
 
 
5
  LANGUAGES = {
6
- "Spanish": "es",
7
- "French": "fr",
8
- "German": "de",
9
- "Italian": "it",
10
- "Portuguese": "pt",
 
 
 
 
 
11
  }
12
 
13
- # Load translation model and tokenizer
14
- model_name = "Helsinki-NLP/opus-mt-en-ROMANCE"
15
- tokenizer = MarianTokenizer.from_pretrained(model_name)
16
- model = MarianMTModel.from_pretrained(model_name)
17
-
18
- def translate_text(text, target_language):
19
- inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
20
- try:
21
- # Generate translations
22
- translated = model.generate(**inputs)
23
- # Decode the output
24
- translated_text = tokenizer.decode(translated[0], skip_special_tokens=True)
25
- return translated_text
26
- except Exception as e:
27
- return f"Error: {str(e)}"
28
 
29
  # Streamlit app
30
- st.title("Language Translator")
31
 
32
- # User input
33
- text_to_translate = st.text_area("Enter text in English:", "")
34
- selected_language = st.selectbox("Select target language:", list(LANGUAGES.keys()))
35
 
36
- if st.button("Translate"):
 
37
  if text_to_translate:
38
- translated_text = translate_text(text_to_translate, LANGUAGES[selected_language])
39
- st.write(f"Translated text ({selected_language}):")
40
- st.write(translated_text)
 
 
41
  else:
42
- st.warning("Please enter text to translate.")
 
1
  import streamlit as st
2
  from transformers import MarianMTModel, MarianTokenizer
3
 
4
+ # Load pre-trained MarianMT model and tokenizer
5
+ model_name = "Helsinki-NLP/opus-mt-en-xx" # Multilingual model supporting various languages
6
+ model = MarianMTModel.from_pretrained(model_name)
7
+ tokenizer = MarianTokenizer.from_pretrained(model_name)
8
+
9
+ # Supported languages and their target language codes
10
  LANGUAGES = {
11
+ 'French': 'fr',
12
+ 'Spanish': 'es',
13
+ 'German': 'de',
14
+ 'Chinese': 'zh',
15
+ 'Russian': 'ru',
16
+ 'Japanese': 'ja',
17
+ 'Arabic': 'ar',
18
+ 'Urdu': 'ur',
19
+ 'Hindi': 'hi',
20
+ 'Bengali': 'bn',
21
  }
22
 
23
+ def translate_text(text, target_lang):
24
+ # Encode the text and prepare it for translation
25
+ encoded_text = tokenizer(text, return_tensors="pt")
26
+ # Translate text
27
+ translated = model.generate(**encoded_text, forced_bos_token_id=tokenizer.get_lang_id(target_lang))
28
+ # Decode the translated text
29
+ translated_text = tokenizer.decode(translated[0], skip_special_tokens=True)
30
+ return translated_text
 
 
 
 
 
 
 
31
 
32
  # Streamlit app
33
+ st.title('Language Translator')
34
 
35
+ # Input text and language selection
36
+ text_to_translate = st.text_area("Enter text to translate")
37
+ target_language = st.selectbox("Select target language", list(LANGUAGES.keys()))
38
 
39
+ # Translate button
40
+ if st.button('Translate'):
41
  if text_to_translate:
42
+ if target_language in LANGUAGES:
43
+ translated_text = translate_text(text_to_translate, LANGUAGES[target_language])
44
+ st.write(f"Translated text ({target_language}): {translated_text}")
45
+ else:
46
+ st.error("Target language not supported.")
47
  else:
48
+ st.error("Please enter text to translate.")