File size: 8,319 Bytes
f55199b
43398e6
 
 
 
 
 
 
 
 
 
 
f55199b
43398e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f55199b
43398e6
 
f55199b
43398e6
 
 
f55199b
43398e6
f55199b
43398e6
f55199b
 
43398e6
 
 
f55199b
43398e6
 
 
 
 
 
 
f55199b
 
43398e6
 
f55199b
43398e6
7e4728d
 
 
43398e6
 
 
 
f55199b
43398e6
7e4728d
 
43398e6
f55199b
43398e6
f55199b
43398e6
 
 
 
 
 
 
 
 
 
 
 
 
 
f55199b
43398e6
 
f55199b
43398e6
f55199b
 
43398e6
 
 
 
 
f55199b
 
43398e6
f55199b
43398e6
 
f55199b
 
43398e6
 
 
f55199b
43398e6
 
f55199b
 
 
43398e6
f55199b
43398e6
 
 
f55199b
43398e6
 
f55199b
43398e6
 
f55199b
43398e6
f55199b
43398e6
f55199b
43398e6
 
 
f55199b
43398e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e4728d
 
 
 
 
 
 
 
 
f55199b
43398e6
 
 
 
 
 
f55199b
43398e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f55199b
43398e6
 
 
 
 
 
 
 
 
 
 
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# 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