Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,42 +12,26 @@ pipe = pipeline(
|
|
| 12 |
device_map="auto",
|
| 13 |
)
|
| 14 |
|
| 15 |
-
|
|
|
|
| 16 |
if history is None:
|
| 17 |
history = []
|
| 18 |
-
history
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 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 |
-
|
| 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()
|