Update app.py
Browse files
app.py
CHANGED
@@ -4,32 +4,59 @@ from huggingface_hub import InferenceClient
|
|
4 |
# Cliente de inferência com modelo de IA pública
|
5 |
client = InferenceClient(model="meta-llama/Meta-Llama-3-8B-Instruct") # Modelo gratuito e avançado
|
6 |
|
7 |
-
mensagens = [
|
8 |
-
{"role": "system", "content": "Seu nome é Benjamin e você é um assistente útil especializado em acessibilidade digital e nos cursos a distância da Coordenação de Educação a Distância do Instituto Benjamin Constant, Centro de Referência na Área da Deficiência Visual."},
|
9 |
-
{"role": "user", "content": "Quais são as diretrizes do WCAG?"}
|
10 |
-
]
|
11 |
-
|
12 |
-
resposta = client.chat_completion(mensagens, max_tokens=5000)
|
13 |
-
|
14 |
-
print(resposta)
|
15 |
-
|
16 |
# Função para processar a conversa
|
17 |
def responder(mensagem, historico):
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
)
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
# Cliente de inferência com modelo de IA pública
|
5 |
client = InferenceClient(model="meta-llama/Meta-Llama-3-8B-Instruct") # Modelo gratuito e avançado
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
# Função para processar a conversa
|
8 |
def responder(mensagem, historico):
|
9 |
+
mensagens = []
|
10 |
+
if historico is None:
|
11 |
+
historico = []
|
12 |
+
|
13 |
+
for item in historico:
|
14 |
+
if isinstance(item, list) and len(item) == 2:
|
15 |
+
user_msg, bot_msg = item
|
16 |
+
mensagens.append({"role": "user", "content": user_msg})
|
17 |
+
if bot_msg:
|
18 |
+
mensagens.append({"role": "assistant", "content": bot_msg})
|
19 |
+
|
20 |
+
mensagens.append({"role": "user", "content": mensagem})
|
21 |
+
resposta = ""
|
22 |
+
|
23 |
+
try:
|
24 |
+
for mensagem in client.chat_completion(
|
25 |
+
mensagens,
|
26 |
+
max_tokens=5000,
|
27 |
+
stream=True,
|
28 |
+
temperature=0.5,
|
29 |
+
top_p=0.9,
|
30 |
+
):
|
31 |
+
if not mensagem or not isinstance(mensagem, dict):
|
32 |
+
continue
|
33 |
+
|
34 |
+
try:
|
35 |
+
conteudo = mensagem["choices"][0]["delta"].get("content", "")
|
36 |
+
if conteudo.strip():
|
37 |
+
resposta += conteudo
|
38 |
+
yield resposta
|
39 |
+
except (AttributeError, IndexError, KeyError) as e:
|
40 |
+
print(f"Erro ao processar mensagem: {e}")
|
41 |
+
continue
|
42 |
+
|
43 |
+
except Exception as e:
|
44 |
+
print(f"Erro inesperado: {e}")
|
45 |
+
yield "Ocorreu um erro ao gerar a resposta."
|
46 |
+
|
47 |
+
if not resposta.strip():
|
48 |
+
yield "Nenhuma resposta gerada. Tente novamente."
|
49 |
+
|
50 |
+
# Interface do chat com labels em português
|
51 |
+
demo = gr.ChatInterface(
|
52 |
+
responder,
|
53 |
+
title="Benjamin – Assistente Virtual com Inteligência Artificial da CEaD - IBC",
|
54 |
+
description="Tire dúvidas, receba orientações e aproveite melhor com a ajuda do Benjamin!",
|
55 |
+
submit_btn="Enviar",
|
56 |
+
stop_btn="Parar",
|
57 |
+
textbox=gr.Textbox(placeholder="Digite uma mensagem e depois tecle Enter"),
|
58 |
+
type="messages"
|
59 |
+
)
|
60 |
+
|
61 |
+
if __name__ == "__main__":
|
62 |
+
demo.launch()
|