Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,9 +15,16 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 15 |
trust_remote_code=True
|
| 16 |
)
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
prompt = tokenizer.apply_chat_template(
|
| 23 |
messages,
|
|
@@ -42,16 +49,28 @@ def chat(user_input, history):
|
|
| 42 |
|
| 43 |
messages.append({"role": "assistant", "content": response})
|
| 44 |
|
| 45 |
-
|
|
|
|
|
|
|
| 46 |
|
| 47 |
with gr.Blocks() as demo:
|
| 48 |
gr.Markdown("## π€ Qwen Chatbot")
|
| 49 |
|
| 50 |
-
chatbot = gr.Chatbot(
|
| 51 |
msg = gr.Textbox(label="Your message", autofocus=True)
|
|
|
|
| 52 |
clear = gr.Button("Clear")
|
| 53 |
|
| 54 |
-
msg.submit(
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
demo.launch()
|
|
|
|
| 15 |
trust_remote_code=True
|
| 16 |
)
|
| 17 |
|
| 18 |
+
SYSTEM_PROMPT = {
|
| 19 |
+
"role": "system",
|
| 20 |
+
"content": "You are a helpful AI assistant."
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
def chat(user_input, chat_history, message_state):
|
| 24 |
+
# message_state stores Qwen-style messages
|
| 25 |
+
messages = message_state or [SYSTEM_PROMPT]
|
| 26 |
+
|
| 27 |
+
messages.append({"role": "user", "content": user_input})
|
| 28 |
|
| 29 |
prompt = tokenizer.apply_chat_template(
|
| 30 |
messages,
|
|
|
|
| 49 |
|
| 50 |
messages.append({"role": "assistant", "content": response})
|
| 51 |
|
| 52 |
+
chat_history.append((user_input, response))
|
| 53 |
+
|
| 54 |
+
return chat_history, messages, ""
|
| 55 |
|
| 56 |
with gr.Blocks() as demo:
|
| 57 |
gr.Markdown("## π€ Qwen Chatbot")
|
| 58 |
|
| 59 |
+
chatbot = gr.Chatbot()
|
| 60 |
msg = gr.Textbox(label="Your message", autofocus=True)
|
| 61 |
+
state = gr.State([]) # stores Qwen messages
|
| 62 |
clear = gr.Button("Clear")
|
| 63 |
|
| 64 |
+
msg.submit(
|
| 65 |
+
chat,
|
| 66 |
+
inputs=[msg, chatbot, state],
|
| 67 |
+
outputs=[chatbot, state, msg]
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
clear.click(
|
| 71 |
+
lambda: ([], []),
|
| 72 |
+
None,
|
| 73 |
+
outputs=[chatbot, state]
|
| 74 |
+
)
|
| 75 |
|
| 76 |
demo.launch()
|