Advocate_Life_Style / gradio_interface.py
DocUA's picture
Implement intelligent LLM-based lifestyle profile updates with disk persistence
7e4728d
raw
history blame
8.32 kB
# gradio_interface.py - Gradio interface for the application
import os
import gradio as gr
from lifestyle_app import ExtendedLifestyleJourneyApp
try:
from app_config import GRADIO_CONFIG
except ImportError:
GRADIO_CONFIG = {"theme": "soft", "show_api": False}
def create_gradio_interface():
"""Create Gradio interface"""
app = ExtendedLifestyleJourneyApp()
log_prompts_enabled = os.getenv("LOG_PROMPTS", "false").lower() == "true"
theme_name = GRADIO_CONFIG.get("theme", "soft")
if theme_name.lower() == "soft":
theme = gr.themes.Soft()
elif theme_name.lower() == "default":
theme = gr.themes.Default()
else:
theme = gr.themes.Soft()
with gr.Blocks(
title=GRADIO_CONFIG.get("title", "Lifestyle Journey MVP + Testing Lab"),
theme=theme,
analytics_enabled=False
) as demo:
# Header
if log_prompts_enabled:
gr.Markdown("# πŸ₯ Lifestyle Journey MVP + πŸ§ͺ Testing Lab πŸ“")
gr.Markdown("⚠️ **DEBUG MODE:** LLM prompts and responses are saved to `lifestyle_journey.log`")
else:
gr.Markdown("# πŸ₯ Lifestyle Journey MVP + πŸ§ͺ Testing Lab")
gr.Markdown("Medical chatbot with lifestyle coaching and new patient testing system")
# Tabs
with gr.Tabs():
# Main chat tab
with gr.TabItem("πŸ’¬ Patient Chat", id="main_chat"):
with gr.Row():
with gr.Column(scale=2):
chatbot = gr.Chatbot(
label="πŸ’¬ Conversation with Assistant",
height=400,
show_copy_button=True,
type="messages"
)
with gr.Row():
msg = gr.Textbox(
label="Your message",
placeholder="Type your question...",
scale=4
)
send_btn = gr.Button("πŸ“€ Send", scale=1)
with gr.Row():
clear_btn = gr.Button("πŸ—‘οΈ Clear Chat", scale=1)
end_conversation_btn = gr.Button("🏁 End Conversation", scale=1, variant="secondary")
with gr.Column(scale=1):
status_box = gr.Markdown(
value=app._get_status_info(),
label="πŸ“Š System Status"
)
end_conversation_result = gr.Markdown(value="", visible=False)
# Testing Lab tab
with gr.TabItem("πŸ§ͺ Testing Lab", id="testing_lab"):
gr.Markdown("## πŸ“ Load Test Patient")
with gr.Row():
with gr.Column():
clinical_file = gr.File(
label="πŸ₯ Clinical Background JSON",
file_types=[".json"],
type="filepath"
)
lifestyle_file = gr.File(
label="πŸ’š Lifestyle Profile JSON",
file_types=[".json"],
type="filepath"
)
load_patient_btn = gr.Button("πŸ“‹ Load Patient", variant="primary")
with gr.Column():
load_result = gr.Markdown(value="Select files to load")
# Quick test buttons
gr.Markdown("## ⚑ Quick Testing (Built-in Data)")
with gr.Row():
quick_elderly_btn = gr.Button("πŸ‘΅ Elderly Mary", size="sm")
quick_athlete_btn = gr.Button("πŸƒ Athletic John", size="sm")
quick_pregnant_btn = gr.Button("🀰 Pregnant Sarah", size="sm")
gr.Markdown("## πŸ‘€ Patient Preview")
patient_preview = gr.Markdown(value="No patient loaded")
gr.Markdown("## 🎯 Test Session Management")
with gr.Row():
end_session_notes = gr.Textbox(
label="Session End Notes",
placeholder="Describe testing results...",
lines=3
)
with gr.Column():
end_session_btn = gr.Button("⏹️ End Test Session")
end_session_result = gr.Markdown(value="")
# Test results tab
with gr.TabItem("πŸ“Š Test Results", id="test_results"):
gr.Markdown("## πŸ“ˆ Test Session Analysis")
refresh_results_btn = gr.Button("πŸ”„ Refresh Results")
with gr.Row():
with gr.Column(scale=2):
results_summary = gr.Markdown(value="Click 'Refresh Results'")
with gr.Column(scale=1):
export_btn = gr.Button("πŸ’Ύ Export to CSV")
export_result = gr.Markdown(value="")
gr.Markdown("## πŸ“‹ Recent Test Sessions")
results_table = gr.Dataframe(
headers=["Patient", "Time", "Messages", "Medical", "Lifestyle", "Escalations", "Duration", "Notes"],
datatype=["str", "str", "number", "number", "number", "number", "str", "str"],
label="Session Details",
value=[]
)
# Event handlers for main chat
def handle_message(message, history):
return app.process_message(message, history)
def handle_clear():
return app.reset_session()
send_btn.click(
handle_message,
inputs=[msg, chatbot],
outputs=[chatbot, status_box]
).then(
lambda: "",
outputs=[msg]
)
msg.submit(
handle_message,
inputs=[msg, chatbot],
outputs=[chatbot, status_box]
).then(
lambda: "",
outputs=[msg]
)
clear_btn.click(
handle_clear,
outputs=[chatbot, status_box]
)
# End conversation handler
def handle_end_conversation():
return app.end_conversation_with_profile_update()
end_conversation_btn.click(
handle_end_conversation,
outputs=[chatbot, status_box, end_conversation_result]
)
# Testing Lab handlers
load_patient_btn.click(
app.load_test_patient,
inputs=[clinical_file, lifestyle_file],
outputs=[load_result, patient_preview, chatbot, status_box]
)
# Quick test buttons
quick_elderly_btn.click(
lambda: app.load_quick_test_patient("elderly"),
outputs=[load_result, patient_preview, chatbot, status_box]
)
quick_athlete_btn.click(
lambda: app.load_quick_test_patient("athlete"),
outputs=[load_result, patient_preview, chatbot, status_box]
)
quick_pregnant_btn.click(
lambda: app.load_quick_test_patient("pregnant"),
outputs=[load_result, patient_preview, chatbot, status_box]
)
end_session_btn.click(
app.end_test_session,
inputs=[end_session_notes],
outputs=[end_session_result]
)
# Results handlers
refresh_results_btn.click(
app.get_test_results_summary,
outputs=[results_summary, results_table]
)
export_btn.click(
app.export_test_results,
outputs=[export_result]
)
return demo