Spaces:
Sleeping
Sleeping
Commit
Β·
0f32de6
1
Parent(s):
e198429
for the class
Browse files- app.py +44 -0
- 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
|