File size: 22,589 Bytes
aa8da93
 
 
 
 
 
 
 
e8a8438
bc1e103
42e0d37
0ea0ecf
aa8da93
e4d6847
aa8da93
dc1f94f
aa8da93
 
 
 
 
 
 
e4d6847
aa8da93
 
3bbe322
e4d6847
 
3bbe322
e4d6847
 
3bbe322
7f3af20
 
aa8da93
1a294ac
aa8da93
 
 
 
 
1a294ac
e8a8438
aa8da93
 
 
 
 
 
 
3bbe322
aa8da93
 
 
 
 
bc1e103
aa8da93
 
 
 
 
 
 
 
3bbe322
bc1e103
aa8da93
 
 
1a294ac
aa8da93
3bbe322
 
 
 
 
bc1e103
3bbe322
bc1e103
aa8da93
bc1e103
e8a8438
bc1e103
 
 
 
 
aa8da93
 
 
 
bc1e103
 
 
aa8da93
 
 
 
 
3bbe322
aa8da93
42ed51c
42e0d37
e4d6847
 
42ed51c
dc1f94f
 
42e0d37
bc1e103
 
aa8da93
bc1e103
e4d6847
bc1e103
 
e4d6847
 
3bbe322
bc1e103
e4d6847
3bbe322
bc1e103
 
 
3bbe322
 
 
42ed51c
dc1f94f
 
3bbe322
 
 
e4d6847
 
bc1e103
 
 
e4d6847
 
42ed51c
bc1e103
e4d6847
 
3bbe322
bc1e103
 
 
e4d6847
 
42ed51c
dc1f94f
 
e4d6847
 
3bbe322
aa8da93
bc1e103
 
 
 
 
 
 
 
aa8da93
 
7f3af20
aa8da93
e8a8438
bc1e103
e4d6847
 
42e0d37
 
42ed51c
 
aa8da93
 
 
 
dc1f94f
aa8da93
 
 
 
 
dc1f94f
aa8da93
 
 
dc1f94f
aa8da93
 
 
 
dc1f94f
aa8da93
 
 
 
dc1f94f
aa8da93
1a294ac
 
 
98bf4ff
 
1a294ac
 
3bbe322
98bf4ff
 
3bbe322
1a294ac
98bf4ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a294ac
3bbe322
98bf4ff
 
 
 
 
 
 
 
 
1a294ac
3bbe322
 
 
 
1a294ac
e4d6847
 
 
98bf4ff
 
7f3af20
e4d6847
3bbe322
98bf4ff
3bbe322
e4d6847
98bf4ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4d6847
3bbe322
98bf4ff
 
 
 
 
 
 
 
 
e4d6847
3bbe322
 
 
 
e4d6847
aa8da93
 
 
 
 
 
 
 
e8a8438
 
 
 
 
 
 
 
5af5ad4
aa8da93
 
 
e8a8438
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42e0d37
 
 
 
 
 
 
e4d6847
 
 
 
 
 
 
 
5af5ad4
 
 
 
 
 
 
 
 
 
e8a8438
aa8da93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4d6847
 
bc1e103
e4d6847
 
 
30a3e9e
3bbe322
e4d6847
 
 
 
 
e8a8438
aa8da93
 
e8a8438
 
 
 
 
 
 
7f3af20
 
 
 
1a294ac
e4d6847
 
 
 
 
 
 
 
aa8da93
 
e8a8438
 
 
 
 
aa8da93
 
 
 
 
 
 
 
 
 
 
 
 
42e0d37
 
 
 
 
 
 
 
42ed51c
 
 
 
 
42e0d37
 
 
 
42ed51c
e4d6847
aa8da93
 
 
 
 
 
 
e8a8438
 
 
aa8da93
e8a8438
1a294ac
 
 
 
 
 
 
e4d6847
 
 
 
 
 
 
e8a8438
 
 
 
 
 
 
aa8da93
bc1e103
aa8da93
 
