Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,18 +6,16 @@ from huggingface_hub import InferenceClient
|
|
6 |
from gtts import gTTS
|
7 |
import speech_recognition as sr
|
8 |
from pydub import AudioSegment
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
p.terminate()
|
19 |
-
|
20 |
-
return available_microphones
|
21 |
|
22 |
def recognize_speech(device_index, show_messages=True):
|
23 |
recognizer = sr.Recognizer()
|
@@ -93,12 +91,12 @@ def text_to_speech(text, speed=1.3):
|
|
93 |
|
94 |
def main():
|
95 |
st.title("Chatbot de Voz a Voz")
|
96 |
-
available_microphones =
|
97 |
|
98 |
-
if available_microphones:
|
99 |
-
st.info("
|
100 |
-
selected_microphone = st.selectbox("Selecciona un micr贸fono", available_microphones)
|
101 |
-
st.info("
|
102 |
recognizer = sr.Recognizer()
|
103 |
audio_recording = sr.Microphone(device_index=selected_microphone, sample_rate=16000, chunk_size=1024)
|
104 |
|
@@ -111,22 +109,21 @@ def main():
|
|
111 |
|
112 |
if audio_data:
|
113 |
st.audio(audio_data.frame_data, format="audio/wav")
|
114 |
-
|
115 |
-
if
|
116 |
-
|
117 |
-
|
118 |
if audio_text:
|
119 |
-
st.session_state.history
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
else:
|
129 |
st.warning("No se detectaron micr贸fonos disponibles. Aseg煤rate de que tengas un micr贸fono conectado.")
|
130 |
|
131 |
if __name__ == "__main__":
|
132 |
-
main()
|
|
|
6 |
from gtts import gTTS
|
7 |
import speech_recognition as sr
|
8 |
from pydub import AudioSegment
|
9 |
+
import webrtcvad
|
10 |
|
11 |
+
def perform_vad(audio_data):
|
12 |
+
vad = webrtcvad.Vad()
|
13 |
+
vad.set_mode(1)
|
14 |
+
|
15 |
+
samples = audio_data.frame_data
|
16 |
+
is_speech = vad.is_speech(samples, sample_rate=audio_data.sample_rate)
|
17 |
+
|
18 |
+
return is_speech
|
|
|
|
|
|
|
19 |
|
20 |
def recognize_speech(device_index, show_messages=True):
|
21 |
recognizer = sr.Recognizer()
|
|
|
91 |
|
92 |
def main():
|
93 |
st.title("Chatbot de Voz a Voz")
|
94 |
+
available_microphones = pyaudio.PyAudio().get_device_count()
|
95 |
|
96 |
+
if available_microphones > 0:
|
97 |
+
st.info(f"N煤mero de micr贸fonos disponibles: {available_microphones}")
|
98 |
+
selected_microphone = st.selectbox("Selecciona un micr贸fono", list(range(available_microphones)))
|
99 |
+
st.info(f"N煤mero de micr贸fono seleccionado: {selected_microphone}")
|
100 |
recognizer = sr.Recognizer()
|
101 |
audio_recording = sr.Microphone(device_index=selected_microphone, sample_rate=16000, chunk_size=1024)
|
102 |
|
|
|
109 |
|
110 |
if audio_data:
|
111 |
st.audio(audio_data.frame_data, format="audio/wav")
|
112 |
+
|
113 |
+
if perform_vad(audio_data):
|
114 |
+
audio_text = recognize_speech(device_index=selected_microphone)
|
|
|
115 |
if audio_text:
|
116 |
+
output, audio_file = generate(audio_text, history=st.session_state.history)
|
117 |
+
|
118 |
+
if audio_file is not None:
|
119 |
+
st.markdown(
|
120 |
+
f"""
|
121 |
+
<audio autoplay="autoplay" controls="controls" src="data:audio/mp3;base64,{base64.b64encode(audio_file.read()).decode()}" type="audio/mp3" id="audio_player"></audio>
|
122 |
+
""",
|
123 |
+
unsafe_allow_html=True
|
124 |
+
)
|
125 |
else:
|
126 |
st.warning("No se detectaron micr贸fonos disponibles. Aseg煤rate de que tengas un micr贸fono conectado.")
|
127 |
|
128 |
if __name__ == "__main__":
|
129 |
+
main()
|