add session id
Browse files
app.py
CHANGED
@@ -68,11 +68,33 @@ class SessionChatBot:
|
|
68 |
# Save log after full response
|
69 |
self.append_to_session_log(message, response)
|
70 |
|
71 |
-
# ----
|
72 |
-
|
73 |
-
|
74 |
|
75 |
-
|
|
|
|
|
76 |
|
77 |
-
|
78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
# Save log after full response
|
69 |
self.append_to_session_log(message, response)
|
70 |
|
71 |
+
# ---- Gradio Interface with per-user session support ----
|
72 |
+
with gr.Blocks() as demo:
|
73 |
+
chatbot_state = gr.State() # each user gets a different SessionChatBot
|
74 |
|
75 |
+
chat = gr.Chatbot()
|
76 |
+
msg = gr.Textbox(label="Message", placeholder="Say something...")
|
77 |
+
send_btn = gr.Button("Send")
|
78 |
|
79 |
+
# Create a chatbot instance per user when interface loads
|
80 |
+
def init_bot():
|
81 |
+
return SessionChatBot()
|
82 |
+
|
83 |
+
demo.load(init_bot, outputs=chatbot_state)
|
84 |
+
|
85 |
+
def handle_message(message, history, bot: SessionChatBot):
|
86 |
+
return bot.respond(message, history)
|
87 |
+
|
88 |
+
send_btn.click(
|
89 |
+
handle_message,
|
90 |
+
inputs=[msg, chat, chatbot_state],
|
91 |
+
outputs=chat
|
92 |
+
)
|
93 |
+
|
94 |
+
msg.submit(
|
95 |
+
handle_message,
|
96 |
+
inputs=[msg, chat, chatbot_state],
|
97 |
+
outputs=chat
|
98 |
+
)
|
99 |
+
|
100 |
+
demo.launch()
|