File size: 2,449 Bytes
0f32de6
 
 
 
37e7b97
0f32de6
514ce55
 
011f128
 
 
 
37e7b97
 
0f32de6
 
37e7b97
 
 
 
 
1d7e7b8
37e7b97
 
 
0f32de6
 
1d7e7b8
37e7b97
 
1d7e7b8
 
37e7b97
 
 
 
 
 
 
 
 
 
 
 
0f32de6
 
 
37e7b97
0f32de6
37e7b97
0f32de6
 
37e7b97
 
 
 
0f32de6
 
 
 
 
 
 
 
 
 
 
 
 
 
37e7b97
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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, token=True)
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16, device_map="auto", token=True)


# Set pad_token_id if it's not already set
if tokenizer.pad_token_id is None:
    tokenizer.pad_token_id = tokenizer.eos_token_id

def chat_with_model(messages):
    # Prepare the input
    input_ids = tokenizer.encode(str(messages), return_tensors="pt").to(model.device)
    attention_mask = torch.ones_like(input_ids)
    
    # Generate response
    with torch.no_grad():
        output = model.generate(
            input_ids,
            attention_mask=attention_mask,
            max_length=1000,
            num_return_sequences=1,
            temperature=0.7,
            pad_token_id=tokenizer.pad_token_id
        )
    
    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()