Chatbot2 / app.py
Phoenix21's picture
Update app.py
c585131 verified
raw
history blame
1.48 kB
import gradio as gr
from pipeline import run_with_chain
from my_memory_logic import memory, restatement_chain
def chat_history_fn(user_input, history):
# Convert `history` (list of [user, ai] pairs) into memory messages
for user_msg, ai_msg in history:
memory.chat_memory.add_user_message(user_msg)
memory.chat_memory.add_ai_message(ai_msg)
# 1) Restate the user question with chat history
reformulated_q = restatement_chain.run({
"chat_history": memory.chat_memory.messages,
"input": user_input
})
# 2) Pass the reformulated question to your pipeline
answer = run_with_chain(reformulated_q)
# 3) Update memory
memory.chat_memory.add_user_message(user_input)
memory.chat_memory.add_ai_message(answer)
# 4) Convert the updated history to a list of message dicts.
# This is what Gradio's ChatInterface expects (instead of returning a tuple).
history.append((user_input, answer))
message_dicts = []
for user_msg, ai_msg in history:
# user turn
message_dicts.append({"role": "user", "content": user_msg})
# AI turn
message_dicts.append({"role": "assistant", "content": ai_msg})
return message_dicts
demo = gr.ChatInterface(
fn=chat_history_fn,
title="DailyWellnessAI with Memory",
description="A chat bot that remembers context using memory + question restatement."
)
if __name__ == "__main__":
demo.launch()