nurrizkyaj commited on
Commit
106722f
·
verified ·
1 Parent(s): ecaaa10

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +60 -0
  2. requirements.txt +0 -0
app.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer
4
+ import spaces
5
+
6
+ device = "cuda" if torch.cuda.is_available() else "cpu"
7
+
8
+ model = AutoModelForCausalLM.from_pretrained(
9
+ "Qwen/Qwen2-0.5B-Instruct",
10
+ torch_dtype="auto",
11
+ device_map="auto"
12
+ ).to(device)
13
+ tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2-0.5B-Instruct")
14
+
15
+ @spaces.GPU
16
+
17
+ def chatbot(user_input, history):
18
+ system_message = {"role": "system", "content": "You are a helpful assistant."}
19
+ messages = history + [{"role": "user", "content": user_input}]
20
+
21
+ if len(history) == 0:
22
+ messages.insert(0, system_message)
23
+
24
+ text = tokenizer.apply_chat_template(
25
+ messages,
26
+ tokenize=False,
27
+ add_generation_prompt=True
28
+ )
29
+
30
+ model_inputs = tokenizer([text], return_tensors="pt").to(device)
31
+ attention_mask = torch.ones(model_inputs.input_ids.shape, device=device)
32
+
33
+ generated_ids = model.generate(
34
+ model_inputs.input_ids,
35
+ attention_mask=attention_mask,
36
+ max_new_tokens=512
37
+ )
38
+
39
+ generated_ids = [
40
+ output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
41
+ ]
42
+
43
+ response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
44
+
45
+ history.append({"role": "user", "content": user_input})
46
+ history.append({"role": "assistant", "content": response})
47
+
48
+ gradio_history = [[msg["role"], msg["content"]] for msg in history]
49
+
50
+ return gradio_history, history
51
+
52
+ with gr.Blocks() as demo:
53
+ chatbot_interface = gr.Chatbot()
54
+ state = gr.State([])
55
+
56
+ with gr.Row():
57
+ txt = gr.Textbox(show_label=False, placeholder="Ask anything")
58
+ txt.submit(chatbot, [txt, state], [chatbot_interface, state])
59
+
60
+ demo.launch()
requirements.txt ADDED
Binary file (332 Bytes). View file