UPDATE APP.PY ADDED CHAT_HISTORY FEATURE
Browse files
app.py
CHANGED
@@ -1,32 +1,33 @@
|
|
1 |
-
# app.py
|
2 |
-
import os
|
3 |
import gradio as gr
|
4 |
from pipeline import run_with_chain
|
|
|
5 |
|
6 |
-
def
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
lines=2,
|
17 |
-
label="Ask DailyWellnessAI",
|
18 |
-
placeholder="e.g., How do I improve my posture while working?"
|
19 |
-
),
|
20 |
-
outputs=gr.Textbox(
|
21 |
-
label="DailyWellnessAI Answer"
|
22 |
-
),
|
23 |
-
title="DailyWellnessAI Chat",
|
24 |
-
description=(
|
25 |
-
"Ask a wellness question, or something about DailyWellnessAI (brand). "
|
26 |
-
"If the question is out of scope, you'll get a polite refusal."
|
27 |
-
),
|
28 |
-
allow_flagging="never"
|
29 |
)
|
30 |
-
|
31 |
-
if __name__ == "__main__":
|
32 |
-
interface.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from pipeline import run_with_chain
|
3 |
+
from my_memory_logic import memory, restatement_chain
|
4 |
|
5 |
+
def chat_history_fn(user_input, history):
|
6 |
+
# Convert `history` (list of [user, ai] pairs) into memory messages
|
7 |
+
for user_msg, ai_msg in history:
|
8 |
+
memory.chat_memory.add_user_message(user_msg)
|
9 |
+
memory.chat_memory.add_ai_message(ai_msg)
|
10 |
+
|
11 |
+
# 1) Restate the user question with chat history
|
12 |
+
reformulated_q = restatement_chain.run({
|
13 |
+
"chat_history": memory.chat_memory.messages,
|
14 |
+
"input": user_input
|
15 |
+
})
|
16 |
+
|
17 |
+
# 2) Pass the reformulated question to your pipeline
|
18 |
+
answer = run_with_chain(reformulated_q)
|
19 |
+
|
20 |
+
# 3) Update memory
|
21 |
+
memory.chat_memory.add_user_message(user_input)
|
22 |
+
memory.chat_memory.add_ai_message(answer)
|
23 |
+
|
24 |
+
# 4) Return the new message + updated history for Gradio
|
25 |
+
history.append((user_input, answer))
|
26 |
+
return history, history
|
27 |
|
28 |
+
demo = gr.ChatInterface(
|
29 |
+
fn=chat_history_fn,
|
30 |
+
title="DailyWellnessAI with Memory",
|
31 |
+
description="A chat bot that remembers context using memory + question restatement."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
)
|
33 |
+
demo.launch()
|
|
|
|