shahpalash10 commited on
Commit
c505931
1 Parent(s): bfb387e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -10
app.py CHANGED
@@ -2,31 +2,44 @@ import streamlit as st
2
  from transformers import pipeline
3
  from gtts import gTTS
4
  import speech_recognition as sr
 
5
 
6
-
7
  pipe = pipeline('translation', model='Helsinki-NLP/opus-mt-en-hi')
8
 
9
-
10
  recognizer = sr.Recognizer()
11
 
12
-
13
  audio_input = st.empty()
14
 
15
  if st.checkbox("Use Microphone for English Input"):
16
  with audio_input:
17
  st.warning("Listening for audio input... Speak in English.")
18
  try:
19
- with sr.Microphone() as source:
20
- audio = recognizer.listen(source)
 
 
 
 
 
 
 
 
 
 
 
 
21
  st.success("Audio input recorded. Translating...")
22
 
23
-
24
- english_text = recognizer.recognize_google(audio, language='en')
25
 
26
-
27
  out = pipe(english_text, src_lang='en', tgt_lang='hi')
28
 
29
-
30
  translation_text = out[0]['translation_text']
31
  st.text(f"English Input: {english_text}")
32
  st.text(f"Hindi Translation: {translation_text}")
@@ -34,7 +47,6 @@ if st.checkbox("Use Microphone for English Input"):
34
  tts = gTTS(translation_text, lang='hi')
35
  tts.save("translated_audio.mp3")
36
 
37
-
38
  st.audio("translated_audio.mp3", format='audio/mp3')
39
 
40
  except sr.WaitTimeoutError:
 
2
  from transformers import pipeline
3
  from gtts import gTTS
4
  import speech_recognition as sr
5
+ import pyaudio
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
  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
+ # Create a PyAudio object
21
+ pa = pyaudio.PyAudio()
22
+
23
+ # Use PyAudio for microphone input
24
+ with pa.open(format=pyaudio.paInt16,
25
+ channels=1,
26
+ rate=44100,
27
+ input=True,
28
+ frames_per_buffer=1024) as stream:
29
+ audio_data = stream.read(44100) # Adjust the number of frames as needed
30
+
31
+ # Close the PyAudio object
32
+ pa.terminate()
33
+
34
  st.success("Audio input recorded. Translating...")
35
 
36
+ # Recognize the English speech
37
+ english_text = recognizer.recognize_google(audio_data, language='en')
38
 
39
+ # Translate the English text to Hindi
40
  out = pipe(english_text, src_lang='en', tgt_lang='hi')
41
 
42
+ # Extract the translation
43
  translation_text = out[0]['translation_text']
44
  st.text(f"English Input: {english_text}")
45
  st.text(f"Hindi Translation: {translation_text}")
 
47
  tts = gTTS(translation_text, lang='hi')
48
  tts.save("translated_audio.mp3")
49
 
 
50
  st.audio("translated_audio.mp3", format='audio/mp3')
51
 
52
  except sr.WaitTimeoutError: