shahpalash10 commited on
Commit
837e23c
·
1 Parent(s): 924a44b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -35
app.py CHANGED
@@ -6,42 +6,40 @@ import speech_recognition as sr
6
  # Create a translation pipeline
7
  pipe = pipeline('translation', model='Helsinki-NLP/opus-mt-en-hi')
8
 
9
- # Initialize the SpeechRecognition recognizer
10
- recognizer = sr.Recognizer()
11
-
12
- # Create a Streamlit input element for microphone input
13
- audio_input = st.empty()
14
 
15
  # Check if the microphone input is requested
16
  if st.checkbox("Use Microphone for English Input"):
17
- with audio_input:
 
18
  st.warning("Listening for audio input... Speak in English.")
19
- try:
20
- with sr.Microphone() as source:
21
- audio = recognizer.listen(source)
22
- st.success("Audio input recorded. Translating...")
23
-
24
- # Recognize the English speech
25
- english_text = recognizer.recognize_google(audio, language='en')
26
-
27
- # Translate the English text to Hindi
28
- out = pipe(english_text, src_lang='en', tgt_lang='hi')
29
-
30
- # Extract the translation
31
- translation_text = out[0]['translation_text']
32
- st.text(f"English Input: {english_text}")
33
- st.text(f"Hindi Translation: {translation_text}")
34
-
35
- # Convert the translated text to speech
36
- tts = gTTS(translation_text, lang='hi')
37
- tts.save("translated_audio.mp3")
38
-
39
- # Display the audio player for listening to the speech
40
- st.audio("translated_audio.mp3", format='audio/mp3')
41
-
42
- except sr.WaitTimeoutError:
43
- st.warning("No speech detected. Please speak into the microphone.")
44
- except sr.RequestError as e:
45
- st.error(f"Could not request results from Google Speech Recognition service: {e}")
46
- except sr.UnknownValueError:
47
- st.warning("Speech recognition could not understand the audio.")
 
6
  # Create a translation pipeline
7
  pipe = pipeline('translation', model='Helsinki-NLP/opus-mt-en-hi')
8
 
9
+ # Create a Streamlit input element for text input
10
+ text_input = st.text_area("Enter some English text")
 
 
 
11
 
12
  # Check if the microphone input is requested
13
  if st.checkbox("Use Microphone for English Input"):
14
+ recognizer = sr.Recognizer()
15
+ with sr.Microphone() as source:
16
  st.warning("Listening for audio input... Speak in English.")
17
+ audio = recognizer.listen(source)
18
+ st.success("Audio input recorded. Translating...")
19
+
20
+ # Recognize the English speech using Google Web Speech API
21
+ try:
22
+ english_text = recognizer.recognize_google(audio, language='en')
23
+ text_input = st.text_area("English Input", english_text)
24
+ except sr.WaitTimeoutError:
25
+ st.warning("No speech detected. Please speak into the microphone.")
26
+ except sr.RequestError as e:
27
+ st.error(f"Could not request results from Google Speech Recognition service: {e}")
28
+ except sr.UnknownValueError:
29
+ st.warning("Speech recognition could not understand the audio.")
30
+
31
+ if text_input:
32
+ # Translate the English text to Hindi
33
+ out = pipe(text_input, src_lang='en', tgt_lang='hi')
34
+
35
+ # Extract the translation
36
+ translation_text = out[0]['translation_text']
37
+ st.text(f"English Input: {text_input}")
38
+ st.text(f"Hindi Translation: {translation_text}")
39
+
40
+ # Convert the translated text to speech
41
+ tts = gTTS(translation_text, lang='hi')
42
+ tts.save("translated_audio.mp3")
43
+
44
+ # Display the audio player for listening to the speech
45
+ st.audio("translated_audio.mp3", format='audio/mp3')