Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,65 +1,28 @@
|
|
1 |
import gradio as gr
|
2 |
from gtts import gTTS
|
3 |
import os
|
4 |
-
from huggingface_hub import InferenceClient
|
5 |
|
6 |
-
#
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
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 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
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 |
-
|
65 |
-
|
|
|
1 |
import gradio as gr
|
2 |
from gtts import gTTS
|
3 |
import os
|
|
|
4 |
|
5 |
+
# Função para gerar resposta de áudio
|
6 |
+
def chatbot_response(message):
|
7 |
+
# Aqui você pode definir uma lógica mais complexa para as respostas
|
8 |
+
response_text = f"Você disse: {message}"
|
9 |
+
|
10 |
+
# Convertendo a resposta em voz
|
11 |
+
tts = gTTS(response_text, lang='pt')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
audio_file = "response.mp3"
|
13 |
tts.save(audio_file)
|
14 |
+
|
15 |
+
# Retorna o arquivo de áudio gerado
|
16 |
+
return audio_file
|
17 |
+
|
18 |
+
# Criando a interface no Gradio
|
19 |
+
interface = gr.Interface(
|
20 |
+
fn=chatbot_response,
|
21 |
+
inputs="text",
|
22 |
+
outputs="audio",
|
23 |
+
title="Chatbot com Resposta em Voz",
|
24 |
+
description="Digite sua pergunta e o chatbot irá responder com voz."
|
|
|
|
|
|
|
|
|
|
|
25 |
)
|
26 |
|
27 |
+
# Executando a interface
|
28 |
+
interface.launch()
|