AreesaAshfaq commited on
Commit
dd6ec5a
·
verified ·
1 Parent(s): 842a9ce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -14
app.py CHANGED
@@ -6,10 +6,21 @@ model_name = "Helsinki-NLP/opus-mt-en-ROMANCE"
6
  model = MarianMTModel.from_pretrained(model_name)
7
  tokenizer = MarianTokenizer.from_pretrained(model_name)
8
 
9
- def translate_text(text, target_lang):
 
 
 
 
 
 
 
 
 
 
10
  # Prepare the input and translate
11
  inputs = tokenizer.encode(text, return_tensors="pt")
12
- translated = model.generate(inputs, decoder_start_token_id=tokenizer.lang_code_to_id[target_lang])
 
13
  translated_text = tokenizer.decode(translated[0], skip_special_tokens=True)
14
  return translated_text
15
 
@@ -20,21 +31,12 @@ def main():
20
  text_to_translate = st.text_area("Enter text in English:")
21
 
22
  # Language options
23
- languages = {
24
- 'French': 'fr',
25
- 'German': 'de',
26
- 'Italian': 'it',
27
- 'Portuguese': 'pt',
28
- 'Spanish': 'es',
29
- # Add more languages if needed
30
- }
31
-
32
- selected_language = st.selectbox("Select target language:", list(languages.keys()))
33
 
34
  if st.button("Translate"):
35
  if text_to_translate:
36
- target_lang = languages[selected_language]
37
- translated_text = translate_text(text_to_translate, target_lang)
38
  st.write(f"**Translation in {selected_language}:**")
39
  st.write(translated_text)
40
  else:
 
6
  model = MarianMTModel.from_pretrained(model_name)
7
  tokenizer = MarianTokenizer.from_pretrained(model_name)
8
 
9
+ # Define language codes supported by the model
10
+ language_codes = {
11
+ 'French': 'fr',
12
+ 'German': 'de',
13
+ 'Italian': 'it',
14
+ 'Portuguese': 'pt',
15
+ 'Spanish': 'es',
16
+ # Add more languages if needed
17
+ }
18
+
19
+ def translate_text(text, target_lang_code):
20
  # Prepare the input and translate
21
  inputs = tokenizer.encode(text, return_tensors="pt")
22
+ # Generate the translation
23
+ translated = model.generate(inputs, decoder_start_token_id=tokenizer.get_lang_id(target_lang_code))
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(language_codes.keys()))
 
 
 
 
 
 
 
 
 
35
 
36
  if st.button("Translate"):
37
  if text_to_translate:
38
+ target_lang_code = language_codes[selected_language]
39
+ translated_text = translate_text(text_to_translate, target_lang_code)
40
  st.write(f"**Translation in {selected_language}:**")
41
  st.write(translated_text)
42
  else: