|
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): |
|
|
|
for user_msg, ai_msg in history: |
|
memory.chat_memory.add_user_message(user_msg) |
|
memory.chat_memory.add_ai_message(ai_msg) |
|
|
|
|
|
reformulated_q = restatement_chain.run({ |
|
"chat_history": memory.chat_memory.messages, |
|
"input": user_input |
|
}) |
|
|
|
|
|
answer = run_with_chain(reformulated_q) |
|
|
|
|
|
memory.chat_memory.add_user_message(user_input) |
|
memory.chat_memory.add_ai_message(answer) |
|
|
|
|
|
|
|
history.append((user_input, answer)) |
|
message_dicts = [] |
|
for user_msg, ai_msg in history: |
|
|
|
message_dicts.append({"role": "user", "content": user_msg}) |
|
|
|
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() |
|
|