feat: add conversation history support to chat service and update Gradio UI for interactive sessions
491caa2 | import logging | |
| # Configure central logger before other imports to capture initialization logs cleanly | |
| logging.basicConfig( | |
| level=logging.INFO, | |
| format="%(asctime)s - %(name)s - %(levelname)s - %(message)s" | |
| ) | |
| logger = logging.getLogger(__name__) | |
| logger.info("Initializing RAG Knowledge Assistant application components...") | |
| import gradio as gr | |
| from db.bootstrap import init_database | |
| # Initialize database schema/collections if not already done | |
| init_database() | |
| from services.ui_handlers import ( | |
| add_user_message, | |
| bot_response, | |
| clear_session, | |
| run_ingestion | |
| ) | |
| # Gradio Interface Construction | |
| logger.info("Creating Gradio UI structure...") | |
| theme = gr.themes.Soft( | |
| primary_hue="indigo", | |
| secondary_hue="blue", | |
| neutral_hue="slate", | |
| font=[gr.themes.GoogleFont("Outfit"), "sans-serif"] | |
| ) | |
| with gr.Blocks(title="Shop Knowledge Assistant") as demo: | |
| gr.Markdown( | |
| """ | |
| # π€ Shop RAG Knowledge Assistant | |
| *Ask questions about store policies and toy products with high-performance vector search and conversation history.* | |
| """ | |
| ) | |
| with gr.Row(): | |
| # Left Sidebar Column: Configuration & Controls | |
| with gr.Column(scale=1): | |
| gr.Markdown("### βοΈ Settings & Actions") | |
| doc_type = gr.Dropdown( | |
| choices=["Auto (AI Router)", "Policy", "Product", "No Filter"], | |
| value="Auto (AI Router)", | |
| label="Filter Mode", | |
| visible=False | |
| ) | |
| ingest_btn = gr.Button("π Re-Ingest Data", variant="secondary") | |
| ingest_status = gr.Textbox( | |
| label="Ingestion Status", | |
| value="Ready", | |
| interactive=False | |
| ) | |
| clear_btn = gr.Button("ποΈ Clear History", variant="stop") | |
| gr.Markdown( | |
| """ | |
| ### π‘ Try Asking | |
| - *What is the return policy for toy products?* | |
| - *Do you have any outdoor adventure sets?* | |
| - *Compare the Camping Adventure and the Horse Stable.* | |
| """ | |
| ) | |
| # Right Chat Area Column: Full Interactive Chatbot | |
| with gr.Column(scale=3): | |
| chatbot = gr.Chatbot( | |
| height=520, | |
| label="Maya - Personal Play Consultant", | |
| placeholder="Hi, I'm Maya! Ask me anything about our toys or store policies. I will remember our chat!" | |
| ) | |
| with gr.Row(): | |
| q = gr.Textbox( | |
| placeholder="Ask Maya a question...", | |
| show_label=False, | |
| scale=4, | |
| container=False | |
| ) | |
| btn = gr.Button("Send π", variant="primary", scale=1) | |
| # Event handlers binding | |
| btn.click( | |
| add_user_message, | |
| inputs=[q, chatbot], | |
| outputs=[q, chatbot], | |
| queue=False | |
| ).then( | |
| bot_response, | |
| inputs=[doc_type, chatbot], | |
| outputs=[chatbot] | |
| ) | |
| q.submit( | |
| add_user_message, | |
| inputs=[q, chatbot], | |
| outputs=[q, chatbot], | |
| queue=False | |
| ).then( | |
| bot_response, | |
| inputs=[doc_type, chatbot], | |
| outputs=[chatbot] | |
| ) | |
| clear_btn.click( | |
| clear_session, | |
| outputs=[chatbot], | |
| queue=False | |
| ) | |
| ingest_btn.click( | |
| run_ingestion, | |
| outputs=[ingest_status] | |
| ) | |
| if __name__ == "__main__": | |
| logger.info("Launching Gradio UI server...") | |
| demo.launch( | |
| theme=theme, | |
| server_name="0.0.0.0", | |
| server_port=7860 | |
| ) |