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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -46
app.py CHANGED
@@ -4,29 +4,27 @@ import io
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()
20
- with sr.AudioData(audio_data, 16000, 2):
21
- audio_text = recognizer.recognize_google(audio_data, language="es-ES")
22
-
23
- if show_messages:
24
- st.subheader("Texto Reconocido:")
25
- st.write(audio_text)
26
- st.success("Reconocimiento de voz completado.")
27
-
28
- return audio_text
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  def format_prompt(message, history):
31
  prompt = "<s>"
32
 
@@ -76,36 +74,27 @@ def main():
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."
93
- output, _ = generate(pre_prompt, history=st.session_state.history)
94
- st.session_state.history.append((pre_prompt, output))
95
-
96
  if audio_text:
97
- output, audio_file = generate(audio_text, history=st.session_state.history)
98
-
99
- if audio_text:
100
- st.session_state.history.append((audio_text, output))
101
-
102
- if audio_file is not None:
103
- st.markdown(
104
- f"""
105
- <audio autoplay="autoplay" controls="controls" src="data:audio/mp3;base64,{base64.b64encode(audio_file.read()).decode()}" type="audio/mp3" id="audio_player"></audio>
106
- """,
107
- unsafe_allow_html=True
108
- )
109
 
110
  if __name__ == "__main__":
111
  main()
 
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 capture_audio():
 
 
 
 
 
 
 
 
 
10
  recognizer = sr.Recognizer()
 
 
 
 
 
 
 
 
 
11
 
12
+ with sr.Microphone() as source:
13
+ print("Di algo...")
14
+ recognizer.adjust_for_ambient_noise(source)
15
+ audio = recognizer.listen(source, timeout=5)
16
+
17
+ try:
18
+ text = recognizer.recognize_google(audio, language="es-ES")
19
+ print("Has dicho: " + text)
20
+ return audio, text
21
+ except sr.UnknownValueError:
22
+ print("No se pudo entender el audio")
23
+ except sr.RequestError as e:
24
+ print("Error al solicitar los resultados: {0}".format(e))
25
+
26
+ return None, None
27
+
28
  def format_prompt(message, history):
29
  prompt = "<s>"
30
 
 
74
  if "history" not in st.session_state:
75
  st.session_state.history = []
76
 
77
+ st.beta_container()
78
+ audio_data, audio_text = capture_audio()
79
 
80
+ if not st.session_state.history:
81
+ pre_prompt = "Te Llamarás Chaman 4.0 y tus respuestas serán sumamente breves."
82
+ output, _ = generate(pre_prompt, history=st.session_state.history)
83
+ st.session_state.history.append((pre_prompt, output))
 
84
 
85
+ if audio_text:
86
+ output, audio_file = generate(audio_text, history=st.session_state.history)
 
87
 
 
 
 
 
 
88
  if audio_text:
89
+ st.session_state.history.append((audio_text, output))
90
+
91
+ if audio_file is not None:
92
+ st.markdown(
93
+ f"""
94
+ <audio autoplay="autoplay" controls="controls" src="data:audio/mp3;base64,{base64.b64encode(audio_file.read()).decode()}" type="audio/mp3" id="audio_player"></audio>
95
+ """,
96
+ unsafe_allow_html=True
97
+ )
 
 
 
98
 
99
  if __name__ == "__main__":
100
  main()