Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,12 @@
|
|
1 |
import streamlit as st
|
|
|
2 |
import io
|
3 |
from huggingface_hub import InferenceClient
|
4 |
from gtts import gTTS
|
5 |
from audiorecorder import audiorecorder
|
|
|
6 |
import speech_recognition as sr
|
|
|
7 |
|
8 |
pre_prompt_text = "eres una IA conductual, tus respuestas serán breves."
|
9 |
|
@@ -15,9 +18,7 @@ if "pre_prompt_sent" not in st.session_state:
|
|
15 |
|
16 |
def recognize_speech(audio_data, show_messages=True):
|
17 |
recognizer = sr.Recognizer()
|
18 |
-
|
19 |
-
audio_data.seek(0) # Asegurémonos de que el puntero esté al principio
|
20 |
-
audio_recording = sr.AudioFile(io.BytesIO(audio_data.read()))
|
21 |
|
22 |
with audio_recording as source:
|
23 |
audio = recognizer.record(source)
|
@@ -47,7 +48,7 @@ def format_prompt(message, history):
|
|
47 |
prompt += f"[INST] {message} [/INST]"
|
48 |
return prompt
|
49 |
|
50 |
-
def generate(audio_text, history, temperature=None, max_new_tokens=
|
51 |
client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
52 |
|
53 |
temperature = float(temperature) if temperature is not None else 0.9
|
@@ -72,34 +73,45 @@ def generate(audio_text, history, temperature=None, max_new_tokens=512, top_p=0.
|
|
72 |
response += response_token.token.text
|
73 |
|
74 |
response = ' '.join(response.split()).replace('</s>', '')
|
75 |
-
audio_file = text_to_speech(response)
|
76 |
return response, audio_file
|
77 |
|
78 |
-
def text_to_speech(text):
|
79 |
tts = gTTS(text=text, lang='es')
|
80 |
audio_fp = io.BytesIO()
|
81 |
tts.write_to_fp(audio_fp)
|
82 |
audio_fp.seek(0)
|
83 |
-
|
|
|
|
|
|
|
|
|
|
|
84 |
|
85 |
-
def audio_play(
|
86 |
-
|
87 |
-
audio_binary = audio_file.read()
|
88 |
-
st.audio(audio_binary, format="audio/mp3", start_time=0)
|
89 |
|
90 |
def display_recognition_result(audio_text, output, audio_file):
|
91 |
if audio_text:
|
92 |
st.session_state.history.append((audio_text, output))
|
93 |
-
|
|
|
|
|
|
|
|
|
94 |
|
95 |
def main():
|
96 |
if not st.session_state.pre_prompt_sent:
|
97 |
st.session_state.pre_prompt_sent = True
|
98 |
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
|
|
|
|
|
|
|
|
103 |
|
104 |
if audio_text:
|
105 |
output, audio_file = generate(audio_text, history=st.session_state.history)
|
|
|
1 |
import streamlit as st
|
2 |
+
import base64
|
3 |
import io
|
4 |
from huggingface_hub import InferenceClient
|
5 |
from gtts import gTTS
|
6 |
from audiorecorder import audiorecorder
|
7 |
+
import wave
|
8 |
import speech_recognition as sr
|
9 |
+
from pydub import AudioSegment
|
10 |
|
11 |
pre_prompt_text = "eres una IA conductual, tus respuestas serán breves."
|
12 |
|
|
|
18 |
|
19 |
def recognize_speech(audio_data, show_messages=True):
|
20 |
recognizer = sr.Recognizer()
|
21 |
+
audio_recording = sr.AudioFile(audio_data)
|
|
|
|
|
22 |
|
23 |
with audio_recording as source:
|
24 |
audio = recognizer.record(source)
|
|
|
48 |
prompt += f"[INST] {message} [/INST]"
|
49 |
return prompt
|
50 |
|
51 |
+
def generate(audio_text, history, temperature=None, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0):
|
52 |
client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
53 |
|
54 |
temperature = float(temperature) if temperature is not None else 0.9
|
|
|
73 |
response += response_token.token.text
|
74 |
|
75 |
response = ' '.join(response.split()).replace('</s>', '')
|
76 |
+
audio_file = text_to_speech(response, speed=1.3)
|
77 |
return response, audio_file
|
78 |
|
79 |
+
def text_to_speech(text, speed=1.3):
|
80 |
tts = gTTS(text=text, lang='es')
|
81 |
audio_fp = io.BytesIO()
|
82 |
tts.write_to_fp(audio_fp)
|
83 |
audio_fp.seek(0)
|
84 |
+
audio = AudioSegment.from_file(audio_fp, format="mp3")
|
85 |
+
modified_speed_audio = audio.speedup(playback_speed=speed)
|
86 |
+
modified_audio_fp = io.BytesIO()
|
87 |
+
modified_speed_audio.export(modified_audio_fp, format="mp3")
|
88 |
+
modified_audio_fp.seek(0)
|
89 |
+
return modified_audio_fp
|
90 |
|
91 |
+
def audio_play(audio_fp):
|
92 |
+
st.audio(audio_fp.read(), format="audio/mp3", start_time=0)
|
|
|
|
|
93 |
|
94 |
def display_recognition_result(audio_text, output, audio_file):
|
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"""<audio autoplay="autoplay" controls="controls" src="data:audio/mp3;base64,{base64.b64encode(audio_file.read()).decode()}" type="audio/mp3" id="audio_player"></audio>""",
|
101 |
+
unsafe_allow_html=True)
|
102 |
|
103 |
def main():
|
104 |
if not st.session_state.pre_prompt_sent:
|
105 |
st.session_state.pre_prompt_sent = True
|
106 |
|
107 |
+
audio_data = audiorecorder(start_prompt="Hablar ▶️", stop_prompt="Detener 🛑")
|
108 |
+
|
109 |
+
if audio_data and 'bytes' in audio_data:
|
110 |
+
audio_bytes = audio_data['bytes']
|
111 |
+
st.audio(audio_bytes, format="audio/wav")
|
112 |
+
audio_data_io = io.BytesIO(audio_bytes)
|
113 |
+
audio_data_io.seek(0)
|
114 |
+
audio_text = recognize_speech(audio_data_io)
|
115 |
|
116 |
if audio_text:
|
117 |
output, audio_file = generate(audio_text, history=st.session_state.history)
|