Sebunya commited on
Commit
ea0bc41
·
verified ·
1 Parent(s): 8dbf90d
Files changed (1) hide show
  1. app.py +30 -26
app.py CHANGED
@@ -254,32 +254,36 @@ def chat_with_session(message, history, session_id=None):
254
  if session_id is None:
255
  session_id = str(uuid.uuid4())
256
  answer = get_context_and_answer(message, history, session_id)
257
- return answer, session_id # Return answer and session_id for state tracking
258
 
259
- if __name__ == "__main__":
260
- # Gradio Blocks layout for combining ChatInterface + Clear Button
261
- with gr.Blocks() as demo:
262
- session_id_state = gr.State(str(uuid.uuid4()))
263
-
264
- chat_interface = gr.ChatInterface(
265
- fn=chat_with_session,
266
- additional_inputs=[session_id_state],
267
- title="ASKXENO",
268
- description="Ask anything about XENO's financial services.",
269
- theme="soft",
270
- examples=[
271
- ["How do I open a XENO account?", None],
272
- ["What are the fees?", None],
273
- ["Tell me about investment options", None]
274
- ]
275
- )
276
 
277
- clear_button = gr.Button("Clear Session")
278
 
279
- # When clear button is clicked, reset session_id state (clears conversation context)
280
- clear_button.click(
281
- fn=lambda: str(uuid.uuid4()),
282
- outputs=[session_id_state]
283
- )
284
-
285
- demo.launch(share=False, server_name="0.0.0.0", server_port=7860)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
254
  if session_id is None:
255
  session_id = str(uuid.uuid4())
256
  answer = get_context_and_answer(message, history, session_id)
 
257
 
258
+ if history is None:
259
+ history = []
260
+ # Append user message and assistant answer to chat history as dicts:
261
+ history.append({"role": "user", "content": message})
262
+ history.append({"role": "assistant", "content": answer})
 
 
 
 
 
 
 
 
 
 
 
 
263
 
264
+ return history, session_id
265
 
266
+ if __name__ == "__main__":
267
+ session_id_state = gr.State(value=str(uuid.uuid4()))
268
+
269
+ iface = gr.ChatInterface(
270
+ fn=chat_with_session,
271
+ additional_inputs=[session_id_state],
272
+ title="ASKXENO",
273
+ description="Ask anything about XENO's financial services.",
274
+ theme="soft",
275
+ type="messages", # Use 'messages' format (dict with role/content)
276
+ examples=[
277
+ ["How do I open a XENO account?", None],
278
+ ["What are the fees?", None],
279
+ ["Tell me about investment options", None]
280
+ ]
281
+ )
282
+
283
+ # Reset session ID on clear button click:
284
+ iface.clear_btn.click(
285
+ fn=lambda: str(uuid.uuid4()),
286
+ outputs=[session_id_state]
287
+ )
288
+
289
+ iface.launch(share=False, server_name="0.0.0.0", server_port=7860)