bc1e103
dc1f94f
aa8da93
 
 
 
 
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
import gradio as gr
import requests
import pandas as pd
import base64
from io import BytesIO
import json
import time
import traceback
from PIL import Image

# πŸ”§ CONFIGURE: Your Flask API URL (Public URL)
FLASK_API_URL = "http://3.16.57.66:5000"

def query_database(question, dashboard_mode=False, chart_type=None):
    if not question.strip():
        return "", None, None, "⚠️ Please enter a question.", "", "", "", "", "", ""
    
    try:
        start_time = time.time()
        print("\n" + "="*50)
        print("=== NEW QUERY REQUEST ===")
        print(f"Time: {time.strftime('%Y-%m-%d %H:%M:%S')}")
        print(f"Question: {question}")
        print(f"Chart type: {chart_type}")
        print("="*50)
        
        # Prepare the request
        payload = {
            "question": question,
            "visualize": True
        }
        
        # Add chart_type if specified
        if chart_type and chart_type != "auto":
            payload["viz_type"] = chart_type
        
        print(f"Request URL: {FLASK_API_URL}/ask")
        print(f"Request payload: {payload}")
        
        # Make the request
        print("Sending request...")
        response = requests.post(
            f"{FLASK_API_URL}/ask", 
            json=payload,
            timeout=300
        )
        
        elapsed_time = time.time() - start_time
        print(f"\n=== RESPONSE RECEIVED ===")
        print(f"Response time: {elapsed_time:.2f} seconds")
        print(f"Response status: {response.status_code}")
        print(f"Response headers: {dict(response.headers)}")
        
        # Check if response is empty
        if not response.text:
            error_msg = "Empty response from server"
            print(f"ERROR: {error_msg}")
            return "", None, None, "⚠️ Please enter a question.", "", "", "", "", "", ""
        
        # Try to parse JSON response
        try:
            result = response.json()
            print(f"Parsed JSON successfully: {type(result)}")
        except json.JSONDecodeError as e:
            error_msg = f"Invalid JSON response: {str(e)}"
            print(f"ERROR: {error_msg}")
            print(f"Response text (first 1000 chars): {response.text[:1000]}")
            return "", None, None, "⚠️ Please enter a question.", "", "", "", "", "", ""
        
        # Check HTTP status
        if response.status_code != 200:
            error_msg = result.get("error", f"HTTP {response.status_code}")
            print(f"ERROR: HTTP status {response.status_code}: {error_msg}")
            
            # For 500 errors, show more details
            if response.status_code == 500:
                error_details = f"Server Error (500): {error_msg}\n"
                error_details += f"Response: {json.dumps(result, indent=2)}"
                return "", None, None, "⚠️ Please enter a question.", "", "", "", error_details, "", "", ""
            
            return "", None, None, "⚠️ Please enter a question.", "", "", "", f"HTTP error: {error_msg}", "", "", ""
        
        # Extract data - Updated to match new response structure
        sql = result.get("sql", "")
        chart = result.get("chart", {})  # Changed from "visualization" to "chart"
        chart_generated = result.get("chart_generated", False)  # New field
        row_count = result.get("row_count", 0)
        
        # Extract result data if available (for backward compatibility)
        rows = result.get("result", [])
        
        print(f"\n=== EXTRACTED DATA ===")
        print(f"SQL: {sql[:100] if sql else 'None'}...")
        print(f"Chart generated: {chart_generated}")
        print(f"Row count: {row_count}")
        print(f"Chart data: {chart}")
        
        # Create DataFrame
        df = pd.DataFrame(rows) if rows else pd.DataFrame()
        print(f"DataFrame shape: {df.shape}")
        
        # Process visualization
        chart_image = None
        chart_html = None
        chart_title = ""
        chart_type_result = ""
        chart_error = None
        chart_format = ""
        show_image = True
        show_html = False
        
        # Only process chart if chart_generated is True
        if chart_generated and chart:
            try:
                if isinstance(chart, dict):
                    # Check if it's an error response
                    if "error" in chart:
                        chart_error = chart["error"]
                        print(f"Chart error: {chart_error}")
                    else:
                        # Extract the base64 image string
                        chart_image_b64 = chart.get("image")
                        if chart_image_b64:
                            try:
                                # Handle base64 prefix
                                if chart_image_b64.startswith("data:image/"):
                                    chart_image_b64 = chart_image_b64.split(",")[1]
                                image_bytes = base64.b64decode(chart_image_b64)
                                chart_image = Image.open(BytesIO(image_bytes))
                                print("Chart decoded successfully")
                                chart_format = "png"
                                show_image = True
                                show_html = False
                            except Exception as e:
                                print(f"Error decoding chart: {e}")
                                chart_error = f"Chart decoding error: {str(e)}"
                        
                        # Extract chart metadata
                        chart_title = chart.get("title", "")
                        chart_type_result = chart.get("type", "")
                        chart_format = chart.get("format", chart_format)
                        print(f"Chart type: {chart_type_result}")
                        print(f"Chart title: {chart_title}")
                        print(f"Chart format: {chart_format}")
                elif isinstance(chart, str):
                    # Fallback for backward compatibility
                    try:
                        # Handle base64 prefix
                        if chart.startswith("data:image/"):
                            chart = chart.split(",")[1]
                        image_bytes = base64.b64decode(chart)
                        chart_image = Image.open(BytesIO(image_bytes))
                        print("Chart decoded successfully (fallback)")
                        chart_format = "png"
                        show_image = True
                        show_html = False
                    except Exception as e:
                        print(f"Error decoding chart (fallback): {e}")
                        chart_error = f"Chart decoding error: {str(e)}"
            except Exception as e:
                print(f"Error processing chart: {e}")
                chart_error = f"Chart processing error: {str(e)}"
        elif not chart_generated:
            print("Chart was not generated")
            chart_error = "Chart generation was not successful"
        else:
            print("No chart data available")
            chart_error = "No chart data available"
        
        # Prepare details
        details = f"Request time: {elapsed_time:.2f}s\n"
        details += f"Status code: {response.status_code}\n"
        details += f"Rows returned: {row_count}\n"
        details += f"Chart generated: {chart_generated}\n"
        if chart_type_result:
            details += f"Chart type: {chart_type_result}\n"
        if chart_title:
            details += f"Chart title: {chart_title}\n"
        if chart_format:
            details += f"Chart format: {chart_format}\n"
        details += f"Response size: {len(response.text)} bytes"
        
        print(f"=== REQUEST COMPLETED SUCCESSFULLY ===")
        
        return sql, df, chart_image, chart_html, show_image, show_html, f"βœ… Query completed successfully", details, "Success", chart_type_result, chart_title, chart_format, chart_error
    
    except requests.exceptions.ConnectionError as e:
        error_msg = f"Connection failed: {str(e)}"
        print(f"CONNECTION ERROR: {error_msg}")
        print(f"Traceback: {traceback.format_exc()}")
        return "", None, None, "⚠️ Please enter a question.", "", True, False, f"❌ {error_msg}", "Connection error", "Connection error", "", "", "", ""
    except requests.exceptions.Timeout:
        error_msg = "Request timed out after 300 seconds"
        print(f"TIMEOUT ERROR: {error_msg}")
        return "", None, None, "⚠️ Please enter a question.", "", True, False, f"⏱️ {error_msg}", "Timeout error", "Timeout error", "", "", "", ""
    except requests.exceptions.RequestException as e:
        error_msg = f"Request exception: {str(e)}"
        print(f"REQUEST ERROR: {error_msg}")
        print(f"Traceback: {traceback.format_exc()}")
        return "", None, None, "⚠️ Please enter a question.", "", True, False, f"❌ {error_msg}", "Request error", "Request error", "", "", "", ""
    except Exception as e:
        error_msg = f"Unexpected error: {str(e)}"
        print(f"UNEXPECTED ERROR: {error_msg}")
        print(f"Traceback: {traceback.format_exc()}")
        return "", None, None, "⚠️ Please enter a question.", "", True, False, f"🚨 {error_msg}", f"Error: {str(e)}", "Unexpected error", "", "", "", ""

