salomonsky commited on
Commit
d843813
verified
1 Parent(s): b0b5cd6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -55
app.py CHANGED
@@ -1,11 +1,7 @@
1
  import streamlit as st
2
- from huggingface_hub import InferenceClient
3
- from gtts import gTTS
4
- import base64
5
  import speech_recognition as sr
6
  from pydub import AudioSegment
7
  from pydub.playback import play
8
- import pyaudio
9
  from io import BytesIO
10
  from time import sleep
11
 
@@ -62,7 +58,6 @@ def generate_with_progress(
62
  response += response_token.token.text
63
  total_tokens += 1
64
 
65
- # Actualizar la barra de progreso
66
  st.subheader("Generando respuesta...")
67
  st.progress(total_tokens / max_new_tokens)
68
 
@@ -70,56 +65,37 @@ def generate_with_progress(
70
 
71
  return response
72
 
73
- # Configuraci贸n para la entrada de voz
74
- with st.form("voice_input_form"):
75
- st.write("Haz clic en el bot贸n para iniciar la grabaci贸n de voz:")
76
- start_recording_button = st.form_submit_button("Iniciar Grabaci贸n")
77
-
78
- # Inicializar historial si no existe
79
  if "history" not in st.session_state:
80
  st.session_state.history = []
81
 
82
- # Verificar si se hace clic en el bot贸n de grabaci贸n
83
- if start_recording_button:
84
- st.info("Habla ahora...")
85
-
86
- audio_data = BytesIO()
87
- p = pyaudio.PyAudio()
88
- stream = p.open(format=pyaudio.paInt16, channels=1, rate=44100, input=True, frames_per_buffer=1024)
89
-
90
- with st.spinner("Grabando..."):
91
- frames = []
92
- for i in range(int(44100 / 1024 * 5)): # grabar durante 5 segundos
93
- data = stream.read(1024)
94
- frames.append(data)
95
-
96
- stream.stop_stream()
97
- stream.close()
98
- p.terminate()
99
-
100
- recognizer = sr.Recognizer()
101
- try:
102
- audio_data.write(b''.join(frames))
103
- audio_data.seek(0)
104
- audio = AudioSegment.from_file(audio_data, format="wav")
105
- text = recognizer.recognize_google(audio, language="es-ES")
106
- st.success(f"Texto reconocido: {text}")
107
-
108
- generate_progress = st.empty()
109
- generate_progress.progress(0.0)
110
- output = generate_with_progress(text, history=st.session_state.history)
111
- st.session_state.history.append((text, output))
112
- st.success("Respuesta generada con 茅xito.")
113
-
114
- st.subheader("Reproduciendo respuesta...")
115
- audio_file_path = text_to_speech(output)
116
- play(audio_file_path)
117
-
118
- for progress_value in range(0, 101, 10):
119
- st.progress(progress_value / 100)
120
- sleep(0.5)
121
-
122
- except sr.UnknownValueError:
123
- st.warning("No se pudo reconocer el habla.")
124
- except sr.RequestError as e:
125
- st.error(f"Error en la solicitud al servicio de reconocimiento de voz: {e}")
 
1
  import streamlit as st
 
 
 
2
  import speech_recognition as sr
3
  from pydub import AudioSegment
4
  from pydub.playback import play
 
5
  from io import BytesIO
6
  from time import sleep
7
 
 
58
  response += response_token.token.text
59
  total_tokens += 1
60
 
 
61
  st.subheader("Generando respuesta...")
62
  st.progress(total_tokens / max_new_tokens)
63
 
 
65
 
66
  return response
67
 
 
 
 
 
 
 
68
  if "history" not in st.session_state:
69
  st.session_state.history = []
70
 
71
+ recognizer = sr.Recognizer()
72
+
73
+ while True:
74
+ with st.spinner("Escuchando..."):
75
+ try:
76
+ with sr.Microphone() as source:
77
+ audio_data = recognizer.listen(source, timeout=5)
78
+ st.success("Audio capturado con 茅xito.")
79
+
80
+ text = recognizer.recognize_google(audio_data, language="es-ES")
81
+ st.success(f"Texto reconocido: {text}")
82
+
83
+ st.subheader("Generando respuesta...")
84
+ st.progress(0.0)
85
+ output = generate_with_progress(text, history=st.session_state.history)
86
+ st.session_state.history.append((text, output))
87
+ st.success("Respuesta generada con 茅xito.")
88
+
89
+ st.subheader("Reproduciendo respuesta...")
90
+ audio_file_path = text_to_speech(output)
91
+ play(audio_file_path)
92
+
93
+ for progress_value in range(0, 101, 10):
94
+ st.progress(progress_value / 100)
95
+ sleep(0.5)
96
+
97
+ except sr.UnknownValueError:
98
+ st.warning("No se pudo reconocer el habla.")
99
+ except sr.RequestError as e:
100
+ st.error(f"Error en la solicitud al servicio de reconocimiento de voz: {e}")
101
+ break