salomonsky commited on
Commit
6cad3ff
·
verified ·
1 Parent(s): 27ba7ab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -9
app.py CHANGED
@@ -84,9 +84,17 @@ def text_to_speech(text):
84
  audio_fp.seek(0)
85
  return audio_fp
86
 
 
 
 
 
 
 
 
 
 
87
  def def_main():
88
  st.write("Di la palabra XAMAN para empezar o DETENTE para procesar")
89
-
90
  recording = st_mic_recorder(recording_container=st.empty(), auto_recording=True)
91
 
92
  if recording:
@@ -95,13 +103,16 @@ def def_main():
95
  audio_file = io.BytesIO(audio_data)
96
  st.audio(audio_file, format="audio/wav")
97
 
98
- audio_text = recognize_speech(audio_file)
99
- if audio_text:
100
- output, audio_file = generate(audio_text, history=st.session_state.history)
101
- if audio_file is not None:
102
- st.markdown(
103
- f"""<audio autoplay="autoplay" controls="controls" src="data:audio/mp3;base64,{base64.b64encode(audio_file.read()).decode()}" type="audio/mp3" id="audio_player"></audio>""",
104
- unsafe_allow_html=True)
 
 
 
105
 
106
  if __name__ == "__main__":
107
- def_main()
 
84
  audio_fp.seek(0)
85
  return audio_fp
86
 
87
+ def detect_voice_activity(audio_data):
88
+ audio = pydub.AudioSegment.from_file(io.BytesIO(audio_data))
89
+ audio = audio.set_channels(1) # Convertir a mono
90
+ signal_energy = np.sum(np.abs(audio.get_array_of_samples()))
91
+ threshold_energy = 5000 # Ajustar según sea necesario
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
  if recording:
 
103
  audio_file = io.BytesIO(audio_data)
104
  st.audio(audio_file, format="audio/wav")
105
 
106
+ is_voice_active = detect_voice_activity(audio_data)
107
+
108
+ if is_voice_active:
109
+ audio_text = recognize_speech(audio_file)
110
+ if audio_text:
111
+ output, audio_file = generate(audio_text, history=st.session_state.history)
112
+ if audio_file is not None:
113
+ st.markdown(
114
+ f"""<audio autoplay="autoplay" controls="controls" src="data:audio/mp3;base64,{base64.b64encode(audio_file.read()).decode()}" type="audio/mp3" id="audio_player"></audio>""",
115
+ unsafe_allow_html=True)
116
 
117
  if __name__ == "__main__":
118
+ def_main()