def check_health():
    try:
        print("Checking API health...")
        print(f"API URL: {FLASK_API_URL}/health")
        
        response = requests.get(f"{FLASK_API_URL}/health", timeout=10)
        
        print(f"Health check response status: {response.status_code}")
        print(f"Health check response headers: {dict(response.headers)}")
        print(f"Health check response text: {response.text}")
        
        if response.status_code == 200:
            try:
                health_data = response.json()
                print(f"Parsed health data: {health_data}")
                
                status = health_data.get('status', 'unknown')
                tables = health_data.get('tables', [])
                model = health_data.get('model', 'unknown')
                data_rows = health_data.get('data_rows', 0)
                
                # Ensure tables is a list before joining
                if isinstance(tables, list):
                    tables_str = ', '.join(tables) if tables else 'None'
                else:
                    tables_str = str(tables)
                
                health_msg = f"βœ… API Status: {status.upper()}\n"
                health_msg += f"πŸ€– Model: {model}\n"
                health_msg += f"πŸ“Š Tables: {tables_str}\n"
                health_msg += f"πŸ“ˆ Data Rows: {data_rows:,}"
                
                return health_msg, "success"
            except json.JSONDecodeError as e:
                error_msg = f"Failed to parse health check response: {str(e)}"
                print(f"JSON PARSE ERROR: {error_msg}")
                return f"❌ {error_msg}", "error"
        else:
            return f"❌ API returned status {response.status_code}\nResponse: {response.text}", "error"
    except requests.exceptions.ConnectionError as e:
        error_msg = f"Connection to API failed: {str(e)}"
        print(f"CONNECTION ERROR: {error_msg}")
        print(f"Traceback: {traceback.format_exc()}")
        return f"❌ {error_msg}", "error"
    except requests.exceptions.Timeout:
        error_msg = "Health check request timed out"
        print(f"TIMEOUT ERROR: {error_msg}")
        return f"❌ {error_msg}", "error"
    except Exception as e:
        error_msg = f"Health check failed: {str(e)}"
        print(f"HEALTH CHECK ERROR: {error_msg}")
        print(f"Traceback: {traceback.format_exc()}")
        return f"❌ {error_msg}", "error"

