Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,42 +1,57 @@
|
|
1 |
import gradio as gr
|
2 |
from llama_cpp import Llama
|
3 |
|
4 |
-
# Carregar o modelo GGML
|
5 |
llm = Llama(model_path="Llama-3.2-3B-Instruct-Q8_0.gguf", n_ctx=512)
|
6 |
|
7 |
-
# Definir a mensagem inicial
|
8 |
-
mensagem_inicial =
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
-
#
|
15 |
-
|
16 |
-
|
17 |
-
prompt = "\n".join([f"User: {user}\nAssistant: {bot}" for user, bot in history])
|
18 |
-
prompt += f"\nUser: {message}\nAssistant:"
|
19 |
|
20 |
-
# Executar
|
21 |
response = ""
|
22 |
result = llm(prompt, max_tokens=100, temperature=0.7, top_p=0.95, stream=True)
|
23 |
|
24 |
-
#
|
25 |
for token in result:
|
26 |
response += token["choices"][0]["text"]
|
27 |
-
yield response
|
28 |
-
|
29 |
-
# Configurar a interface Gradio com a
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
if __name__ == "__main__":
|
42 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from llama_cpp import Llama
|
3 |
|
4 |
+
# Carregar o modelo GGML
|
5 |
llm = Llama(model_path="Llama-3.2-3B-Instruct-Q8_0.gguf", n_ctx=512)
|
6 |
|
7 |
+
# Definir a mensagem inicial como resposta automática
|
8 |
+
mensagem_inicial = (
|
9 |
+
"Olá! Eu sou um atendente virtual da Receita Federal e do Projeto Remessa Conforme. "
|
10 |
+
"Você pode perguntar tudo sobre como funciona o Remessa Conforme, suas regras e benefícios. "
|
11 |
+
"Este projeto foi desenvolvido com a colaboração de Anderson Xavier. Como posso te ajudar hoje?"
|
12 |
+
)
|
13 |
+
|
14 |
+
# Função para gerar uma resposta inicial automática
|
15 |
+
def start_conversation():
|
16 |
+
return [(None, mensagem_inicial)]
|
17 |
+
|
18 |
+
# Função de resposta para processar mensagens
|
19 |
+
def respond(message, history):
|
20 |
+
# Adicionar a mensagem do usuário ao histórico
|
21 |
+
history = history + [(message, "")]
|
22 |
|
23 |
+
# Construir o prompt com o histórico
|
24 |
+
prompt = "\n".join([f"User: {user}\nAssistant: {bot}" for user, bot in history if user])
|
25 |
+
prompt += "\nAssistant:"
|
|
|
|
|
26 |
|
27 |
+
# Executar inferência com o modelo
|
28 |
response = ""
|
29 |
result = llm(prompt, max_tokens=100, temperature=0.7, top_p=0.95, stream=True)
|
30 |
|
31 |
+
# Adicionar a resposta do modelo ao histórico
|
32 |
for token in result:
|
33 |
response += token["choices"][0]["text"]
|
34 |
+
yield history[:-1] + [(message, response)]
|
35 |
+
|
36 |
+
# Configurar a interface Gradio com a resposta inicial automática
|
37 |
+
with gr.Blocks() as demo:
|
38 |
+
chatbot = gr.Chatbot() # Inicializa o chatbot
|
39 |
+
msg = gr.Textbox(show_label=False, placeholder="Digite sua mensagem...")
|
40 |
+
state = gr.State([]) # Estado para manter o histórico
|
41 |
+
|
42 |
+
# Função de atualização para o chatbot
|
43 |
+
def load_initial_message():
|
44 |
+
initial_history = start_conversation()
|
45 |
+
return initial_history
|
46 |
+
|
47 |
+
# Disparar a mensagem inicial ao carregar a interface
|
48 |
+
chatbot.update(load_initial_message())
|
49 |
+
|
50 |
+
# Lógica para enviar a mensagem e processar respostas
|
51 |
+
def send_message(user_message, history):
|
52 |
+
return respond(user_message, history)
|
53 |
+
|
54 |
+
msg.submit(send_message, [msg, state], [chatbot, state])
|
55 |
|
56 |
if __name__ == "__main__":
|
57 |
demo.launch()
|