Update app.py
Browse files
app.py
CHANGED
|
@@ -4,32 +4,27 @@ from my_memory_logic import run_with_session_memory
|
|
| 4 |
|
| 5 |
def chat_interface_fn(message, history, session_id):
|
| 6 |
"""
|
| 7 |
-
|
| 8 |
-
|
| 9 |
"""
|
| 10 |
-
# 1) We call run_with_session_memory with user message and session_id
|
| 11 |
answer = run_with_session_memory(message, session_id)
|
| 12 |
-
|
| 13 |
-
# 2) Append the turn to the 'history' so Gradio UI displays it
|
| 14 |
history.append((message, answer))
|
| 15 |
-
|
| 16 |
-
#
|
| 17 |
-
# or we can just return a single string. Let's do openai-style message dicts:
|
| 18 |
message_dicts = []
|
| 19 |
for user_msg, ai_msg in history:
|
| 20 |
message_dicts.append({"role": "user", "content": user_msg})
|
| 21 |
message_dicts.append({"role": "assistant", "content": ai_msg})
|
| 22 |
return message_dicts, history
|
| 23 |
|
| 24 |
-
# We'll define a small Gradio Blocks or ChatInterface
|
| 25 |
with gr.Blocks() as demo:
|
| 26 |
session_id_box = gr.Textbox(label="Session ID", value="abc123", interactive=True)
|
|
|
|
| 27 |
chat_interface = gr.ChatInterface(
|
| 28 |
-
fn=lambda
|
| 29 |
-
message, history, session_id_box.value
|
| 30 |
-
),
|
| 31 |
title="DailyWellnessAI (Session-based Memory)",
|
| 32 |
-
description="
|
| 33 |
)
|
| 34 |
|
| 35 |
demo.launch()
|
|
|
|
| 4 |
|
| 5 |
def chat_interface_fn(message, history, session_id):
|
| 6 |
"""
|
| 7 |
+
Single-turn function for Gradio's ChatInterface.
|
| 8 |
+
'session_id' is used to store conversation across turns.
|
| 9 |
"""
|
|
|
|
| 10 |
answer = run_with_session_memory(message, session_id)
|
| 11 |
+
# Update local 'history' for the UI
|
|
|
|
| 12 |
history.append((message, answer))
|
| 13 |
+
|
| 14 |
+
# Convert to openai-style message dicts
|
|
|
|
| 15 |
message_dicts = []
|
| 16 |
for user_msg, ai_msg in history:
|
| 17 |
message_dicts.append({"role": "user", "content": user_msg})
|
| 18 |
message_dicts.append({"role": "assistant", "content": ai_msg})
|
| 19 |
return message_dicts, history
|
| 20 |
|
|
|
|
| 21 |
with gr.Blocks() as demo:
|
| 22 |
session_id_box = gr.Textbox(label="Session ID", value="abc123", interactive=True)
|
| 23 |
+
|
| 24 |
chat_interface = gr.ChatInterface(
|
| 25 |
+
fn=lambda msg, hist: chat_interface_fn(msg, hist, session_id_box.value),
|
|
|
|
|
|
|
| 26 |
title="DailyWellnessAI (Session-based Memory)",
|
| 27 |
+
description="Multi-turn chat using session ID and RunnableWithMessageHistory."
|
| 28 |
)
|
| 29 |
|
| 30 |
demo.launch()
|