def get_schema():
    try:
        print("Fetching database schema...")
        print(f"API URL: {FLASK_API_URL}/tables")
        
        response = requests.get(f"{FLASK_API_URL}/tables", timeout=10)
        
        print(f"Schema response status: {response.status_code}")
        print(f"Schema response text: {response.text}")
        
        if response.status_code == 200:
            try:
                tables_data = response.json()
                print(f"Parsed tables data: {tables_data}")
                
                tables = tables_data.get("tables", [])
                
                schema_text = "## Database Schema\n\n"
                for table in tables:
                    schema_text += f"### {table.get('name', 'Unknown')}\n"
                    schema_text += "| Column |\n|--------|\n"
                    for col in table.get('columns', []):
                        schema_text += f"| {col} |\n"
                    schema_text += "\n"
                
                return schema_text, "success"
            except json.JSONDecodeError as e:
                error_msg = f"Failed to parse schema response: {str(e)}"
                print(f"JSON PARSE ERROR: {error_msg}")
                return f"❌ {error_msg}", "error"
        else:
            return f"❌ API returned status {response.status_code}\nResponse: {response.text}", "error"
    except requests.exceptions.ConnectionError as e:
        error_msg = f"Connection to API failed: {str(e)}"
        print(f"CONNECTION ERROR: {error_msg}")
        print(f"Traceback: {traceback.format_exc()}")
        return f"❌ {error_msg}", "error"
    except requests.exceptions.Timeout:
        error_msg = "Schema request timed out"
        print(f"TIMEOUT ERROR: {error_msg}")
        return f"❌ {error_msg}", "error"
    except Exception as e:
        error_msg = f"Failed to fetch schema: {str(e)}"
        print(f"SCHEMA FETCH ERROR: {error_msg}")
        print(f"Traceback: {traceback.format_exc()}")
        return f"❌ {error_msg}", "error"

