Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,40 +1,79 @@
|
|
1 |
import streamlit as st
|
2 |
-
import
|
3 |
-
import
|
|
|
|
|
4 |
|
5 |
-
|
6 |
-
|
|
|
7 |
|
8 |
-
|
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 |
-
if
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import base64
|
3 |
+
import io
|
4 |
+
from huggingface_hub import InferenceClient
|
5 |
+
from gtts import gTTS
|
6 |
|
7 |
+
client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
8 |
+
pre_prompt = "Hola, te llamar谩s Chaman 3.0, una IA conductual. Tus principios son el transhumanismo ecol贸gico."
|
9 |
+
pre_prompt_sent = False
|
10 |
|
11 |
+
def format_prompt(message, history):
|
12 |
+
global pre_prompt_sent
|
13 |
+
prompt = "<s>"
|
14 |
|
15 |
+
if not pre_prompt_sent and all(f"[INST] {pre_prompt} [/INST]" not in user_prompt for user_prompt, _ in history):
|
16 |
+
prompt += f"[INST] {pre_prompt} [/INST]"
|
17 |
+
pre_prompt_sent = True
|
18 |
|
19 |
+
for user_prompt, bot_response in history:
|
20 |
+
prompt += f"[INST] {user_prompt} [/INST]"
|
21 |
+
prompt += f" {bot_response}</s> "
|
22 |
|
23 |
+
prompt += f"[INST] {message} [/INST]"
|
24 |
+
return prompt
|
25 |
|
26 |
+
def text_to_speech(text, speed=1.3):
|
27 |
+
tts = gTTS(text=text, lang='es')
|
28 |
+
audio_fp = io.BytesIO()
|
29 |
+
tts.write_to_fp(audio_fp)
|
30 |
+
audio_fp.seek(0)
|
31 |
+
return audio_fp
|
32 |
|
33 |
+
def generate(user_input, history, temperature=None, max_new_tokens=512, top_p=0.95, repetition_penalty=1.0):
|
34 |
+
global pre_prompt_sent
|
35 |
+
temperature = float(temperature) if temperature is not None else 0.9
|
36 |
+
if temperature < 1e-2:
|
37 |
+
temperature = 1e-2
|
38 |
+
top_p = float(top_p)
|
39 |
|
40 |
+
generate_kwargs = dict(
|
41 |
+
temperature=temperature,
|
42 |
+
max_new_tokens=max_new_tokens,
|
43 |
+
top_p=top_p,
|
44 |
+
repetition_penalty=repetition_penalty,
|
45 |
+
do_sample=True,
|
46 |
+
seed=42,
|
47 |
+
)
|
48 |
|
49 |
+
formatted_prompt = format_prompt(user_input, history)
|
50 |
+
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=True)
|
51 |
+
response = ""
|
52 |
|
53 |
+
for response_token in stream:
|
54 |
+
response += response_token.token.text
|
55 |
+
|
56 |
+
response = ' '.join(response.split()).replace('</s>', '')
|
57 |
+
audio_file = text_to_speech(response, speed=1.3)
|
58 |
+
return response, audio_file
|
59 |
|
60 |
+
if "history" not in st.session_state:
|
61 |
+
st.session_state.history = []
|
62 |
+
|
63 |
+
with st.container():
|
64 |
+
user_input = st.text_input(label="Usuario", value="Saludos")
|
65 |
+
output, audio_file = generate(user_input, history=st.session_state.history)
|
66 |
+
st.text_area("Respuesta", height=400, value=output, key="output_text", disabled=True)
|
67 |
+
|
68 |
+
if user_input:
|
69 |
+
st.session_state.history.append((user_input, output))
|
70 |
+
|
71 |
+
st.write("Presiona el bot贸n y comienza a hablar...")
|
72 |
+
|
73 |
+
if audio_file is not None:
|
74 |
+
st.markdown(
|
75 |
+
f"""
|
76 |
+
<audio autoplay="autoplay" controls="controls" src="data:audio/mp3;base64,{base64.b64encode(audio_file.read()).decode()}" type="audio/mp3" id="audio_player"></audio>
|
77 |
+
""",
|
78 |
+
unsafe_allow_html=True
|
79 |
+
)
|