Update app.py
Browse files
app.py
CHANGED
|
@@ -1,47 +1,33 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
client = InferenceClient(model="mistralai/Mistral-7B-Instruct-v0.3")
|
| 6 |
|
| 7 |
# Função para processar a conversa
|
| 8 |
def responder(mensagem, historico):
|
| 9 |
-
|
| 10 |
-
if historico is None:
|
| 11 |
-
historico = []
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
mensagens.append({"role": "user", "content": mensagem})
|
| 21 |
resposta = ""
|
| 22 |
|
| 23 |
try:
|
| 24 |
-
for
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
stream=True,
|
| 28 |
temperature=0.4,
|
| 29 |
top_p=0.8,
|
|
|
|
| 30 |
):
|
| 31 |
-
|
| 32 |
-
|
| 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
|
| 45 |
yield "Ocorreu um erro ao gerar a resposta."
|
| 46 |
|
| 47 |
if not resposta.strip():
|
|
@@ -50,9 +36,12 @@ def responder(mensagem, historico):
|
|
| 50 |
# Interface do chat com labels em português
|
| 51 |
demo = gr.ChatInterface(
|
| 52 |
responder,
|
| 53 |
-
title="Benjamin – Assistente Virtual da CEaD - IBC
|
| 54 |
textbox=gr.Textbox(placeholder="Digite uma mensagem e depois tecle Enter"),
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
| 56 |
)
|
| 57 |
|
| 58 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
+
# Substitua por seu token se for necessário: client = InferenceClient(token="seu_token_aqui")
|
| 5 |
+
client = InferenceClient(model="mistralai/Mistral-7B-Instruct-v0.3")
|
| 6 |
|
| 7 |
# Função para processar a conversa
|
| 8 |
def responder(mensagem, historico):
|
| 9 |
+
historico = historico or []
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# Formata o histórico como contexto
|
| 12 |
+
contexto = ""
|
| 13 |
+
for user_msg, bot_msg in historico:
|
| 14 |
+
contexto += f"<s>[INST] {user_msg} [/INST] {bot_msg} </s>\n"
|
| 15 |
+
contexto += f"<s>[INST] {mensagem} [/INST]"
|
| 16 |
+
|
|
|
|
|
|
|
| 17 |
resposta = ""
|
| 18 |
|
| 19 |
try:
|
| 20 |
+
for token in client.text_generation_stream(
|
| 21 |
+
prompt=contexto,
|
| 22 |
+
max_new_tokens=300,
|
|
|
|
| 23 |
temperature=0.4,
|
| 24 |
top_p=0.8,
|
| 25 |
+
stop_sequences=["</s>"],
|
| 26 |
):
|
| 27 |
+
resposta += token
|
| 28 |
+
yield resposta
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
except Exception as e:
|
| 30 |
+
print(f"Erro ao gerar resposta: {e}")
|
| 31 |
yield "Ocorreu um erro ao gerar a resposta."
|
| 32 |
|
| 33 |
if not resposta.strip():
|
|
|
|
| 36 |
# Interface do chat com labels em português
|
| 37 |
demo = gr.ChatInterface(
|
| 38 |
responder,
|
| 39 |
+
title="Benjamin – Assistente Virtual da CEaD - IBC",
|
| 40 |
textbox=gr.Textbox(placeholder="Digite uma mensagem e depois tecle Enter"),
|
| 41 |
+
description="Tire dúvidas com minha inteligência artificial (minha base de dados vai até 2021)",
|
| 42 |
+
retry_btn="Tentar novamente",
|
| 43 |
+
undo_btn="Desfazer",
|
| 44 |
+
clear_btn="Limpar",
|
| 45 |
)
|
| 46 |
|
| 47 |
if __name__ == "__main__":
|