salomonsky commited on
Commit
b39afbd
verified
1 Parent(s): 8438d4f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -20
app.py CHANGED
@@ -15,6 +15,7 @@ def recognize_speech(audio_data, show_messages=True):
15
  audio_recording = sr.AudioFile(audio_data)
16
 
17
  with audio_recording as source:
 
18
  audio = recognizer.record(source)
19
 
20
  try:
@@ -24,7 +25,7 @@ def recognize_speech(audio_data, show_messages=True):
24
  st.write(audio_text)
25
  st.success("Reconocimiento de voz completado.")
26
  except sr.UnknownValueError:
27
- st.warning("No se pudo reconocer el audio.")
28
  audio_text = ""
29
  except sr.RequestError:
30
  st.error("Hablame para comenzar!")
@@ -82,25 +83,17 @@ def text_to_speech(text, speed=1.3):
82
  modified_audio_fp.seek(0)
83
  return modified_audio_fp
84
 
85
- def main():
86
- st.title("Chatbot de Voz a Voz")
87
- microphones = sr.Microphone.list_microphone_names()
88
- if microphones:
89
- selected_microphone_index = 0
90
- audio_data = sr.Microphone(device_index=selected_microphone_index)
91
- else:
92
- st.warning("No se encontraron dispositivos.")
93
-
94
- if not audio_data.empty():
95
- st.audio(audio_data.export().read(), format="audio/wav")
96
- audio_data.export("audio.wav", format="wav")
97
- audio_text = recognize_speech("audio.wav")
98
-
99
- if audio_text:
100
- output, audio_file = generate(audio_text, history=st.session_state.history)
101
 
102
- if audio_text:
103
- st.session_state.history.append((audio_text, output))
104
 
105
  if audio_file is not None:
106
  st.markdown(
@@ -110,5 +103,23 @@ def main():
110
  unsafe_allow_html=True
111
  )
112
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
  if __name__ == "__main__":
114
- main()
 
15
  audio_recording = sr.AudioFile(audio_data)
16
 
17
  with audio_recording as source:
18
+ recognizer.adjust_for_ambient_noise(source) # Ajuste para el ruido ambiente
19
  audio = recognizer.record(source)
20
 
21
  try:
 
25
  st.write(audio_text)
26
  st.success("Reconocimiento de voz completado.")
27
  except sr.UnknownValueError:
28
+ st.warning("No se pudo reconocer el audio. 驴Intentaste grabar algo?")
29
  audio_text = ""
30
  except sr.RequestError:
31
  st.error("Hablame para comenzar!")
 
83
  modified_audio_fp.seek(0)
84
  return modified_audio_fp
85
 
86
+ def background_listener(recognizer, source):
87
+ while True:
88
+ try:
89
+ audio_text = recognizer.recognize_google(recognizer.listen(source), language="es-ES")
90
+ st.subheader("Texto Reconocido:")
91
+ st.write(audio_text)
92
+ st.success("Reconocimiento de voz completado.")
93
+ output, audio_file = generate(audio_text, history=st.session_state.history)
 
 
 
 
 
 
 
 
94
 
95
+ if audio_text:
96
+ st.session_state.history.append((audio_text, output))
97
 
98
  if audio_file is not None:
99
  st.markdown(
 
103
  unsafe_allow_html=True
104
  )
105
 
106
+ except sr.UnknownValueError:
107
+ pass # Ignore if nothing is recognized
108
+ except sr.RequestError:
109
+ st.error("Error de solicitud. Aseg煤rate de tener una conexi贸n a Internet.")
110
+
111
+ def main():
112
+ st.title("Chatbot de Voz a Voz")
113
+ recognizer = sr.Recognizer()
114
+ microphone = sr.Microphone()
115
+
116
+ with microphone as source:
117
+ recognizer.adjust_for_ambient_noise(source) # Ajuste inicial para el ruido ambiente
118
+
119
+ st.warning("Escuchando en segundo plano. Puedes hablar en cualquier momento.")
120
+ stop_listening = recognizer.listen_in_background(microphone, background_listener)
121
+
122
+ st.button("Detener Escucha", on_click=stop_listening)
123
+
124
  if __name__ == "__main__":
125
+ main()