File size: 1,152 Bytes
f96f6fb
db0699d
f96f6fb
0ded2ac
f96f6fb
0ded2ac
6c8697c
f96f6fb
 
 
6c8697c
0ded2ac
864db67
db0699d
0ded2ac
864db67
 
 
db0699d
864db67
0ded2ac
864db67
db0699d
864db67
db0699d
864db67
 
 
 
 
db0699d
864db67
 
 
 
db0699d
864db67
db0699d
864db67
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
from transformers import AutoModelForCausalLM, AutoTokenizer

model_name = "TheBloke/phi-2-GPTQ"

tokenizer = AutoTokenizer.from_pretrained(model_name)

model = AutoModelForCausalLM.from_pretrained(
    model_name,
    device_map="auto",    # ou "cuda:0" se for GPU
    trust_remote_code=True
)
# Pipeline de texto
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)

# Função do chat + salvar memória
def chat(user_input, history):
    prompt = user_input
    result = pipe(prompt, max_new_tokens=256, temperature=0.7)[0]["generated_text"]

    # Salvar memória em arquivo
    with open("memoria.txt", "a", encoding="utf-8") as f:
        f.write(f"User: {user_input}\nAI: {result}\n")

    return result

# Interface Gradio
with gr.Blocks() as demo:
    chat_history = gr.State([])
    chatbot = gr.Chatbot()
    msg = gr.Textbox(label="Digite sua pergunta:")

    def respond(user_input, chat_history):
        answer = chat(user_input, chat_history)
        chat_history.append((user_input, answer))
        return chat_history, chat_history

    msg.submit(respond, [msg, chat_history], [chatbot, chat_history])

demo.launch()