# 🎨 Theme: Enterprise Dark Blue
theme = gr.themes.Default(
    primary_hue="blue",
    secondary_hue="gray",
    neutral_hue="slate",
    font=["Inter", "sans-serif"]
).set(
    body_background_fill="*neutral_950",
    background_fill_secondary="*neutral_900",
    button_primary_background_fill="*primary_600",
    button_primary_background_fill_hover="*primary_700",
    button_secondary_background_fill="*neutral_800",
    button_secondary_background_fill_hover="*neutral_700",
    block_title_text_color="*primary_400",
    block_label_text_color="*neutral_300",
    input_background_fill="*neutral_800",
    input_border_color="*neutral_700"
)

# πŸš€ UI Layout
with gr.Blocks(theme=theme, title="Enterprise SQL Assistant", css="""
    .example-btn {
        background-color: #1e40af;
        color: white;
        border-radius: 8px;
        padding: 8px 12px;
        margin: 4px;
        font-size: 0.9em;
        border: none;
        cursor: pointer;
        transition: all 0.2s;
    }
    .example-btn:hover {
        background-color: #1d4ed8;
        transform: translateY(-1px);
    }
    .chatbot-container {
        border: 1px solid #334155;
        border-radius: 8px;
        padding: 15px;
        background-color: #1e293b;
        min-height: 200px;
        max-height: 400px;
        overflow-y: auto;
    }
    .status-success {
        color: #10b981;
        font-weight: bold;
    }
    .status-error {
        color: #ef4444;
        font-weight: bold;
    }
    .status-warning {
        color: #f59e0b;
        font-weight: bold;
    }
    .chart-container {
        border: 1px solid #334155;
        border-radius: 8px;
        padding: 10px;
        background-color: #1e293b;
    }
    .chart-metadata {
        background-color: #1e293b;
        border: 1px solid #334155;
        border-radius: 8px;
        padding: 10px;
        margin-bottom: 10px;
    }
    .schema-container {
        background-color: #1e293b;
        border: 1px solid #334155;
        border-radius: 8px;
        padding: 15px;
        max-height: 400px;
        overflow-y: auto;
    }
    /* Fix input text color */
    .gradio-container input[type="text"], 
    .gradio-container textarea {
        color: #f3f4f6 !important;
    }
    /* Fix placeholder color */
    .gradio-container input::placeholder,
    .gradio-container textarea::placeholder {
        color: #9ca3af !important;
    }
""") as demo:
    gr.HTML("""
    <div style="text-align: center; padding: 20px;">
        <h1 style="color: #3B82F6; margin-bottom: 5px;">πŸ“Š Enterprise SQL Assistant</h1>
        <p style="color: #9CA3AF; font-size: 1.1em; margin-top: 0;">
            Ask questions about your data. Get SQL, results, and insights.
        </p>
    </div>
    """)
    
    with gr.Row():
        with gr.Column(scale=1):
            gr.Markdown("### πŸ” Ask a Question")
            question_input = gr.Textbox(
                placeholder="E.g., How many members are there?",
                label="Natural Language Query",
                lines=3
            )
            
            with gr.Row():
                # Removed dashboard_mode as backend no longer supports it
                chart_type_dropdown = gr.Dropdown(
                    label="Chart Type (Optional)",
                    choices=[
                        "auto", "bar", "line", "scatter", "pie", "histogram",
                        "time_series", "correlation"
                    ],
                    value="auto",
                    info="Force a specific chart type"
                )
            
            submit_btn = gr.Button("πŸš€ Generate SQL & Results", variant="primary", size="lg")
            
            gr.Markdown("### πŸ’‘ Example Queries")
            with gr.Row():
                with gr.Column():
                    example1 = gr.Button("How many members are there?", elem_classes=["example-btn"])
                    example2 = gr.Button("What is the total transaction amount?", elem_classes=["example-btn"])
                    example3 = gr.Button("Show members with their account balances", elem_classes=["example-btn"])
                    example4 = gr.Button("Which member has the highest balance?", elem_classes=["example-btn"])
                with gr.Column():
                    example5 = gr.Button("Show transaction trends over time", elem_classes=["example-btn"])
                    example6 = gr.Button("Count of members by status", elem_classes=["example-btn"])
                    example7 = gr.Button("Show distribution of transaction amounts", elem_classes=["example-btn"])
                    example8 = gr.Button("Show correlations between numeric fields", elem_classes=["example-btn"])
            
            with gr.Accordion("Advanced Options", open=False):
                gr.Markdown("### 🩺 System Information")
                with gr.Row():
                    health_btn = gr.Button("Check API Health", variant="secondary", size="sm")
                    schema_btn = gr.Button("Get Database Schema", variant="secondary", size="sm")
                
                health_output = gr.Markdown(label="API Status")
                schema_output = gr.Markdown(label="Database Schema", elem_classes="schema-container")
        
        with gr.Column(scale=2):
            gr.Markdown("### πŸ€– AI Assistant Response")
            chatbot_output = gr.Markdown(
                label="AI Response",
                elem_classes=["chatbot-container"]
            )
            
            with gr.Tabs():
                with gr.Tab("SQL Query"):
                    sql_output = gr.Code(label="", language="sql")
                
                with gr.Tab("Data Results"):
                    results_output = gr.Dataframe(
                        label="Query Results",
                        interactive=False,
                        wrap=True
                    )
                
                with gr.Tab("Visual Insights"):
                    with gr.Row():
                        with gr.Column(scale=3):
                            chart_output = gr.Image(
                                label="Chart",
                                type="pil",
                                height=400,
                                elem_classes="chart-container"
                            )
                            # Add HTML component for fallback
                            html_output = gr.HTML(
                                label="Interactive Chart",
                                visible=False
                            )
                        with gr.Column(scale=1):
                            gr.Markdown("### πŸ“Š Chart Information")
                            chart_type_output = gr.Markdown(label="Chart Type", elem_classes="chart-metadata")
                            chart_title_output = gr.Markdown(label="Chart Title", elem_classes="chart-metadata")
                            chart_format_output = gr.Markdown(label="Chart Format", elem_classes="chart-metadata")
                            chart_error_output = gr.Markdown(label="Chart Error", elem_classes="chart-metadata")
            
            gr.Markdown("### πŸ“Š Request Details")
            request_details = gr.Textbox(label="Request Details", interactive=False, lines=6)
            
            gr.Markdown("### πŸ” Error Details")
            error_details = gr.Textbox(label="Error Details", interactive=False, lines=4)
    
    # Function to handle example query clicks
    def set_example_query(example_text):
        return example_text
    
    # Events
    # Health check button
    health_btn.click(
        fn=check_health,
        inputs=[],
        outputs=[health_output, error_details]
    )
    
    # Schema button
    schema_btn.click(
        fn=get_schema,
        inputs=[],
        outputs=[schema_output, error_details]
    )
    
    # Example query buttons
    for example_btn in [example1, example2, example3, example4, example5, example6, example7, example8]:
        example_btn.click(
            fn=set_example_query,
            inputs=[example_btn],
            outputs=[question_input]
        )
    
    # Submit button - Updated to remove dashboard_mode parameter
    submit_btn.click(
        fn=query_database,
        inputs=[question_input, chart_type_dropdown],  # Removed dashboard_mode
        outputs=[sql_output, results_output, chart_output, html_output, gr.Number(visible=False, value=1), gr.Number(visible=False, value=0), chatbot_output, request_details, error_details, chart_type_output, chart_title_output, chart_format_output, chart_error_output]
    )

# Launch
if __name__ == "__main__":
    demo.launch()