Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,27 +3,31 @@ import base64
|
|
3 |
import io
|
4 |
from huggingface_hub import InferenceClient
|
5 |
from gtts import gTTS
|
|
|
6 |
import speech_recognition as sr
|
7 |
|
8 |
-
def
|
9 |
recognizer = sr.Recognizer()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
-
with sr.Microphone() as source:
|
12 |
-
st.write("Di algo...")
|
13 |
-
recognizer.adjust_for_ambient_noise(source)
|
14 |
-
audio = recognizer.listen(source, timeout=5)
|
15 |
-
|
16 |
-
try:
|
17 |
-
text = recognizer.recognize_google(audio, language="es-ES")
|
18 |
-
st.write("Has dicho: " + text)
|
19 |
-
return audio, text
|
20 |
-
except sr.UnknownValueError:
|
21 |
-
st.write("No se pudo entender el audio")
|
22 |
-
except sr.RequestError as e:
|
23 |
-
st.write("Error al solicitar los resultados: {0}".format(e))
|
24 |
-
|
25 |
-
return None, None
|
26 |
-
|
27 |
def format_prompt(message, history):
|
28 |
prompt = "<s>"
|
29 |
|
@@ -73,27 +77,31 @@ def main():
|
|
73 |
if "history" not in st.session_state:
|
74 |
st.session_state.history = []
|
75 |
|
76 |
-
|
77 |
-
audio_data, audio_text = capture_audio()
|
78 |
-
|
79 |
-
if not st.session_state.history:
|
80 |
-
pre_prompt = "Te Llamar谩s Chaman 4.0 y tus respuestas ser谩n sumamente breves."
|
81 |
-
output, _ = generate(pre_prompt, history=st.session_state.history)
|
82 |
-
st.session_state.history.append((pre_prompt, output))
|
83 |
|
84 |
-
if
|
85 |
-
|
|
|
|
|
86 |
|
|
|
|
|
|
|
|
|
|
|
87 |
if audio_text:
|
88 |
-
st.session_state.history
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
|
|
|
|
|
|
97 |
|
98 |
if __name__ == "__main__":
|
99 |
main()
|
|
|
3 |
import io
|
4 |
from huggingface_hub import InferenceClient
|
5 |
from gtts import gTTS
|
6 |
+
from audiorecorder import audiorecorder
|
7 |
import speech_recognition as sr
|
8 |
|
9 |
+
def recognize_speech(audio_data, show_messages=True):
|
10 |
recognizer = sr.Recognizer()
|
11 |
+
audio_recording = sr.AudioFile(audio_data)
|
12 |
+
|
13 |
+
with audio_recording as source:
|
14 |
+
audio = recognizer.record(source)
|
15 |
+
|
16 |
+
try:
|
17 |
+
audio_text = recognizer.recognize_google(audio, language="es-ES")
|
18 |
+
if show_messages:
|
19 |
+
st.subheader("Texto Reconocido:")
|
20 |
+
st.write(audio_text)
|
21 |
+
st.success("Reconocimiento de voz completado.")
|
22 |
+
except sr.UnknownValueError:
|
23 |
+
st.warning("No se pudo reconocer el audio. 驴Intentaste grabar algo?")
|
24 |
+
audio_text = ""
|
25 |
+
except sr.RequestError:
|
26 |
+
st.error("No he recibido ningun audio. Por favor, int茅ntalo de nuevo.")
|
27 |
+
audio_text = ""
|
28 |
+
|
29 |
+
return audio_text
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
def format_prompt(message, history):
|
32 |
prompt = "<s>"
|
33 |
|
|
|
77 |
if "history" not in st.session_state:
|
78 |
st.session_state.history = []
|
79 |
|
80 |
+
audio_data = audiorecorder("Habla para grabar", "Deteniendo la grabaci贸n...")
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
|
82 |
+
if not audio_data.empty():
|
83 |
+
st.audio(audio_data.export().read(), format="audio/wav")
|
84 |
+
audio_data.export("audio.wav", format="wav")
|
85 |
+
audio_text = recognize_speech("audio.wav")
|
86 |
|
87 |
+
if not st.session_state.history:
|
88 |
+
pre_prompt = "Te Llamar谩s Chaman 4.0 y tus respuestas ser谩n sumamente breves."
|
89 |
+
output, _ = generate(pre_prompt, history=st.session_state.history)
|
90 |
+
st.session_state.history.append((pre_prompt, output))
|
91 |
+
|
92 |
if audio_text:
|
93 |
+
output, audio_file = generate(audio_text, history=st.session_state.history)
|
94 |
+
|
95 |
+
if audio_text:
|
96 |
+
st.session_state.history.append((audio_text, output))
|
97 |
+
|
98 |
+
if audio_file is not None:
|
99 |
+
st.markdown(
|
100 |
+
f"""
|
101 |
+
<audio autoplay="autoplay" controls="controls" src="data:audio/mp3;base64,{base64.b64encode(audio_file.read()).decode()}" type="audio/mp3" id="audio_player"></audio>
|
102 |
+
""",
|
103 |
+
unsafe_allow_html=True
|
104 |
+
)
|
105 |
|
106 |
if __name__ == "__main__":
|
107 |
main()
|