Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,109 +1,56 @@
|
|
1 |
-
import
|
2 |
-
import base64
|
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 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
max_new_tokens=max_new_tokens,
|
64 |
-
top_p=top_p,
|
65 |
-
repetition_penalty=repetition_penalty,
|
66 |
-
do_sample=True,
|
67 |
-
seed=42)
|
68 |
-
|
69 |
-
formatted_prompt = format_prompt(audio_text, history)
|
70 |
-
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=True)
|
71 |
-
response = ""
|
72 |
-
|
73 |
-
for response_token in stream:
|
74 |
-
response += response_token.token.text
|
75 |
-
|
76 |
-
response = ' '.join(response.split()).replace('</s>', '')
|
77 |
-
audio_file = text_to_speech(response, speed=1.3)
|
78 |
-
return response, audio_file
|
79 |
-
|
80 |
-
def text_to_speech(text, speed=1.3):
|
81 |
-
tts = gTTS(text=text, lang='es')
|
82 |
-
audio_fp = io.BytesIO()
|
83 |
-
tts.write_to_fp(audio_fp)
|
84 |
-
audio_fp.seek(0)
|
85 |
-
audio = AudioSegment.from_file(audio_fp, format="mp3")
|
86 |
-
modified_speed_audio = audio.speedup(playback_speed=speed)
|
87 |
-
modified_audio_fp = io.BytesIO()
|
88 |
-
modified_speed_audio.export(modified_audio_fp, format="mp3")
|
89 |
-
modified_audio_fp.seek(0)
|
90 |
-
return modified_audio_fp
|
91 |
-
|
92 |
-
def main():
|
93 |
-
audio_data = audiorecorder("Presiona para hablar", "Deteniendo la grabación...")
|
94 |
-
|
95 |
-
if not audio_data.empty():
|
96 |
-
st.audio(audio_data.export().read(), format="audio/wav")
|
97 |
-
audio_data.export("audio.wav", format="wav")
|
98 |
-
audio_text = recognize_speech("audio.wav")
|
99 |
-
|
100 |
-
if audio_text:
|
101 |
-
output, audio_file = generate(audio_text, history=st.session_state.history)
|
102 |
-
|
103 |
-
if audio_file is not None:
|
104 |
-
st.markdown(
|
105 |
-
f"""<audio autoplay="autoplay" controls="controls" src="data:audio/mp3;base64,{base64.b64encode(audio_file.read()).decode()}" type="audio/mp3" id="audio_player"></audio>""",
|
106 |
-
unsafe_allow_html=True)
|
107 |
-
|
108 |
-
if __name__ == "__main__":
|
109 |
-
main()
|
|
|
1 |
+
import webrtcvad
|
|
|
|
|
|
|
|
|
|
|
2 |
import speech_recognition as sr
|
|
|
3 |
|
4 |
+
# Configuramos la tasa de muestreo y el tamaño del frame
|
5 |
+
sample_rate = 16000
|
6 |
+
frame_size = 30
|
7 |
+
|
8 |
+
# Creamos un objeto VAD y un reconocedor de voz
|
9 |
+
vad = webrtcvad.Vad(aggressiveness=3)
|
10 |
+
recognizer = sr.Recognizer()
|
11 |
+
|
12 |
+
# Leemos un archivo de audio
|
13 |
+
with open("audio.wav", "rb") as f:
|
14 |
+
audio_data = f.read()
|
15 |
+
|
16 |
+
# Indicadores de estado
|
17 |
+
vad_active = False
|
18 |
+
speech_detected = False
|
19 |
+
phrase = ""
|
20 |
+
|
21 |
+
# Procesamos el audio en frames
|
22 |
+
for i in range(0, len(audio_data), frame_size):
|
23 |
+
# Obtenemos el frame actual
|
24 |
+
frame = audio_data[i:i+frame_size]
|
25 |
+
|
26 |
+
# Detectamos si hay voz en el frame
|
27 |
+
is_speech = vad.is_speech(frame, sample_rate)
|
28 |
+
|
29 |
+
# Actualizamos los indicadores de estado
|
30 |
+
if is_speech and not vad_active:
|
31 |
+
vad_active = True
|
32 |
+
speech_detected = True
|
33 |
+
print("️ Detección de voz iniciada")
|
34 |
+
elif not is_speech and vad_active:
|
35 |
+
vad_active = False
|
36 |
+
print("⏹️ Detección de voz finalizada")
|
37 |
+
|
38 |
+
# Si se ha detectado voz y hay un silencio, transcribimos la frase
|
39 |
+
if speech_detected and not is_speech:
|
40 |
+
# Transcribimos la frase
|
41 |
+
with sr.AudioData(frame, sample_rate) as source:
|
42 |
+
audio = recognizer.record(source)
|
43 |
+
try:
|
44 |
+
text = recognizer.recognize_google(audio)
|
45 |
+
phrase += f" {text}"
|
46 |
+
print(f"️ {text}")
|
47 |
+
except sr.RequestError:
|
48 |
+
print("⚠️ Error al transcribir la frase")
|
49 |
+
except sr.UnknownValueError:
|
50 |
+
print("⚠️ No se ha reconocido la frase")
|
51 |
+
|
52 |
+
# Reiniciamos el indicador de frase
|
53 |
+
speech_detected = False
|
54 |
+
|
55 |
+
# Imprimimos la frase completa
|
56 |
+
print(f"Transcripción completa: {phrase}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|