File size: 1,476 Bytes
f36f021 87cd864 a8d22a4 5a5fa80 e4ef93a 3e00622 be50d0f e4ef93a c450e62 5a5fa80 3d23e89 c450e62 5a5fa80 c450e62 3d23e89 be50d0f 8972636 8a646af 3d23e89 8a646af 3d23e89 8a646af 3d23e89 8a646af 8972636 8a646af 87cd864 2cc6ea2 87cd864 5a5fa80 87cd864 8a646af 87cd864 c450e62 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
import gradio as gr
from my_memory_logic import run_with_session_memory
def chat_interface_fn(message, history):
"""
Multi-turn chat function for Gradio's ChatInterface.
Returns strings for both message and history to match Gradio's expected format.
"""
# Initialize history if None
history = history or []
# Get answer from the session-based memory pipeline
try:
answer = run_with_session_memory(message, session_id_box.value)
except Exception as e:
print(f"Error in run_with_session_memory: {str(e)}")
answer = "I apologize, but I encountered an error processing your request."
# Return strings directly - this is what Gradio's ChatInterface expects
return str(answer)
# Custom CSS for chat interface
my_chat_css = """
.gradio-container {
margin: auto;
}
.user .wrap {
text-align: right !important;
}
.assistant .wrap {
text-align: left !important;
}
"""
# Set up Gradio interface
with gr.Blocks(css=my_chat_css) as demo:
gr.Markdown("### DailyWellnessAI (User on right, Assistant on left)")
session_id_box = gr.Textbox(label="Session ID", value="abc123", interactive=True)
chat_interface = gr.ChatInterface(
fn=chat_interface_fn,
title="DailyWellnessAI (Session-based Memory)",
description="Ask your questions. The session_id determines your stored memory."
)
# Launch the Gradio interface with sharing enabled
demo.launch(share=True) |