ST-THOMAS-OF-AQUINAS commited on
Commit
98ba33d
·
verified ·
1 Parent(s): 39976b4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -7
app.py CHANGED
@@ -15,9 +15,16 @@ model = AutoModelForCausalLM.from_pretrained(
15
  trust_remote_code=True
16
  )
17
 
18
- def chat(user_input, history):
19
- # history is already in messages format
20
- messages = history + [{"role": "user", "content": user_input}]
 
 
 
 
 
 
 
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
- return messages, ""
 
 
46
 
47
  with gr.Blocks() as demo:
48
  gr.Markdown("## 🤖 Qwen Chatbot")
49
 
50
- chatbot = gr.Chatbot(type="messages")
51
  msg = gr.Textbox(label="Your message", autofocus=True)
 
52
  clear = gr.Button("Clear")
53
 
54
- msg.submit(chat, [msg, chatbot], [chatbot, msg])
55
- clear.click(lambda: [], None, chatbot)
 
 
 
 
 
 
 
 
 
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()