Update app.py
Browse files
app.py
CHANGED
|
@@ -1,46 +1,59 @@
|
|
| 1 |
-
import os
|
| 2 |
import gradio as gr
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
|
| 5 |
-
# Cliente com
|
| 6 |
-
client = InferenceClient(model="meta-llama/Llama-3.3-70B-Instruct")
|
| 7 |
-
)
|
| 8 |
|
| 9 |
-
# Função
|
| 10 |
def responder(mensagem, historico):
|
| 11 |
mensagens = []
|
| 12 |
if historico is None:
|
| 13 |
historico = []
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
mensagens.append({"role": "user", "content": mensagem})
|
| 18 |
-
|
| 19 |
resposta = ""
|
| 20 |
|
| 21 |
try:
|
| 22 |
-
for
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
except Exception as e:
|
| 30 |
-
print(f"
|
| 31 |
-
yield "
|
| 32 |
|
| 33 |
if not resposta.strip():
|
| 34 |
yield "Nenhuma resposta gerada. Tente novamente."
|
| 35 |
|
| 36 |
-
# Interface
|
| 37 |
demo = gr.ChatInterface(
|
| 38 |
responder,
|
| 39 |
-
title="Benjamin – Assistente Virtual da CEaD - IBC",
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
type="messages"
|
| 43 |
)
|
| 44 |
|
| 45 |
if __name__ == "__main__":
|
| 46 |
-
demo.launch()
|
|
|
|
|
|
|
| 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()
|