Update app.py
Browse files
app.py
CHANGED
@@ -4,27 +4,48 @@ from my_memory_logic import run_with_session_memory
|
|
4 |
|
5 |
def chat_interface_fn(message, history, session_id):
|
6 |
"""
|
7 |
-
|
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 |
-
#
|
|
|
|
|
|
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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="
|
28 |
)
|
29 |
|
30 |
demo.launch()
|
|
|
4 |
|
5 |
def chat_interface_fn(message, history, session_id):
|
6 |
"""
|
7 |
+
Multi-turn chat function for Gradio's ChatInterface.
|
8 |
'session_id' is used to store conversation across turns.
|
9 |
"""
|
10 |
+
# 1) 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 |
+
# 3) Convert into message dicts for ChatInterface
|
17 |
message_dicts = []
|
18 |
for user_msg, ai_msg in history:
|
19 |
message_dicts.append({"role": "user", "content": user_msg})
|
20 |
message_dicts.append({"role": "assistant", "content": ai_msg})
|
21 |
return message_dicts, history
|
22 |
|
23 |
+
# Define custom CSS to align messages
|
24 |
+
my_chat_css = """
|
25 |
+
/* Center the container horizontally so there's some margin */
|
26 |
+
.gradio-container {
|
27 |
+
margin: auto;
|
28 |
+
}
|
29 |
+
|
30 |
+
/* Right-align user messages */
|
31 |
+
.user .wrap {
|
32 |
+
text-align: right !important;
|
33 |
+
}
|
34 |
+
|
35 |
+
/* Left-align assistant messages */
|
36 |
+
.assistant .wrap {
|
37 |
+
text-align: left !important;
|
38 |
+
}
|
39 |
+
"""
|
40 |
+
|
41 |
+
with gr.Blocks(css=my_chat_css) as demo:
|
42 |
+
gr.Markdown("### DailyWellnessAI (User on right, Assistant on left)")
|
43 |
session_id_box = gr.Textbox(label="Session ID", value="abc123", interactive=True)
|
44 |
|
45 |
chat_interface = gr.ChatInterface(
|
46 |
fn=lambda msg, hist: chat_interface_fn(msg, hist, session_id_box.value),
|
47 |
title="DailyWellnessAI (Session-based Memory)",
|
48 |
+
description="Ask your questions. The session_id determines your stored memory."
|
49 |
)
|
50 |
|
51 |
demo.launch()
|