salomonsky commited on
Commit
ac7712e
·
verified ·
1 Parent(s): c6524ed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -4
app.py CHANGED
@@ -4,7 +4,16 @@ import io
4
  from huggingface_hub import InferenceClient
5
  from gtts import gTTS
6
  import speech_recognition as sr
7
- from streamlit-audiorecorder import st_audiorecorder
 
 
 
 
 
 
 
 
 
8
 
9
  def recognize_speech(audio_data, show_messages=True):
10
  recognizer = sr.Recognizer()
@@ -67,10 +76,17 @@ def main():
67
  if "history" not in st.session_state:
68
  st.session_state.history = []
69
 
70
- audio_bytes = st_audiorecorder()
 
 
 
 
 
 
71
 
72
- if audio_bytes is not None:
73
- st.audio(audio_bytes, format="audio/wav")
 
74
 
75
  if not st.session_state.history:
76
  pre_prompt = "Te Llamarás Chaman 4.0 y tus respuestas serán sumamente breves."
 
4
  from huggingface_hub import InferenceClient
5
  from gtts import gTTS
6
  import speech_recognition as sr
7
+ from streamlit_webrtc import webrtc_streamer, AudioTransformerBase
8
+
9
+ class AudioRecorder(AudioTransformerBase):
10
+ def __init__(self, *args, **kwargs):
11
+ super().__init__(*args, **kwargs)
12
+ self.audio_data = io.BytesIO()
13
+
14
+ def transform(self, audio_data):
15
+ self.audio_data.write(audio_data)
16
+ return audio_data
17
 
18
  def recognize_speech(audio_data, show_messages=True):
19
  recognizer = sr.Recognizer()
 
76
  if "history" not in st.session_state:
77
  st.session_state.history = []
78
 
79
+ audio_recorder = AudioRecorder()
80
+
81
+ webrtc_ctx = webrtc_streamer(
82
+ key="audio-recorder",
83
+ audio_transformer_factory=audio_recorder,
84
+ async_transform=True,
85
+ )
86
 
87
+ if webrtc_ctx.audio_processor:
88
+ audio_data = audio_recorder.audio_data.getvalue()
89
+ audio_text = recognize_speech(audio_data)
90
 
91
  if not st.session_state.history:
92
  pre_prompt = "Te Llamarás Chaman 4.0 y tus respuestas serán sumamente breves."