Update app.py
Browse files
app.py
CHANGED
|
@@ -2,48 +2,54 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
|
| 5 |
-
# Cliente com token seguro
|
| 6 |
client = InferenceClient(
|
| 7 |
model="mistralai/Mistral-7B-Instruct-v0.3",
|
| 8 |
token=os.getenv("HF_TOKEN")
|
| 9 |
)
|
| 10 |
|
| 11 |
-
# Função
|
| 12 |
def responder(mensagem, historico):
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
contexto = ""
|
| 17 |
-
for user_msg, bot_msg in historico:
|
| 18 |
-
contexto += f"<s>[INST] {user_msg} [/INST] {bot_msg} </s>\n"
|
| 19 |
-
contexto += f"<s>[INST] {mensagem} [/INST]"
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
resposta = ""
|
| 22 |
|
| 23 |
try:
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
except Exception as e:
|
| 34 |
-
print(f"
|
| 35 |
-
yield "Ocorreu um erro ao gerar a resposta."
|
| 36 |
|
| 37 |
if not resposta.strip():
|
| 38 |
yield "Nenhuma resposta gerada. Tente novamente."
|
| 39 |
|
| 40 |
-
# Interface
|
| 41 |
demo = gr.ChatInterface(
|
| 42 |
responder,
|
| 43 |
-
type="messages",
|
| 44 |
title="Benjamin – Assistente Virtual da CEaD - IBC",
|
| 45 |
description="Tire dúvidas com minha inteligência artificial (minha base de dados vai até 2021)",
|
| 46 |
-
|
|
|
|
| 47 |
)
|
| 48 |
|
| 49 |
if __name__ == "__main__":
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
|
| 5 |
+
# Cliente com token seguro
|
| 6 |
client = InferenceClient(
|
| 7 |
model="mistralai/Mistral-7B-Instruct-v0.3",
|
| 8 |
token=os.getenv("HF_TOKEN")
|
| 9 |
)
|
| 10 |
|
| 11 |
+
# Função de resposta no estilo chat
|
| 12 |
def responder(mensagem, historico):
|
| 13 |
+
mensagens = []
|
| 14 |
+
if historico is None:
|
| 15 |
+
historico = []
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
for entrada in historico:
|
| 18 |
+
if isinstance(entrada, list) and len(entrada) == 2:
|
| 19 |
+
mensagens.append({"role": "user", "content": entrada[0]})
|
| 20 |
+
mensagens.append({"role": "assistant", "content": entrada[1]})
|
| 21 |
+
|
| 22 |
+
mensagens.append({"role": "user", "content": mensagem})
|
| 23 |
resposta = ""
|
| 24 |
|
| 25 |
try:
|
| 26 |
+
resposta_stream = client.chat_completion(
|
| 27 |
+
messages=mensagens,
|
| 28 |
+
temperature=0.5,
|
| 29 |
+
max_tokens=500,
|
| 30 |
+
stream=True,
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
for parte in resposta_stream:
|
| 34 |
+
token = parte.get("choices", [{}])[0].get("delta", {}).get("content", "")
|
| 35 |
+
if token:
|
| 36 |
+
resposta += token
|
| 37 |
+
yield resposta
|
| 38 |
+
|
| 39 |
except Exception as e:
|
| 40 |
+
print(f"[ERRO] Falha ao consultar modelo: {e}")
|
| 41 |
+
yield "Ocorreu um erro ao gerar a resposta. Verifique o modelo ou o token."
|
| 42 |
|
| 43 |
if not resposta.strip():
|
| 44 |
yield "Nenhuma resposta gerada. Tente novamente."
|
| 45 |
|
| 46 |
+
# Interface Gradio
|
| 47 |
demo = gr.ChatInterface(
|
| 48 |
responder,
|
|
|
|
| 49 |
title="Benjamin – Assistente Virtual da CEaD - IBC",
|
| 50 |
description="Tire dúvidas com minha inteligência artificial (minha base de dados vai até 2021)",
|
| 51 |
+
textbox=gr.Textbox(placeholder="Digite uma mensagem e tecle Enter"),
|
| 52 |
+
type="messages"
|
| 53 |
)
|
| 54 |
|
| 55 |
if __name__ == "__main__":
|