akhaliq HF Staff commited on
Commit
9a03601
·
verified ·
1 Parent(s): 99c7c23

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -11
app.py CHANGED
@@ -12,26 +12,37 @@ pipe = pipeline(
12
  device_map="auto",
13
  )
14
 
15
- @spaces.GPU(duration=120)
16
- def respond(message, history):
17
  if history is None:
18
  history = []
19
- new_history = history + [{"role": "user", "content": message}]
20
- outputs = pipe(
21
- new_history,
22
- max_new_tokens=10000,
23
- )
24
- response = outputs[0]["generated_text"][-1]["content"]
25
- new_history.append({"role": "assistant", "content": response})
26
- return "", new_history
 
 
 
27
 
28
  with gr.Blocks(title="K2-Think Chat") as demo:
29
  gr.Markdown("# K2-Think Chat App")
30
  chatbot = gr.Chatbot(type="messages", height=500)
31
  msg = gr.Textbox(placeholder="Type your message here...", scale=7)
32
  clear_btn = gr.Button("Clear Chat")
33
- msg.submit(respond, [msg, chatbot], [msg, chatbot])
 
 
 
 
 
 
 
 
34
  clear_btn.click(lambda: None, None, chatbot, queue=False)
 
35
 
36
  if __name__ == "__main__":
37
  demo.launch()
 
12
  device_map="auto",
13
  )
14
 
15
+ def user(message, history):
 
16
  if history is None:
17
  history = []
18
+ history.append({"role": "user", "content": message})
19
+ history.append({"role": "assistant", "content": ""})
20
+ return "", history
21
+
22
+ @spaces.GPU(duration=120)
23
+ def bot(history):
24
+ messages = history[:-1]
25
+ for partial_output in pipe(messages, max_new_tokens=256, stream=True):
26
+ partial_response = partial_output[0]["generated_text"][-1]["content"]
27
+ history[-1]["content"] = partial_response
28
+ yield history
29
 
30
  with gr.Blocks(title="K2-Think Chat") as demo:
31
  gr.Markdown("# K2-Think Chat App")
32
  chatbot = gr.Chatbot(type="messages", height=500)
33
  msg = gr.Textbox(placeholder="Type your message here...", scale=7)
34
  clear_btn = gr.Button("Clear Chat")
35
+ submit_btn = gr.Button("Submit", variant="primary")
36
+ stop_btn = gr.Button("Stop", variant="stop")
37
+
38
+ msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
39
+ bot, chatbot, chatbot
40
+ ).then(lambda: gr.update(value=""), None, msg)
41
+ submit_btn.click(user, [msg, chatbot], [msg, chatbot], queue=False).then(
42
+ bot, chatbot, chatbot
43
+ ).then(lambda: gr.update(value=""), None, msg)
44
  clear_btn.click(lambda: None, None, chatbot, queue=False)
45
+ stop_btn.click(None, None, None, cancels=[bot])
46
 
47
  if __name__ == "__main__":
48
  demo.launch()