UPDATE APP.PY WITH SESSION ID
Browse files
app.py
CHANGED
|
@@ -1,63 +1,35 @@
|
|
| 1 |
-
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
from pipeline import run_with_chain
|
| 6 |
-
|
| 7 |
-
# Suppose 'memory' and 'restatement_chain' come from my_memory_logic.py
|
| 8 |
-
from my_memory_logic import memory, restatement_chain
|
| 9 |
-
|
| 10 |
-
def chat_history_fn(user_input, history):
|
| 11 |
"""
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
Also, handle potential None or invalid strings for user_input/answer
|
| 15 |
-
to avoid Pydantic validation errors.
|
| 16 |
"""
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
})
|
| 26 |
-
|
| 27 |
-
# -- 2) Pass the reformulated question into your pipeline
|
| 28 |
-
answer = run_with_chain(reformulated_q)
|
| 29 |
-
# also sanitize if needed
|
| 30 |
-
if answer is None or not isinstance(answer, str):
|
| 31 |
-
answer = "" if answer is None else str(answer)
|
| 32 |
-
|
| 33 |
-
# -- 3) Add this new user->assistant turn to memory
|
| 34 |
-
memory.chat_memory.add_user_message(user_input)
|
| 35 |
-
memory.chat_memory.add_ai_message(answer)
|
| 36 |
-
|
| 37 |
-
# -- 4) Update Gradio’s 'history' so the UI shows the new turn
|
| 38 |
-
history.append((user_input, answer))
|
| 39 |
-
|
| 40 |
-
# -- 5) Convert the entire 'history' to message dictionaries:
|
| 41 |
-
# [{"role":"user","content":...},{"role":"assistant","content":...},...]
|
| 42 |
message_dicts = []
|
| 43 |
-
for
|
| 44 |
-
|
| 45 |
-
usr_msg = str(usr_msg) if usr_msg else ""
|
| 46 |
-
if not isinstance(ai_msg, str):
|
| 47 |
-
ai_msg = str(ai_msg) if ai_msg else ""
|
| 48 |
-
|
| 49 |
-
message_dicts.append({"role": "user", "content": usr_msg})
|
| 50 |
message_dicts.append({"role": "assistant", "content": ai_msg})
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
)
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
import gradio as gr
|
| 3 |
+
from my_memory_logic import run_with_session_memory
|
| 4 |
|
| 5 |
+
def chat_interface_fn(message, history, session_id):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
"""
|
| 7 |
+
A single-turn chat function for Gradio's ChatInterface.
|
| 8 |
+
We rely on session_id to store the conversation in our my_memory_logic store.
|
|
|
|
|
|
|
| 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 |
+
# 3) Convert into message dicts if ChatInterface is using openai-style messages
|
| 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 message, history: chat_interface_fn(
|
| 29 |
+
message, history, session_id_box.value
|
| 30 |
+
),
|
| 31 |
+
title="DailyWellnessAI (Session-based Memory)",
|
| 32 |
+
description="Ask your questions. The session_id determines your stored memory."
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
demo.launch()
|