Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,13 +1,13 @@
|
|
1 |
import streamlit as st
|
|
|
2 |
import io
|
3 |
from huggingface_hub import InferenceClient
|
|
|
4 |
from audiorecorder import audiorecorder
|
5 |
import speech_recognition as sr
|
6 |
-
import
|
7 |
-
import wave
|
8 |
-
import os
|
9 |
|
10 |
-
pre_prompt_text = "
|
11 |
|
12 |
if "history" not in st.session_state:
|
13 |
st.session_state.history = []
|
@@ -15,7 +15,6 @@ if "history" not in st.session_state:
|
|
15 |
if "pre_prompt_sent" not in st.session_state:
|
16 |
st.session_state.pre_prompt_sent = False
|
17 |
|
18 |
-
# Simplified error messages
|
19 |
def recognize_speech(audio_data, show_messages=True):
|
20 |
recognizer = sr.Recognizer()
|
21 |
audio_recording = sr.AudioFile(audio_data)
|
@@ -26,21 +25,28 @@ def recognize_speech(audio_data, show_messages=True):
|
|
26 |
try:
|
27 |
audio_text = recognizer.recognize_google(audio, language="es-ES")
|
28 |
if show_messages:
|
29 |
-
st.subheader("
|
30 |
st.write(audio_text)
|
31 |
-
st.success("
|
32 |
-
except
|
33 |
-
st.
|
|
|
|
|
|
|
34 |
audio_text = ""
|
35 |
|
36 |
return audio_text
|
37 |
|
38 |
-
# Simplified prompt formatting
|
39 |
def format_prompt(message, history):
|
40 |
-
prompt = "<s>
|
|
|
|
|
|
|
|
|
41 |
|
42 |
for user_prompt, bot_response in history:
|
43 |
-
prompt += f"[INST] {user_prompt} [/INST]
|
|
|
44 |
|
45 |
prompt += f"[INST] {message} [/INST]"
|
46 |
return prompt
|
@@ -68,15 +74,23 @@ def generate(audio_text, history, temperature=None, max_new_tokens=512, top_p=0.
|
|
68 |
response += response_token.token.text
|
69 |
|
70 |
response = ' '.join(response.split()).replace('</s>', '')
|
71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
-
# Simplified audio playback using os.system and say
|
74 |
-
def play_audio(text):
|
75 |
-
os.system(f"say '{text}'")
|
76 |
-
|
77 |
-
# Simplified audio playback
|
78 |
def main():
|
79 |
-
audio_data = audiorecorder("
|
80 |
|
81 |
if not audio_data.empty():
|
82 |
st.audio(audio_data.export().read(), format="audio/wav")
|
@@ -84,8 +98,12 @@ def main():
|
|
84 |
audio_text = recognize_speech("audio.wav")
|
85 |
|
86 |
if audio_text:
|
87 |
-
|
88 |
-
|
|
|
|
|
|
|
|
|
89 |
|
90 |
if __name__ == "__main__":
|
91 |
-
main()
|
|
|
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 speech_recognition as sr
|
8 |
+
from pydub import AudioSegment
|
|
|
|
|
9 |
|
10 |
+
pre_prompt_text = "You are a behavioral AI, your answers should be brief, stoic and humanistic."
|
11 |
|
12 |
if "history" not in st.session_state:
|
13 |
st.session_state.history = []
|
|
|
15 |
if "pre_prompt_sent" not in st.session_state:
|
16 |
st.session_state.pre_prompt_sent = False
|
17 |
|
|
|
18 |
def recognize_speech(audio_data, show_messages=True):
|
19 |
recognizer = sr.Recognizer()
|
20 |
audio_recording = sr.AudioFile(audio_data)
|
|
|
25 |
try:
|
26 |
audio_text = recognizer.recognize_google(audio, language="es-ES")
|
27 |
if show_messages:
|
28 |
+
st.subheader("Recognized text:")
|
29 |
st.write(audio_text)
|
30 |
+
st.success("Completed.")
|
31 |
+
except sr.UnknownValueError:
|
32 |
+
st.warning("The audio could not be recognized. Did you try to record something?")
|
33 |
+
audio_text = ""
|
34 |
+
except sr.RequestError:
|
35 |
+
st.error("Talk to me to get started!")
|
36 |
audio_text = ""
|
37 |
|
38 |
return audio_text
|
39 |
|
|
|
40 |
def format_prompt(message, history):
|
41 |
+
prompt = "<s>"
|
42 |
+
|
43 |
+
if not st.session_state.pre_prompt_sent:
|
44 |
+
prompt += f"[INST] {pre_prompt_text} [/INST]"
|
45 |
+
st.session_state.pre_prompt_sent = True
|
46 |
|
47 |
for user_prompt, bot_response in history:
|
48 |
+
prompt += f"[INST] {user_prompt} [/INST]"
|
49 |
+
prompt += f" {bot_response}</s> "
|
50 |
|
51 |
prompt += f"[INST] {message} [/INST]"
|
52 |
return prompt
|
|
|
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("Push to Play", "Stop Recording...")
|
94 |
|
95 |
if not audio_data.empty():
|
96 |
st.audio(audio_data.export().read(), format="audio/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()
|