Update app.py
Browse files
app.py
CHANGED
@@ -1,35 +1,59 @@
|
|
1 |
-
|
2 |
-
import
|
3 |
|
4 |
-
|
5 |
-
|
6 |
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
response = output[0].content[0].text.value
|
13 |
-
return response
|
14 |
-
css = """
|
15 |
-
label[data-testid="block-label"] {
|
16 |
-
display: none !important;
|
17 |
-
}
|
18 |
-
footer {
|
19 |
-
display: none !important;
|
20 |
-
}
|
21 |
-
"""
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
}
|
31 |
-
"""
|
32 |
-
with gr.Blocks(css=css, js = js_func, theme="monochrome") as demo:
|
33 |
-
chatbot = gr.ChatInterface(chat_response,title="ProjSite")
|
34 |
|
35 |
-
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from huggingface_hub import InferenceClient
|
3 |
|
4 |
+
# Cliente de inferência com modelo de IA pública
|
5 |
+
client = InferenceClient(model="meta-llama/Llama-3.3-70B-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=300,
|
27 |
+
stream=True,
|
28 |
+
temperature=0.4,
|
29 |
+
top_p=0.8,
|
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 da CEaD - IBC. Tire dúvidas com minha inteligência artificial (minha base de dados vai até 2021)",
|
54 |
+
textbox=gr.Textbox(placeholder="Digite uma mensagem e depois tecle Enter"),
|
55 |
+
type="messages"
|
56 |
+
)
|
|
|
|
|
|
|
|
|
57 |
|
58 |
+
if __name__ == "__main__":
|
59 |
+
demo.launch()
|