ST-THOMAS-OF-AQUINAS commited on
Commit
5dacbc7
Β·
verified Β·
1 Parent(s): e67cea7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -0
app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
+ import gradio as gr
4
+
5
+ MODEL_NAME = "Qwen/Qwen2.5-1.5B-Instruct"
6
+
7
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, trust_remote_code=True)
8
+ model = AutoModelForCausalLM.from_pretrained(
9
+ MODEL_NAME,
10
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
11
+ device_map="auto",
12
+ trust_remote_code=True
13
+ )
14
+
15
+ def chat(user_input, history):
16
+ messages = []
17
+
18
+ for h in history:
19
+ messages.append({"role": "user", "content": h[0]})
20
+ messages.append({"role": "assistant", "content": h[1]})
21
+
22
+ messages.append({"role": "user", "content": user_input})
23
+
24
+ prompt = tokenizer.apply_chat_template(
25
+ messages,
26
+ tokenize=False,
27
+ add_generation_prompt=True
28
+ )
29
+
30
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
31
+
32
+ outputs = model.generate(
33
+ **inputs,
34
+ max_new_tokens=512,
35
+ do_sample=True,
36
+ temperature=0.7,
37
+ top_p=0.9
38
+ )
39
+
40
+ response = tokenizer.decode(
41
+ outputs[0][inputs["input_ids"].shape[-1]:],
42
+ skip_special_tokens=True
43
+ )
44
+
45
+ history.append((user_input, response))
46
+ return history, ""
47
+
48
+ with gr.Blocks() as demo:
49
+ gr.Markdown("## πŸ€– Qwen Chatbot")
50
+
51
+ chatbot = gr.Chatbot()
52
+ msg = gr.Textbox(label="Your message")
53
+ clear = gr.Button("Clear")
54
+
55
+ msg.submit(chat, [msg, chatbot], [chatbot, msg])
56
+ clear.click(lambda: [], None, chatbot)
57
+
58
+ demo.launch()