zR commited on
Commit
cefabb8
·
1 Parent(s): 6976261
Files changed (3) hide show
  1. README.md +1 -1
  2. app.py +84 -0
  3. requirements.txt +4 -0
README.md CHANGED
@@ -4,7 +4,7 @@ emoji: 🐨
4
  colorFrom: green
5
  colorTo: indigo
6
  sdk: gradio
7
- sdk_version: 5.5.0
8
  app_file: app.py
9
  pinned: false
10
  ---
 
4
  colorFrom: green
5
  colorTo: indigo
6
  sdk: gradio
7
+ sdk_version: 5.6.0
8
  app_file: app.py
9
  pinned: false
10
  ---
app.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from threading import Thread
2
+ import gradio as gr
3
+ from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer
4
+ import space
5
+ tokenizer = AutoTokenizer.from_pretrained("THUDM/glm-edge-1.5b-chat")
6
+ model = AutoModelForCausalLM.from_pretrained("THUDM/glm-edge-1.5b-chat", device='auto')
7
+
8
+
9
+ def preprocess_messages(history):
10
+ messages = []
11
+
12
+ for idx, (user_msg, model_msg) in enumerate(history):
13
+ if idx == len(history) - 1 and not messages:
14
+ messages.append({"role": "user", "content": user_msg})
15
+ break
16
+ if user_msg:
17
+ messages.append({"role": "user", "content": user_msg})
18
+ if model_msg:
19
+ messages.append({"role": "assistant", "content": messages})
20
+
21
+ return messages
22
+
23
+
24
+ @spaces.GPU()
25
+ def predict(history, max_length, top_p, temperature):
26
+ messages = preprocess_messages(history)
27
+ model_inputs = tokenizer.apply_chat_template(
28
+ messages, add_generation_prompt=True, tokenize=True, return_tensors="pt", return_dict=True
29
+ )
30
+ streamer = TextIteratorStreamer(tokenizer, timeout=60, skip_prompt=True, skip_special_tokens=True)
31
+ generate_kwargs = {
32
+ "input_ids": model_inputs["input_ids"],
33
+ "attention_mask": model_inputs["attention_mask"],
34
+ "streamer": streamer,
35
+ "max_new_tokens": max_length,
36
+ "do_sample": True,
37
+ "top_p": top_p,
38
+ "temperature": temperature,
39
+ "repetition_penalty": 1.2,
40
+ }
41
+
42
+ generate_kwargs['eos_token_id'] = tokenizer.encode("<|user|>")
43
+
44
+ t = Thread(target=model.generate, kwargs=generate_kwargs)
45
+ t.start()
46
+ for new_token in streamer:
47
+ if new_token:
48
+ history[-1][1] += new_token
49
+ yield history
50
+
51
+
52
+ def main():
53
+ with gr.Blocks() as demo:
54
+ gr.HTML("""<h1 align="center">GLM-Edge-Chat Gradio Chat Demo</h1>""")
55
+
56
+ with gr.Row():
57
+ with gr.Column(scale=3):
58
+ chatbot = gr.Chatbot()
59
+
60
+ with gr.Row():
61
+ with gr.Column(scale=2):
62
+ user_input = gr.Textbox(show_label=True, placeholder="Input...", label="User Input")
63
+ submitBtn = gr.Button("Submit")
64
+ emptyBtn = gr.Button("Clear History")
65
+ with gr.Column(scale=1):
66
+ max_length = gr.Slider(0, 8192, value=4096, step=1.0, label="Maximum length", interactive=True)
67
+ top_p = gr.Slider(0, 1, value=0.8, step=0.01, label="Top P", interactive=True)
68
+ temperature = gr.Slider(0.01, 1, value=0.6, step=0.01, label="Temperature", interactive=True)
69
+
70
+ # Define functions for button actions
71
+ def user(query, history):
72
+ return "", history + [[query, ""]]
73
+
74
+ submitBtn.click(user, [user_input, chatbot], [user_input, chatbot], queue=False).then(
75
+ predict, [chatbot, max_length, top_p, temperature], chatbot
76
+ )
77
+ emptyBtn.click(lambda: (None, None), None, [chatbot], queue=False)
78
+
79
+ demo.queue()
80
+ demo.launch()
81
+
82
+
83
+ if __name__ == "__main__":
84
+ main()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ git+https://github.com/huggingface/transformers.git
2
+ gradio==5.6.0
3
+ space
4
+