fargerm commited on
Commit
f855702
1 Parent(s): a51a9d1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -9
app.py CHANGED
@@ -7,9 +7,6 @@ from io import BytesIO
7
 
8
  # Define the language options
9
  languages = {
10
- 'es': 'Spanish',
11
- 'fr': 'French',
12
- 'de': 'German',
13
  'ur': 'Urdu',
14
  'hi': 'Hindi',
15
  'bn': 'Bengali'
@@ -19,21 +16,20 @@ languages = {
19
  st.title("Real-Time Language Translator and TTS")
20
 
21
  # Text input
22
- text_to_translate = st.text_area("Enter text to translate:", "Hello, how are you?")
23
 
24
  # Language selection
25
  target_language = st.selectbox("Select target language:", list(languages.keys()))
26
 
27
  def translate_text(text, target_lang):
28
- # Update the model and tokenizer based on the selected language
29
  model_name = f"Helsinki-NLP/opus-mt-en-{target_lang}"
30
  tokenizer = MarianTokenizer.from_pretrained(model_name)
31
  model = MarianMTModel.from_pretrained(model_name)
32
 
33
  # Translate text
34
  encoded_text = tokenizer.encode(text, return_tensors="pt")
35
- forced_bos_token_id = model.config.forced_bos_token_id
36
- translated = model.generate(encoded_text, forced_bos_token_id=forced_bos_token_id)
37
  translated_text = tokenizer.decode(translated[0], skip_special_tokens=True)
38
  return translated_text
39
 
@@ -65,8 +61,11 @@ if st.button("Translate and Synthesize Speech"):
65
  st.write(f"Translated text ({languages[target_language]}): {translated_text}")
66
 
67
  # Perform text-to-speech
68
- audio_bytes = synthesize_speech(translated_text)
69
- st.audio(audio_bytes, format="audio/wav")
 
 
 
70
 
71
 
72
 
 
7
 
8
  # Define the language options
9
  languages = {
 
 
 
10
  'ur': 'Urdu',
11
  'hi': 'Hindi',
12
  'bn': 'Bengali'
 
16
  st.title("Real-Time Language Translator and TTS")
17
 
18
  # Text input
19
+ text_to_translate = st.text_area("Enter text to translate:", "My Name is Meer Hassan Farhad, I live in Islamabad")
20
 
21
  # Language selection
22
  target_language = st.selectbox("Select target language:", list(languages.keys()))
23
 
24
  def translate_text(text, target_lang):
25
+ # Use a more specific model for translation
26
  model_name = f"Helsinki-NLP/opus-mt-en-{target_lang}"
27
  tokenizer = MarianTokenizer.from_pretrained(model_name)
28
  model = MarianMTModel.from_pretrained(model_name)
29
 
30
  # Translate text
31
  encoded_text = tokenizer.encode(text, return_tensors="pt")
32
+ translated = model.generate(encoded_text)
 
33
  translated_text = tokenizer.decode(translated[0], skip_special_tokens=True)
34
  return translated_text
35
 
 
61
  st.write(f"Translated text ({languages[target_language]}): {translated_text}")
62
 
63
  # Perform text-to-speech
64
+ try:
65
+ audio_bytes = synthesize_speech(translated_text)
66
+ st.audio(audio_bytes, format="audio/wav")
67
+ except Exception as e:
68
+ st.error(f"Error generating audio: {e}")
69
 
70
 
71