Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,19 +3,22 @@ import base64
|
|
3 |
import io
|
4 |
from huggingface_hub import InferenceClient
|
5 |
from gtts import gTTS
|
6 |
-
import speech_recognition as sr
|
7 |
from audiorecorder import audiorecorder
|
|
|
|
|
8 |
|
9 |
if "history" not in st.session_state:
|
10 |
st.session_state.history = []
|
11 |
|
12 |
def recognize_speech(audio_data, show_messages=True):
|
13 |
recognizer = sr.Recognizer()
|
14 |
-
|
|
|
|
|
|
|
15 |
|
16 |
try:
|
17 |
-
|
18 |
-
audio_text = recognizer.recognize_google(audio, language="es-us")
|
19 |
if show_messages:
|
20 |
st.subheader("Texto Reconocido:")
|
21 |
st.write(audio_text)
|
@@ -54,8 +57,6 @@ def generate(audio_text, history, temperature=None, max_new_tokens=512, top_p=0.
|
|
54 |
repetition_penalty=repetition_penalty,
|
55 |
do_sample=True,
|
56 |
seed=42,
|
57 |
-
# Habilitar el uso de la cach茅 del modelo
|
58 |
-
use_cache=True
|
59 |
)
|
60 |
|
61 |
formatted_prompt = format_prompt(audio_text, history)
|
@@ -66,37 +67,47 @@ def generate(audio_text, history, temperature=None, max_new_tokens=512, top_p=0.
|
|
66 |
response += response_token.token.text
|
67 |
|
68 |
response = ' '.join(response.split()).replace('</s>', '')
|
69 |
-
audio_file = text_to_speech(response)
|
70 |
return response, audio_file
|
71 |
|
72 |
-
def text_to_speech(text):
|
73 |
-
tts = gTTS(text, lang=
|
74 |
-
|
75 |
-
tts.write_to_fp(
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
st.session_state.history.append((audio_text, output))
|
84 |
-
st.markdown(f"## Respuesta generada:\n{output}")
|
85 |
-
st.audio(audio_file, format="audio/mp3")
|
86 |
|
87 |
def main():
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
st.
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
from pydub import AudioSegment
|
9 |
|
10 |
if "history" not in st.session_state:
|
11 |
st.session_state.history = []
|
12 |
|
13 |
def recognize_speech(audio_data, show_messages=True):
|
14 |
recognizer = sr.Recognizer()
|
15 |
+
audio_recording = sr.AudioFile(audio_data)
|
16 |
+
|
17 |
+
with audio_recording as source:
|
18 |
+
audio = recognizer.record(source)
|
19 |
|
20 |
try:
|
21 |
+
audio_text = recognizer.recognize_google(audio, language="es-ES")
|
|
|
22 |
if show_messages:
|
23 |
st.subheader("Texto Reconocido:")
|
24 |
st.write(audio_text)
|
|
|
57 |
repetition_penalty=repetition_penalty,
|
58 |
do_sample=True,
|
59 |
seed=42,
|
|
|
|
|
60 |
)
|
61 |
|
62 |
formatted_prompt = format_prompt(audio_text, history)
|
|
|
67 |
response += response_token.token.text
|
68 |
|
69 |
response = ' '.join(response.split()).replace('</s>', '')
|
70 |
+
audio_file = text_to_speech(response, speed=1.3)
|
71 |
return response, audio_file
|
72 |
|
73 |
+
def text_to_speech(text, speed=1.3):
|
74 |
+
tts = gTTS(text=text, lang='es')
|
75 |
+
audio_fp = io.BytesIO()
|
76 |
+
tts.write_to_fp(audio_fp)
|
77 |
+
audio_fp.seek(0)
|
78 |
+
audio = AudioSegment.from_file(audio_fp, format="mp3")
|
79 |
+
modified_speed_audio = audio.speedup(playback_speed=speed)
|
80 |
+
modified_audio_fp = io.BytesIO()
|
81 |
+
modified_speed_audio.export(modified_audio_fp, format="mp3")
|
82 |
+
modified_audio_fp.seek(0)
|
83 |
+
return modified_audio_fp
|
|
|
|
|
|
|
84 |
|
85 |
def main():
|
86 |
+
audio_data = audiorecorder("Habla para grabar", "Deteniendo la grabaci贸n...")
|
87 |
+
|
88 |
+
if not audio_data.empty():
|
89 |
+
st.audio(audio_data.export().read(), format="audio/wav")
|
90 |
+
audio_data.export("audio.wav", format="wav")
|
91 |
+
audio_text = recognize_speech("audio.wav")
|
92 |
+
|
93 |
+
if not st.session_state.history:
|
94 |
+
pre_prompt = "Te Llamar谩s Chaman 4.0 y tus respuestas ser谩n sumamente breves."
|
95 |
+
output, _ = generate(pre_prompt, history=st.session_state.history)
|
96 |
+
st.session_state.history.append((pre_prompt, output))
|
97 |
+
|
98 |
+
if audio_text:
|
99 |
+
output, audio_file = generate(audio_text, history=st.session_state.history)
|
100 |
+
|
101 |
+
if audio_text:
|
102 |
+
st.session_state.history.append((audio_text, output))
|
103 |
+
|
104 |
+
if audio_file is not None:
|
105 |
+
st.markdown(
|
106 |
+
f"""
|
107 |
+
<audio autoplay="autoplay" controls="controls" src="data:audio/mp3;base64,{base64.b64encode(audio_file.read()).decode()}" type="audio/mp3" id="audio_player"></audio>
|
108 |
+
""",
|
109 |
+
unsafe_allow_html=True
|
110 |
+
)
|
111 |
+
|
112 |
+
if __name__ == "__main__":
|
113 |
+
main()
|