salomonsky commited on
Commit
94a8e3e
·
verified ·
1 Parent(s): 6cad3ff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -21
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) # 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:
101
- st.write("Listening...")
102
- audio_data = base64.b64decode(recording.split(",")[1])
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()
 
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...")