beingcognitive commited on
Commit
0f32de6
Β·
1 Parent(s): e198429

for the class

Browse files
Files changed (2) hide show
  1. app.py +44 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from datetime import datetime
3
+ import uuid
4
+ import gradio as gr
5
+ from transformers import AutoTokenizer, AutoModelForCausalLM
6
+ import torch
7
+
8
+ # Load model and tokenizer
9
+ model_name = "meta-llama/Llama-2-7b-chat-hf"
10
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
11
+ model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto")
12
+
13
+ def chat_with_model(messages):
14
+ input_ids = tokenizer.encode(str(messages), return_tensors="pt").to(model.device)
15
+ output = model.generate(input_ids, max_length=1000, num_return_sequences=1, temperature=0.7)
16
+ response = tokenizer.decode(output[0], skip_special_tokens=True)
17
+ return response
18
+
19
+ def chat_with_model_gradio(message, history, session_id):
20
+ messages = [
21
+ {"role": "system", "content": f"λ„ˆμ˜ 이름은 ChatMBTI. μ‚¬λžŒλ“€μ˜ MBTIμœ ν˜•μ— μ•Œλ§žμ€ 상담을 진행할 수 μžˆμ–΄. μƒλŒ€λ°©μ˜ MBTI μœ ν˜•μ„ λ¨Όμ € 물어보고, κ·Έ μœ ν˜•μ— μ•Œλ§žκ²Œ 상담을 μ§„ν–‰ν•΄μ€˜. 참고둜 ν˜„μž¬ μ‹œκ°μ€ {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}이야."},
22
+ ]
23
+ messages.extend([{"role": "user" if i % 2 == 0 else "assistant", "content": m} for i, m in enumerate(history)])
24
+ messages.append({"role": "user", "content": message})
25
+
26
+ response = chat_with_model(messages)
27
+ history.append((message, response))
28
+
29
+ return "", history
30
+
31
+ def main():
32
+ session_id = str(uuid.uuid4())
33
+ with gr.Blocks() as demo:
34
+ chatbot = gr.Chatbot(label="ChatMBTI")
35
+ msg = gr.Textbox(label="λ©”μ‹œμ§€λ₯Ό μž…λ ₯ν•˜μ„Έμš”")
36
+ clear = gr.Button("λŒ€ν™” μ΄ˆκΈ°ν™”")
37
+
38
+ msg.submit(chat_with_model_gradio, [msg, chatbot, gr.State(session_id)], [msg, chatbot])
39
+ clear.click(lambda: None, None, chatbot, queue=False)
40
+
41
+ demo.launch()
42
+
43
+ if __name__ == "__main__":
44
+ main()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ transformers
2
+ torch