akhaliq HF Staff commited on
Commit
4034e3f
·
verified ·
1 Parent(s): 218ff72

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -27
app.py CHANGED
@@ -12,42 +12,26 @@ pipe = pipeline(
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
- # Store the event handler to cancel it later
39
- bot_event = msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
40
- bot, chatbot, chatbot
41
- ).then(lambda: gr.update(value=""), None, msg)
42
-
43
- submit_event = submit_btn.click(user, [msg, chatbot], [msg, chatbot], queue=False).then(
44
- bot, chatbot, chatbot
45
- ).then(lambda: gr.update(value=""), None, msg)
46
-
47
  clear_btn.click(lambda: None, None, chatbot, queue=False)
48
-
49
- # Cancel the events instead of trying to cancel the function directly
50
- stop_btn.click(None, None, None, cancels=[bot_event, submit_event])
51
 
52
  if __name__ == "__main__":
53
  demo.launch()
 
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=32768,
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()