frimelle HF Staff commited on
Commit
5fc3290
·
1 Parent(s): 87f27fd

add session id

Browse files
Files changed (1) hide show
  1. app.py +28 -6
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
- # ---- Instantiate a new bot for each user session ----
72
- def create_chatbot():
73
- return SessionChatBot().respond
74
 
75
- demo = gr.ChatInterface(fn=create_chatbot(), title="BoundrAI")
 
 
76
 
77
- if __name__ == "__main__":
78
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()