File size: 2,048 Bytes
0f32de6
 
 
 
 
 
514ce55
 
011f128
 
 
 
514ce55
011f128
0f32de6
 
f3e59d7
514ce55
 
0f32de6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
514ce55
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
49
50
51
52
53
import os
from datetime import datetime
import uuid
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
from huggingface_hub import login

from dotenv import load_dotenv
# Load environment variables
load_dotenv()

# Authenticate with Hugging Face
login(token=os.getenv("HUGGINGFACE_TOKEN"))

# Load model and tokenizer
model_name = "meta-llama/Meta-Llama-3.1-8B-Instruct"
tokenizer = AutoTokenizer.from_pretrained(model_name, use_auth_token=True)
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto", use_auth_token=True)

def chat_with_model(messages):
    input_ids = tokenizer.encode(str(messages), return_tensors="pt").to(model.device)
    output = model.generate(input_ids, max_length=1000, num_return_sequences=1, temperature=0.7)
    response = tokenizer.decode(output[0], skip_special_tokens=True)
    return response

def chat_with_model_gradio(message, history, session_id):
    messages = [
        {"role": "system", "content": f"λ„ˆμ˜ 이름은 ChatMBTI. μ‚¬λžŒλ“€μ˜ MBTIμœ ν˜•μ— μ•Œλ§žμ€ 상담을 μ§„ν–‰ν•  수 μžˆμ–΄. μƒλŒ€λ°©μ˜ MBTI μœ ν˜•μ„ λ¨Όμ € 물어보고, κ·Έ μœ ν˜•μ— μ•Œλ§žκ²Œ 상담을 μ§„ν–‰ν•΄μ€˜. 참고둜 ν˜„μž¬ μ‹œκ°μ€ {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}이야."},
    ]
    messages.extend([{"role": "user" if i % 2 == 0 else "assistant", "content": m} for i, m in enumerate(history)])
    messages.append({"role": "user", "content": message})

    response = chat_with_model(messages)
    history.append((message, response))

    return "", history

def main():
    session_id = str(uuid.uuid4())
    with gr.Blocks() as demo:
        chatbot = gr.Chatbot(label="ChatMBTI")
        msg = gr.Textbox(label="λ©”μ‹œμ§€λ₯Ό μž…λ ₯ν•˜μ„Έμš”")
        clear = gr.Button("λŒ€ν™” μ΄ˆκΈ°ν™”")

        msg.submit(chat_with_model_gradio, [msg, chatbot, gr.State(session_id)], [msg, chatbot])
        clear.click(lambda: None, None, chatbot, queue=False)

    demo.launch()

if __name__ == "__main__":
    main()