File size: 1,528 Bytes
23927e6
ec53365
94472f0
193b23e
 
94472f0
ec53365
 
193b23e
ec53365
193b23e
 
 
 
 
 
ec53365
 
 
193b23e
 
 
23927e6
 
193b23e
ec53365
193b23e
 
ec53365
193b23e
ec53365
94472f0
ec53365
 
94472f0
ec53365
 
 
193b23e
23927e6
193b23e
 
 
 
ec53365
94472f0
23927e6
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import gradio as gr
from huggingface_hub import InferenceClient

# Substitua por seu token se for necessário: client = InferenceClient(token="seu_token_aqui")
client = InferenceClient(model="mistralai/Mistral-7B-Instruct-v0.3")

# Função para processar a conversa
def responder(mensagem, historico):
    historico = historico or []
    
    # Formata o histórico como contexto
    contexto = ""
    for user_msg, bot_msg in historico:
        contexto += f"<s>[INST] {user_msg} [/INST] {bot_msg} </s>\n"
    contexto += f"<s>[INST] {mensagem} [/INST]"

    resposta = ""

    try:
        for token in client.text_generation_stream(
            prompt=contexto,
            max_new_tokens=300,
            temperature=0.4,
            top_p=0.8,
            stop_sequences=["</s>"],
        ):
            resposta += token
            yield resposta
    except Exception as e:
        print(f"Erro ao gerar resposta: {e}")
        yield "Ocorreu um erro ao gerar a resposta."

    if not resposta.strip():
        yield "Nenhuma resposta gerada. Tente novamente."

# Interface do chat com labels em português
demo = gr.ChatInterface(
    responder,
    title="Benjamin – Assistente Virtual da CEaD - IBC",
    textbox=gr.Textbox(placeholder="Digite uma mensagem e depois tecle Enter"),
    description="Tire dúvidas com minha inteligência artificial (minha base de dados vai até 2021)",
    retry_btn="Tentar novamente",
    undo_btn="Desfazer",
    clear_btn="Limpar",
)

if __name__ == "__main__":
    demo.launch()