Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,19 +4,26 @@ import base64
|
|
4 |
import io
|
5 |
from huggingface_hub import InferenceClient
|
6 |
from gtts import gTTS
|
7 |
-
from audiorecorder import audiorecorder
|
8 |
import speech_recognition as sr
|
9 |
from pydub import AudioSegment
|
10 |
|
11 |
if "history" not in st.session_state:
|
12 |
st.session_state.history = []
|
13 |
|
14 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
recognizer = sr.Recognizer()
|
16 |
-
audio_recording = sr.Microphone(device_index=
|
17 |
|
18 |
with audio_recording as source:
|
19 |
-
list_available_microphones()
|
20 |
recognizer.adjust_for_ambient_noise(source, gain=None)
|
21 |
audio = recognizer.listen(source, timeout=5, gain=None)
|
22 |
|
@@ -84,37 +91,37 @@ def text_to_speech(text, speed=1.3):
|
|
84 |
modified_audio_fp.seek(0)
|
85 |
return modified_audio_fp
|
86 |
|
87 |
-
def list_available_microphones():
|
88 |
-
p = pyaudio.PyAudio()
|
89 |
-
info = p.get_host_api_info_by_index(0)
|
90 |
-
numdevices = info.get('deviceCount')
|
91 |
-
for i in range(0, numdevices):
|
92 |
-
if (p.get_device_info_by_index(i).get('maxInputChannels')) > 0:
|
93 |
-
print("Input Device id {}: {}".format(i, p.get_device_info_by_index(i).get('name')))
|
94 |
-
p.terminate()
|
95 |
-
|
96 |
def main():
|
97 |
st.title("Chatbot de Voz a Voz")
|
98 |
-
|
99 |
-
|
100 |
-
if
|
101 |
-
st.
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
|
119 |
if __name__ == "__main__":
|
120 |
main()
|
|
|
4 |
import io
|
5 |
from huggingface_hub import InferenceClient
|
6 |
from gtts import gTTS
|
|
|
7 |
import speech_recognition as sr
|
8 |
from pydub import AudioSegment
|
9 |
|
10 |
if "history" not in st.session_state:
|
11 |
st.session_state.history = []
|
12 |
|
13 |
+
def list_available_microphones():
|
14 |
+
p = pyaudio.PyAudio()
|
15 |
+
info = p.get_host_api_info_by_index(0)
|
16 |
+
numdevices = info.get('deviceCount')
|
17 |
+
available_microphones = [i for i in range(numdevices) if p.get_device_info_by_index(i).get('maxInputChannels') > 0]
|
18 |
+
p.terminate()
|
19 |
+
|
20 |
+
return available_microphones
|
21 |
+
|
22 |
+
def recognize_speech(device_index, show_messages=True):
|
23 |
recognizer = sr.Recognizer()
|
24 |
+
audio_recording = sr.Microphone(device_index=device_index, sample_rate=16000, chunk_size=1024)
|
25 |
|
26 |
with audio_recording as source:
|
|
|
27 |
recognizer.adjust_for_ambient_noise(source, gain=None)
|
28 |
audio = recognizer.listen(source, timeout=5, gain=None)
|
29 |
|
|
|
91 |
modified_audio_fp.seek(0)
|
92 |
return modified_audio_fp
|
93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
def main():
|
95 |
st.title("Chatbot de Voz a Voz")
|
96 |
+
available_microphones = list_available_microphones()
|
97 |
+
|
98 |
+
if available_microphones:
|
99 |
+
st.info("Microfonos disponibles: {}".format(available_microphones))
|
100 |
+
device_index = available_microphones[0] # Puedes ajustar esto seg煤n tus necesidades
|
101 |
+
st.info("Micr贸fono seleccionado: {}".format(device_index))
|
102 |
+
|
103 |
+
audio_data = audiorecorder("Habla para grabar", "Deteniendo la grabaci贸n...", device_index=device_index)
|
104 |
+
|
105 |
+
if not audio_data.empty():
|
106 |
+
st.audio(audio_data.export().read(), format="audio/wav")
|
107 |
+
audio_data.export("audio.wav", format="wav")
|
108 |
+
audio_text = recognize_speech(device_index=device_index)
|
109 |
+
|
110 |
+
if audio_text:
|
111 |
+
output, audio_file = generate(audio_text, history=st.session_state.history)
|
112 |
+
|
113 |
+
if audio_text:
|
114 |
+
st.session_state.history.append((audio_text, output))
|
115 |
+
|
116 |
+
if audio_file is not None:
|
117 |
+
st.markdown(
|
118 |
+
f"""
|
119 |
+
<audio autoplay="autoplay" controls="controls" src="data:audio/mp3;base64,{base64.b64encode(audio_file.read()).decode()}" type="audio/mp3" id="audio_player"></audio>
|
120 |
+
""",
|
121 |
+
unsafe_allow_html=True
|
122 |
+
)
|
123 |
+
else:
|
124 |
+
st.warning("No se detectaron micr贸fonos disponibles. Aseg煤rate de que tengas un micr贸fono conectado.")
|
125 |
|
126 |
if __name__ == "__main__":
|
127 |
main()
|