Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,28 +1,65 @@
|
|
1 |
import gradio as gr
|
2 |
from gtts import gTTS
|
3 |
import os
|
|
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
audio_file = "response.mp3"
|
13 |
tts.save(audio_file)
|
14 |
-
|
15 |
-
#
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
25 |
)
|
26 |
|
27 |
-
|
28 |
-
|
|
|
1 |
import gradio as gr
|
2 |
from gtts import gTTS
|
3 |
import os
|
4 |
+
from huggingface_hub import InferenceClient
|
5 |
|
6 |
+
# Hugging Face inference client para o chatbot
|
7 |
+
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
+
|
9 |
+
# Função para gerar resposta de texto e áudio
|
10 |
+
def respond(
|
11 |
+
message,
|
12 |
+
history: list[tuple[str, str]],
|
13 |
+
system_message,
|
14 |
+
max_tokens,
|
15 |
+
temperature,
|
16 |
+
top_p,
|
17 |
+
):
|
18 |
+
messages = [{"role": "system", "content": system_message}]
|
19 |
+
|
20 |
+
for val in history:
|
21 |
+
if val[0]:
|
22 |
+
messages.append({"role": "user", "content": val[0]})
|
23 |
+
if val[1]:
|
24 |
+
messages.append({"role": "assistant", "content": val[1]})
|
25 |
+
|
26 |
+
messages.append({"role": "user", "content": message})
|
27 |
+
|
28 |
+
response = ""
|
29 |
+
|
30 |
+
# Geração de resposta do chatbot
|
31 |
+
for message in client.chat_completion(
|
32 |
+
messages,
|
33 |
+
max_tokens=max_tokens,
|
34 |
+
stream=True,
|
35 |
+
temperature=temperature,
|
36 |
+
top_p=top_p,
|
37 |
+
):
|
38 |
+
token = message.choices[0].delta.content
|
39 |
+
response += token
|
40 |
+
yield response, None # Retornando a resposta do chatbot inicialmente como texto
|
41 |
+
|
42 |
+
# Convertendo a resposta em áudio usando gTTS
|
43 |
+
tts = gTTS(response, lang='pt')
|
44 |
audio_file = "response.mp3"
|
45 |
tts.save(audio_file)
|
46 |
+
|
47 |
+
yield response, audio_file # Retornando o texto e o arquivo de áudio gerado
|
48 |
+
|
49 |
+
# Interface do Gradio com campo adicional para ajustar o sistema e configurações
|
50 |
+
demo = gr.ChatInterface(
|
51 |
+
fn=respond,
|
52 |
+
additional_inputs=[
|
53 |
+
gr.Textbox(value="Você é um chatbot amigável.", label="Mensagem do sistema"),
|
54 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
55 |
+
gr.Slider(minimum=0.1, maximum 4.0, value=0.7, step=0.1, label="Temperatura"),
|
56 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
57 |
+
],
|
58 |
+
outputs=[
|
59 |
+
"text", # Texto de saída do chatbot
|
60 |
+
gr.Audio(label="Áudio da Resposta") # Saída de áudio da resposta gerada
|
61 |
+
]
|
62 |
)
|
63 |
|
64 |
+
if __name__ == "__main__":
|
65 |
+
demo.launch()
|