mishiawan commited on
Commit
e977341
Β·
verified Β·
1 Parent(s): e6258ad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -16
app.py CHANGED
@@ -2,11 +2,8 @@
2
  import streamlit as st
3
  import speech_recognition as sr
4
  from transformers import pipeline
5
- import pyttsx3
6
-
7
- # Initialize the text-to-speech engine
8
- tts_engine = pyttsx3.init()
9
- tts_engine.setProperty('rate', 150) # Set the speech rate for TTS
10
 
11
  # Function to transcribe audio to text
12
  def transcribe_audio(audio_file, input_language):
@@ -20,30 +17,72 @@ def transcribe_audio(audio_file, input_language):
20
  return f"Error: {str(e)}"
21
 
22
  # Function to translate text
23
- def translate_text(text, input_language, output_language):
24
  try:
25
- model_name = f"Helsinki-NLP/opus-mt-{input_language}-{output_language}"
26
- translator = pipeline("translation", model=model_name)
27
  translated = translator(text)[0]["translation_text"]
28
  return translated
29
  except Exception as e:
30
  return f"Error: {str(e)}"
31
 
32
- # Function to convert text to speech
33
- def speak_text(text):
34
  try:
35
- tts_engine.say(text)
36
- tts_engine.runAndWait()
 
37
  except Exception as e:
38
- st.error(f"Error in text-to-speech: {str(e)}")
39
 
40
- # Streamlit app UI
41
  st.title("Real-Time Voice-to-Voice Translator 🌍🎀")
42
  st.markdown("""
43
  This app translates spoken input between multiple languages in real time.
44
  1. Select input and output languages.
45
- 2. Upload an audio file for translation.
46
  3. Listen to the translated speech.
47
  """)
48
 
49
- #
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  import streamlit as st
3
  import speech_recognition as sr
4
  from transformers import pipeline
5
+ from gtts import gTTS
6
+ import os
 
 
 
7
 
8
  # Function to transcribe audio to text
9
  def transcribe_audio(audio_file, input_language):
 
17
  return f"Error: {str(e)}"
18
 
19
  # Function to translate text
20
+ def translate_text(text, target_language):
21
  try:
22
+ translator = pipeline("translation", model=f"Helsinki-NLP/opus-mt-en-{target_language}")
 
23
  translated = translator(text)[0]["translation_text"]
24
  return translated
25
  except Exception as e:
26
  return f"Error: {str(e)}"
27
 
28
+ # Function to generate and play speech
29
+ def speak_text(text, language):
30
  try:
31
+ tts = gTTS(text=text, lang=language)
32
+ tts.save("translated_audio.mp3")
33
+ return "translated_audio.mp3"
34
  except Exception as e:
35
+ return None
36
 
37
+ # Streamlit app
38
  st.title("Real-Time Voice-to-Voice Translator 🌍🎀")
39
  st.markdown("""
40
  This app translates spoken input between multiple languages in real time.
41
  1. Select input and output languages.
42
+ 2. Upload your audio file or record audio for translation.
43
  3. Listen to the translated speech.
44
  """)
45
 
46
+ # Language options
47
+ languages = {
48
+ "English": "en",
49
+ "Spanish": "es",
50
+ "French": "fr",
51
+ "German": "de",
52
+ "Chinese": "zh",
53
+ "Japanese": "ja",
54
+ "Korean": "ko",
55
+ "Hindi": "hi",
56
+ }
57
+
58
+ # Select input and output languages
59
+ input_lang = st.selectbox("Select input language", options=languages.keys())
60
+ output_lang = st.selectbox("Select output language", options=languages.keys())
61
+
62
+ # Upload audio file
63
+ uploaded_audio = st.file_uploader("Upload an audio file", type=["wav", "mp3", "ogg"])
64
+
65
+ if uploaded_audio:
66
+ # Process the uploaded audio
67
+ st.audio(uploaded_audio, format="audio/wav")
68
+ st.write("Transcribing audio...")
69
+ input_language_code = languages[input_lang]
70
+ transcribed_text = transcribe_audio(uploaded_audio, input_language_code)
71
+ st.write(f"Transcribed Text: {transcribed_text}")
72
+
73
+ # Translate text
74
+ if transcribed_text and not transcribed_text.startswith("Error"):
75
+ st.write("Translating text...")
76
+ output_language_code = languages[output_lang]
77
+ translated_text = translate_text(transcribed_text, output_language_code)
78
+ st.write(f"Translated Text: {translated_text}")
79
+
80
+ # Generate speech for the translated text
81
+ st.write("Generating translated audio...")
82
+ audio_file = speak_text(translated_text, output_language_code)
83
+ if audio_file:
84
+ st.audio(audio_file, format="audio/mp3")
85
+ else:
86
+ st.error("Error in generating translated speech.")
87
+ else:
88
+ st.error("Error in transcription.")