shahpalash10 commited on
Commit
3f918a6
1 Parent(s): d24a73d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -33
app.py CHANGED
@@ -2,44 +2,51 @@ import streamlit as st
2
  from transformers import pipeline
3
  from gtts import gTTS
4
  import speech_recognition as sr
 
5
 
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')
 
 
 
 
 
2
  from transformers import pipeline
3
  from gtts import gTTS
4
  import speech_recognition as sr
5
+ import sounddevice as sd # Import sounddevice
6
 
7
  # Create a translation pipeline
8
  pipe = pipeline('translation', model='Helsinki-NLP/opus-mt-en-hi')
9
 
10
+ # Initialize the SpeechRecognition recognizer
11
+ recognizer = sr.Recognizer()
12
+
13
+ # Create a Streamlit input element for microphone input
14
+ audio_input = st.empty()
15
 
16
  # Check if the microphone input is requested
17
  if st.checkbox("Use Microphone for English Input"):
18
+ with audio_input:
 
19
  st.warning("Listening for audio input... Speak in English.")
20
+ try:
21
+ # Replace pyaudio.Microphone with sd.InputStream
22
+ with sd.InputStream(callback=None, channels=1, dtype='int16', samplerate=16000):
23
+ with sr.AudioFile("temp.wav") as source: # Save audio to temp.wav
24
+ recognizer.adjust_for_ambient_noise(source)
25
+ audio = recognizer.listen(source)
26
+
27
+ st.success("Audio input recorded. Translating...")
28
+
29
+ # Recognize the English speech
30
+ english_text = recognizer.recognize_google(audio, language='en')
31
+
32
+ # Translate the English text to Hindi
33
+ out = pipe(english_text, src_lang='en', tgt_lang='hi')
34
+
35
+ # Extract the translation
36
+ translation_text = out[0]['translation_text']
37
+ st.text(f"English Input: {english_text}")
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')
46
+
47
+ except sr.WaitTimeoutError:
48
+ st.warning("No speech detected. Please speak into the microphone.")
49
+ except sr.RequestError as e:
50
+ st.error(f"Could not request results from Google Speech Recognition service: {e}")
51
+ except sr.UnknownValueError:
52
+ st.warning("Speech recognition could not understand the audio.")