Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -86,33 +86,34 @@ def text_to_speech(text):
|
|
86 |
|
87 |
def detect_voice_activity(audio_data):
|
88 |
audio = pydub.AudioSegment.from_file(io.BytesIO(audio_data))
|
89 |
-
audio = audio.set_channels(1)
|
90 |
signal_energy = np.sum(np.abs(audio.get_array_of_samples()))
|
91 |
-
threshold_energy = 5000
|
92 |
is_voice_active = signal_energy > threshold_energy
|
93 |
|
94 |
return is_voice_active
|
95 |
|
96 |
def def_main():
|
97 |
st.write("Di la palabra XAMAN para empezar o DETENTE para procesar")
|
|
|
98 |
recording = st_mic_recorder(recording_container=st.empty(), auto_recording=True)
|
|
|
99 |
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
def_main()
|
|
|
86 |
|
87 |
def detect_voice_activity(audio_data):
|
88 |
audio = pydub.AudioSegment.from_file(io.BytesIO(audio_data))
|
89 |
+
audio = audio.set_channels(1)
|
90 |
signal_energy = np.sum(np.abs(audio.get_array_of_samples()))
|
91 |
+
threshold_energy = 5000
|
92 |
is_voice_active = signal_energy > threshold_energy
|
93 |
|
94 |
return is_voice_active
|
95 |
|
96 |
def def_main():
|
97 |
st.write("Di la palabra XAMAN para empezar o DETENTE para procesar")
|
98 |
+
|
99 |
recording = st_mic_recorder(recording_container=st.empty(), auto_recording=True)
|
100 |
+
audio_data = None
|
101 |
|
102 |
+
while audio_data is None:
|
103 |
+
if recording:
|
104 |
+
st.write("Listening...")
|
105 |
+
audio_data = base64.b64decode(recording.split(",")[1])
|
106 |
+
audio_file = io.BytesIO(audio_data)
|
107 |
+
st.audio(audio_file, format="audio/wav")
|
108 |
+
is_voice_active = detect_voice_activity(audio_data)
|
109 |
+
|
110 |
+
if is_voice_active:
|
111 |
+
audio_text = recognize_speech(audio_file)
|
112 |
+
if audio_text:
|
113 |
+
output, audio_file = generate(audio_text, history=st.session_state.history)
|
114 |
+
if audio_file is not None:
|
115 |
+
st.markdown(
|
116 |
+
f"""<audio autoplay="autoplay" controls="controls" src="data:audio/mp3;base64,{base64.b64encode(audio_file.read()).decode()}" type="audio/mp3" id="audio_player"></audio>""",
|
117 |
+
unsafe_allow_html=True)
|
118 |
+
else:
|
119 |
+
st.write("Waiting for voice input...")
|
|