File size: 30,351 Bytes
a48d292
 
 
 
 
 
 
138d311
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a48d292
 
f81a8b5
a48d292
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cd4ad46
a48d292
 
 
1c8bc64
a48d292
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60b5b2b
 
 
 
 
 
 
cd4ad46
60b5b2b
 
 
1c8bc64
a48d292
60b5b2b
a48d292
 
1c8bc64
a48d292
ec32a5b
a48d292
 
 
 
 
 
 
8b42754
a48d292
 
 
 
 
354270d
 
 
 
 
a48d292
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b42754
a48d292
 
 
8b42754
a48d292
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b42754
a48d292
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c8bc64
8b42754
a48d292
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
354270d
 
 
a48d292
354270d
 
 
 
 
a48d292
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f81a8b5
a48d292
 
 
 
 
 
 
 
 
 
 
 
 
 
1c8bc64
a48d292
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c8bc64
a48d292
 
 
 
 
 
 
 
 
 
 
 
 
 
1c8bc64
a48d292
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c8bc64
a48d292
 
 
 
 
 
 
 
1c8bc64
a48d292
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c8bc64
a48d292
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c8bc64
a48d292
 
 
 
 
 
 
 
 
 
 
 
 
cd4ad46
a48d292
 
 
 
 
cd4ad46
 
a48d292
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cd4ad46
 
a48d292
cd4ad46
 
a48d292
 
1c8bc64
a48d292
 
 
 
 
 
 
 
 
 
 
 
cd4ad46
a48d292
cd4ad46
 
a48d292
 
 
 
 
 
 
 
 
 
1c8bc64
a48d292
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60b5b2b
25e4358
a48d292
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cd4ad46
 
 
a48d292
cd4ad46
f81a8b5
a48d292
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cd4ad46
 
 
 
 
 
 
a48d292
 
f81a8b5
a48d292
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76210f8
25e4358
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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
"""Gradio application wiring for the Business Intelligence dashboard."""

from __future__ import annotations

import tempfile
from typing import Any, Dict, Iterable, List, Optional, Tuple

# Monkey-patch to fix gradio_client TypeError bug with boolean additionalProperties
# This must be done BEFORE importing gradio
import gradio_client.utils as _gc_utils

_original_json_schema_to_python_type = _gc_utils._json_schema_to_python_type

def _patched_json_schema_to_python_type(schema, defs=None):
    """Patched version that handles boolean schema values."""
    # Handle boolean schemas (e.g., additionalProperties: true/false)
    if isinstance(schema, bool):
        return "Any" if schema else "None"
    return _original_json_schema_to_python_type(schema, defs)

_gc_utils._json_schema_to_python_type = _patched_json_schema_to_python_type

import gradio as gr
import pandas as pd
import matplotlib.figure as mpl_fig

from data_processor import (
    DatasetBundle,
    dataset_overview,
    dataset_preview,
    filter_dataframe,
    filter_metadata,
    load_dataset,
    load_sample_dataset,
    missing_value_report,
    numeric_summary,
    categorical_summary,
    correlation_matrix,
    sample_dataset_options,
)
from insights import (
    detect_anomalies,
    detect_trend,
    get_default_insight_columns,
    top_bottom_performers,
)
from visualizations import (
    create_category_plot,
    create_correlation_heatmap,
    create_distribution_plot,
    create_scatter_plot,
    create_time_series_plot,
    figure_to_png_bytes,
)





def _format_overview_text(info: Dict[str, Any], source_name: str) -> str:
    """Render dataset information as Markdown."""
    lines = [
        f"**Source:** {source_name}",
        f"- Rows: {info['Rows']}",
        f"- Columns: {info['Columns']}",
        f"- Memory Usage: {info['Memory Usage (MB)']} MB",
    ]
    return "\n".join(lines)


def _empty_dataframe(message: str = "No data available") -> pd.DataFrame:
    """Return a placeholder DataFrame for empty displays."""
    return pd.DataFrame({"status": [message]})



DEFAULT_STATE = {
    "dataframe": None,
    "filtered_df": None,
    "column_types": None,
    "filter_meta": None,
    "source_name": None,
    "current_figure": None,
}


def _ensure_state(state) -> Dict[str, Any]:
    """Guarantee a dictionary-based state object."""
    return state or DEFAULT_STATE.copy()


def _current_dataframe(state, filtered: bool = True) -> pd.DataFrame:
    """Return the filtered or raw dataframe from state."""
    state = _ensure_state(state)
    key = "filtered_df" if filtered else "dataframe"
    df = state.get(key)
    if isinstance(df, pd.DataFrame):
        return df
    raise ValueError("Please upload a dataset before performing this action.")


def _finalize_dataset_load(bundle, state):
    """Populate shared outputs after a dataset is loaded."""
    df = bundle.dataframe
    state = {
        "dataframe": df,
        "filtered_df": df,
        "column_types": {
            "numeric": bundle.column_types.numeric,
            "categorical": bundle.column_types.categorical,
            "datetime": bundle.column_types.datetime,
        },
        "filter_meta": filter_metadata(df, bundle.column_types),
        "source_name": bundle.source_name,
    }

    overview = dataset_overview(df)
    preview = dataset_preview(df)

    status = f"✅ Loaded '{bundle.source_name}' with {df.shape[0]} rows and {df.shape[1]} columns."
    info_text = _format_overview_text(overview["info"], bundle.source_name)
    dtypes_df = overview["dtypes"]
    head_df = preview["head"]
    tail_df = preview["tail"]
    filter_preview = head_df
    row_count = f"Rows displayed: {len(df)}"

    return state, status, info_text, dtypes_df, head_df, tail_df, filter_preview, row_count


def _handle_file_upload(file, state):
    """Load a dataset from the uploaded file."""
    state = _ensure_state(state)
    try:
        bundle = load_dataset(file)
    except ValueError as exc:
        return (
            state,
            f"❌ {exc}",
            "No dataset loaded.",
            _empty_dataframe(),
            _empty_dataframe(),
            _empty_dataframe(),
            _empty_dataframe(),
            "Rows displayed: 0",
        )

    return _finalize_dataset_load(bundle, state)


def _handle_sample_dataset(selection: Optional[str], state):
    """Load one of the bundled sample datasets."""
    state = _ensure_state(state)
    if not selection:
        message = "Please choose a sample dataset before loading."
        empty = _empty_dataframe(message)
        return state, f"⚠️ {message}", "No dataset loaded.", empty, empty, empty, empty, "Rows displayed: 0"

    try:
        bundle = load_sample_dataset(selection)
    except ValueError as exc:
        empty = _empty_dataframe(str(exc))
        return state, f"❌ {exc}", "No dataset loaded.", empty, empty, empty, empty, "Rows displayed: 0"

    return _finalize_dataset_load(bundle, state)


def _populate_column_options(
    state,
):
    """Populate dropdown choices based on the uploaded dataset."""
    state = _ensure_state(state)
    column_types = state.get("column_types")
    if not column_types:
        empty_dropdown = gr.update(choices=[], value=None, interactive=False, visible=True)
        hidden_checkbox = gr.update(choices=[], value=[], visible=False, interactive=False)
        return (
            empty_dropdown,
            empty_dropdown,
            hidden_checkbox,
            empty_dropdown,
            empty_dropdown,
            empty_dropdown,
            empty_dropdown,
            empty_dropdown,
            empty_dropdown,
            empty_dropdown,
            empty_dropdown,
            empty_dropdown,
            empty_dropdown,
            empty_dropdown,
            empty_dropdown,
            empty_dropdown,
        )

    numeric = list(column_types["numeric"])
    categorical = list(column_types["categorical"])
    datetime_cols = list(column_types["datetime"])
    all_columns = list(state["dataframe"].columns)
    
    defaults = {
        "numeric": numeric[0] if numeric else None,
        "datetime": datetime_cols[0] if datetime_cols else None,
    }

    def dropdown(values: Iterable[str], default: Optional[str] = None):
        choices = list(values)
        value = default if default in choices else None
        return gr.update(
            choices=choices,
            value=value,
            interactive=bool(choices),
            visible=True,
        )

    return (
        dropdown(numeric),        # numeric filter column
        dropdown(datetime_cols),  # date filter column
        gr.update(choices=[], value=[], visible=False, interactive=False),  # categorical values reset
        dropdown(categorical),    # categorical filter column
        dropdown(all_columns, defaults.get("datetime")),    # time series date
        dropdown(numeric, defaults.get("numeric")),         # time series value
        dropdown(numeric),        # distribution numeric
        dropdown(categorical),    # category column
        dropdown(numeric),        # category value
        dropdown(numeric),        # scatter x
        dropdown(numeric),        # scatter y
        gr.update(choices=all_columns, value=None, interactive=bool(all_columns), visible=True),  # scatter color
        dropdown(numeric, defaults.get("numeric")),         # insight numeric
        dropdown(datetime_cols, defaults.get("datetime")),  # insight datetime
        dropdown(numeric, defaults.get("numeric")),         # trend value
        dropdown(numeric, defaults.get("numeric")),         # anomaly column
    )


def _update_numeric_inputs(column: Optional[str], state) -> Tuple[Any, Any]:
    """Update numeric min/max inputs when a column is selected."""
    state = _ensure_state(state)
    hidden = gr.update(visible=False, value=None)
    if not column or "filter_meta" not in state:
        return hidden, hidden
    meta = state["filter_meta"]["numeric"].get(column)
    if not meta:
        return hidden, hidden
    minimum = float(meta["min"])
    maximum = float(meta["max"])
    return (
        gr.update(value=minimum, visible=True, interactive=True, label=f"Min ({column})"),
        gr.update(value=maximum, visible=True, interactive=True, label=f"Max ({column})"),
    )


def _update_categorical_values(column: Optional[str], state):
    """Populate categorical value options for filtering."""
    state = _ensure_state(state)
    if not column or "filter_meta" not in state:
        return gr.update(visible=False)
    values = state["filter_meta"]["categorical"].get(column, [])
    return gr.update(
        choices=values,
        value=values[: min(10, len(values))],
        visible=bool(values),
        interactive=bool(values),
        label=f"Values to include ({column})",
    )


def _update_date_bounds(column: Optional[str], state) -> Tuple[Any, Any]:
    """Populate date inputs when a date column is selected."""
    state = _ensure_state(state)
    if not column or "filter_meta" not in state:
        hidden = gr.update(visible=False, value=None)
        return hidden, hidden
    meta = state["filter_meta"]["datetime"].get(column)
    if not meta:
        hidden = gr.update(visible=False, value=None)
        return hidden, hidden
    start = str(meta["min"])
    end = str(meta["max"])
    return (
        gr.update(value=start, visible=True, label=f"Start date ({column})"),
        gr.update(value=end, visible=True, label=f"End date ({column})"),
    )


def _apply_filters(
    state,
    numeric_column: Optional[str],
    numeric_min: Optional[float],
    numeric_max: Optional[float],
    categorical_column: Optional[str],
    categorical_values: Optional[List[str]],
    date_column: Optional[str],
    start_date: Optional[str],
    end_date: Optional[str],
) -> Tuple[Dict[str, Any], pd.DataFrame, str]:
    """Filter the dataset according to user selections."""
    state = _ensure_state(state)
    df = _current_dataframe(state, filtered=False)

    numeric_filters: Dict[str, Tuple[Optional[float], Optional[float]]] = {}
    categorical_filters: Dict[str, List[str]] = {}
    date_filters: Dict[str, Tuple[Optional[str], Optional[str]]] = {}

    if numeric_column and (numeric_min is not None or numeric_max is not None):
        lower = numeric_min
        upper = numeric_max
        if lower is not None and upper is not None and lower > upper:
            lower, upper = upper, lower
        numeric_filters[numeric_column] = (lower, upper)

    if categorical_column and categorical_values:
        categorical_filters[categorical_column] = categorical_values

    if date_column and (start_date or end_date):
        date_filters[date_column] = (start_date, end_date)

    filtered_df = filter_dataframe(df, numeric_filters, categorical_filters, date_filters)
    state["filtered_df"] = filtered_df

    row_count = f"Rows displayed: {len(filtered_df)}"
    preview = filtered_df.head(5) if not filtered_df.empty else _empty_dataframe("No rows match the filters.")
    return state, preview, row_count


def _generate_statistics(state) -> Tuple[pd.DataFrame, pd.DataFrame, pd.DataFrame, pd.DataFrame, str]:
    """Produce summary statistics for the Statistics tab."""
    state = _ensure_state(state)
    try:
        df = _current_dataframe(state, filtered=False)
    except ValueError as exc:
        message = str(exc)
        empty = _empty_dataframe(message)
        return empty, empty, empty, empty, f"⚠️ {message}"

    num_summary = numeric_summary(df)
    cat_summary = categorical_summary(df)
    missing = missing_value_report(df)
    corr = correlation_matrix(df)
    message = "Statistics generated successfully."
    return (
        num_summary if not num_summary.empty else _empty_dataframe("No numeric columns available."),
        cat_summary if not cat_summary.empty else _empty_dataframe("No categorical columns available."),
        missing if not missing.empty else _empty_dataframe("No missing values detected."),
        corr if not corr.empty else _empty_dataframe("Not enough numeric columns for correlation."),
        message,
    )


def _generate_chart(
    state,
    chart_type: str,
    ts_date: Optional[str],
    ts_value: Optional[str],
    ts_agg: str,
    dist_column: Optional[str],
    dist_type: str,
    cat_column: Optional[str],
    cat_value: Optional[str],
    cat_chart_type: str,
    cat_agg: str,
    scatter_x: Optional[str],
    scatter_y: Optional[str],
    scatter_color: Optional[str],
) -> Tuple[Dict[str, Any], Any, str]:
    """Create a visualization based on user selections."""
    state = _ensure_state(state)
    try:
        df = _current_dataframe(state, filtered=True)
    except ValueError as exc:
        state["current_figure"] = None
        return state, None, f"⚠️ {exc}"

    try:
        if chart_type == "Time Series":
            if not ts_date or not ts_value:
                raise ValueError("Select both a date and value column.")
            fig = create_time_series_plot(df, ts_date, ts_value, aggregation=ts_agg)
        elif chart_type == "Distribution":
            if not dist_column:
                raise ValueError("Select a numeric column for the distribution plot.")
            fig = create_distribution_plot(df, dist_column, plot_type=dist_type)
        elif chart_type == "Category":
            if not cat_column or not cat_value:
                raise ValueError("Select both category and value columns.")
            fig = create_category_plot(df, cat_column, cat_value, aggregation=cat_agg, chart_type=cat_chart_type.lower())
        elif chart_type == "Scatter":
            if not scatter_x or not scatter_y:
                raise ValueError("Select x and y columns for the scatter plot.")
            fig = create_scatter_plot(df, scatter_x, scatter_y, color_column=scatter_color)
        elif chart_type == "Correlation Heatmap":
            fig = create_correlation_heatmap(df)
        else:
            raise ValueError("Unsupported chart type.")
    except ValueError as exc:
        state["current_figure"] = None
        return state, None, f"⚠️ {exc}"

    state["current_figure"] = fig
    return state, fig, "✅ Visualization generated. Use 'Export Chart' to download."


def _download_filtered(state) -> str:
    """Export the filtered dataset to a temporary CSV file."""
    state = _ensure_state(state)
    df = _current_dataframe(state, filtered=True)
    if df.empty:
        raise ValueError("There are no rows to export. Adjust your filters and try again.")

    temp = tempfile.NamedTemporaryFile(delete=False, suffix=".csv", prefix="filtered_", dir=".")
    df.to_csv(temp.name, index=False)
    temp.close()
    return temp.name


def _download_chart(state) -> str:
    """Export the most recent chart to PNG."""
    state = _ensure_state(state)
    fig = state.get("current_figure")
    if fig is None:
        raise ValueError("Generate a visualization before exporting.")
    buffer = figure_to_png_bytes(fig)
    temp = tempfile.NamedTemporaryFile(delete=False, suffix=".png", prefix="chart_", dir=".")
    with open(temp.name, "wb") as fp:
        fp.write(buffer.read())
    return temp.name


def _generate_insights(
    state,
    numeric_column: Optional[str],
    trend_date_column: Optional[str],
    trend_value_column: Optional[str],
    anomaly_column: Optional[str],
) -> Tuple[pd.DataFrame, pd.DataFrame, str, pd.DataFrame, str]:
    """Generate top/bottom performers, trends, and anomalies."""
    state = _ensure_state(state)
    try:
        df = _current_dataframe(state, filtered=True)
    except ValueError as exc:
        empty = _empty_dataframe(str(exc))
        return empty, empty, f"⚠️ {exc}", empty, f"⚠️ {exc}"

    status_messages: List[str] = []

    top_df = bottom_df = _empty_dataframe("Select a numeric column for insights.")
    if numeric_column:
        try:
            performers = top_bottom_performers(df, numeric_column)
            top_df = performers["top"]
            bottom_df = performers["bottom"]
            status_messages.append(f"Top/bottom performers calculated for {numeric_column}.")
        except ValueError as exc:
            top_df = bottom_df = _empty_dataframe(str(exc))
            status_messages.append(f"⚠️ {exc}")

    trend_text = "Select a date and value column to evaluate trend."
    if trend_date_column and trend_value_column:
        try:
            trend_text = detect_trend(df, trend_date_column, trend_value_column)
        except ValueError as exc:
            trend_text = f"⚠️ {exc}"

    anomaly_df = _empty_dataframe("Select a numeric column to detect anomalies.")
    if anomaly_column:
        anomalies = detect_anomalies(df, anomaly_column)
        anomaly_df = anomalies if not anomalies.empty else _empty_dataframe("No significant anomalies detected.")

    combined_status = "\n".join(status_messages) if status_messages else "Insights generated."
    return top_df, bottom_df, trend_text, anomaly_df, combined_status


def _describe_sample_dataset(selection: Optional[str]) -> str:
    """Return a user-friendly description for the selected sample dataset."""
    if not selection:
        return "Select a sample dataset to view its description."
    descriptions = sample_dataset_options()
    description = descriptions.get(selection)
    if not description:
        return "Sample dataset description unavailable. Ensure the file exists in the `data/` directory."
    return f"**{selection}**\n\n{description}"


def create_dashboard():
    with gr.Blocks(theme=gr.themes.Soft()) as demo:
        gr.Markdown("# Business Intelligence Dashboard")
        dataset_state = gr.State(DEFAULT_STATE.copy())
        #last_figure_state = gr.State(None)

        sample_choices = list(sample_dataset_options().keys())

        with gr.Tab("Data Upload"):
            with gr.Row():
                file_input = gr.File(label="Upload CSV or Excel", file_types=[".csv", ".xlsx", ".xls"])
                load_button = gr.Button("Load Data", variant="primary")
            gr.Markdown("Or load one of the curated datasets bundled with the project:")
            with gr.Row():
                sample_dropdown = gr.Dropdown(label="Sample Dataset", choices=sample_choices, value=None, interactive=bool(sample_choices))
                load_sample_button = gr.Button("Load Sample", variant="secondary", interactive=bool(sample_choices))
            if sample_choices:
                sample_description = gr.Markdown("Select a sample dataset to view its description.")
            else:
                sample_description = gr.Markdown("⚠️ No sample datasets detected in the `data/` folder.")
            upload_status = gr.Markdown("No dataset loaded.")
            dataset_info = gr.Markdown()
            dtypes_table = gr.Dataframe(label="Column Types", interactive=False)
            with gr.Row():
                head_table = gr.Dataframe(label="Preview (Head)", interactive=False)
                tail_table = gr.Dataframe(label="Preview (Tail)", interactive=False)

        with gr.Tab("Statistics"):
            stats_status = gr.Markdown()
            numeric_table = gr.Dataframe(label="Numeric Summary", interactive=False)
            categorical_table = gr.Dataframe(label="Categorical Summary", interactive=False)
            missing_table = gr.Dataframe(label="Missing Value Report", interactive=False)
            correlation_table = gr.Dataframe(label="Correlation Matrix", interactive=False)
            generate_stats_button = gr.Button("Generate Statistics", variant="secondary")

        with gr.Tab("Filter & Explore"):
            filter_status = gr.Markdown("Rows displayed: 0")
            with gr.Accordion("Numeric Filter", open=False):
                numeric_column_dropdown = gr.Dropdown(label="Numeric Column", choices=[])
                numeric_min_input = gr.Number(label="Minimum Value", visible=False)
                numeric_max_input = gr.Number(label="Maximum Value", visible=False)
            with gr.Accordion("Categorical Filter", open=False):
                categorical_column_dropdown = gr.Dropdown(label="Category Column", choices=[])
                categorical_values = gr.CheckboxGroup(label="Values", choices=[], visible=False)
            with gr.Accordion("Date Filter", open=False):
                date_column_dropdown = gr.Dropdown(label="Date Column", choices=[])
                start_date_picker = gr.Textbox(label="Start Date (YYYY-MM-DD)", visible=False)
                end_date_picker = gr.Textbox(label="End Date (YYYY-MM-DD)", visible=False)
            apply_filters_button = gr.Button("Apply Filters", variant="primary")
            filter_preview_table = gr.Dataframe(label="Filtered Preview", interactive=False)
            export_filtered_button = gr.Button("Download Filtered Data", variant="secondary")
            export_filtered_file = gr.File(label="Filtered CSV", interactive=False)

        with gr.Tab("Visualizations"):
            viz_status = gr.Markdown()
            chart_type = gr.Radio(
                label="Chart Type",
                choices=["Time Series", "Distribution", "Category", "Scatter", "Correlation Heatmap"],
                value="Time Series",
            )
            with gr.Column(visible=True) as time_series_controls:
                ts_date_column = gr.Dropdown(label="Date Column", choices=[])
                ts_value_column = gr.Dropdown(label="Value Column", choices=[])
                ts_aggregation = gr.Dropdown(label="Aggregation", choices=["sum", "mean", "median", "count"], value="sum")
            with gr.Column(visible=False) as distribution_controls:
                dist_column = gr.Dropdown(label="Numeric Column", choices=[])
                dist_type = gr.Radio(label="Distribution Type", choices=["histogram", "box"], value="histogram")
            with gr.Column(visible=False) as category_controls:
                category_column = gr.Dropdown(label="Category Column", choices=[])
                category_value_column = gr.Dropdown(label="Value Column", choices=[])
                category_chart_type = gr.Radio(label="Chart Style", choices=["Bar", "Pie"], value="Bar")
                category_aggregation = gr.Dropdown(label="Aggregation", choices=["sum", "mean", "median", "count"], value="sum")
            with gr.Column(visible=False) as scatter_controls:
                scatter_x_column = gr.Dropdown(label="X Axis", choices=[])
                scatter_y_column = gr.Dropdown(label="Y Axis", choices=[])
                scatter_color_column = gr.Dropdown(label="Color (optional)", choices=[])

            with gr.Row():
                generate_chart_button = gr.Button("Generate Visualization", variant="primary")
                export_chart_button = gr.Button("Export Chart (PNG)", variant="secondary")
            chart_output = gr.Plot(label="Visualization")
            export_chart_file = gr.File(label="Exported Chart", interactive=False)


        with gr.Tab("Insights"):
            insights_status = gr.Markdown()
            insight_numeric_column = gr.Dropdown(label="Numeric Column", choices=[])
            trend_date_column = gr.Dropdown(label="Date Column", choices=[])
            trend_value_column = gr.Dropdown(label="Value Column", choices=[])
            anomaly_column = gr.Dropdown(label="Column for Anomaly Detection", choices=[])
            generate_insights_button = gr.Button("Generate Insights", variant="primary")
            top_table = gr.Dataframe(label="Top Performers", interactive=False)
            bottom_table = gr.Dataframe(label="Bottom Performers", interactive=False)
            trend_output = gr.Markdown()
            anomaly_table = gr.Dataframe(label="Potential Anomalies", interactive=False)

        # Interactions
        load_button.click(
            fn=_handle_file_upload,
            inputs=[file_input, dataset_state],
            outputs=[
                dataset_state,
                upload_status,
                dataset_info,
                dtypes_table,
                head_table,
                tail_table,
                filter_preview_table,
                filter_status,
            ],
        ).then(
            fn=_populate_column_options,
            inputs=[dataset_state],
            outputs=[
                numeric_column_dropdown,
                date_column_dropdown,
                categorical_values,
                categorical_column_dropdown,
                ts_date_column,
                ts_value_column,
                dist_column,
                category_column,
                category_value_column,
                scatter_x_column,
                scatter_y_column,
                scatter_color_column,
                insight_numeric_column,
                trend_date_column,
                trend_value_column,
                anomaly_column,
            ],
        ).then(
            fn=_generate_statistics,
            inputs=[dataset_state],
            outputs=[
                numeric_table,
                categorical_table,
                missing_table,
                correlation_table,
                stats_status,
            ],
        )

        load_sample_button.click(
            fn=_handle_sample_dataset,
            inputs=[sample_dropdown, dataset_state],
            outputs=[
                dataset_state,
                upload_status,
                dataset_info,
                dtypes_table,
                head_table,
                tail_table,
                filter_preview_table,
                filter_status,
            ],
        ).then(
            fn=_populate_column_options,
            inputs=[dataset_state],
            outputs=[
                numeric_column_dropdown,
                date_column_dropdown,
                categorical_values,
                categorical_column_dropdown,
                ts_date_column,
                ts_value_column,
                dist_column,
                category_column,
                category_value_column,
                scatter_x_column,
                scatter_y_column,
                scatter_color_column,
                insight_numeric_column,
                trend_date_column,
                trend_value_column,
                anomaly_column,
            ],
        ).then(
            fn=_generate_statistics,
            inputs=[dataset_state],
            outputs=[
                numeric_table,
                categorical_table,
                missing_table,
                correlation_table,
                stats_status,
            ],
        )

        sample_dropdown.change(
            fn=_describe_sample_dataset,
            inputs=[sample_dropdown],
            outputs=[sample_description],
        )

        numeric_column_dropdown.change(
            fn=_update_numeric_inputs,
            inputs=[numeric_column_dropdown, dataset_state],
            outputs=[numeric_min_input, numeric_max_input],
        )

        categorical_column_dropdown.change(
            fn=_update_categorical_values,
            inputs=[categorical_column_dropdown, dataset_state],
            outputs=[categorical_values],
        )

        date_column_dropdown.change(
            fn=_update_date_bounds,
            inputs=[date_column_dropdown, dataset_state],
            outputs=[start_date_picker, end_date_picker],
        )

        generate_stats_button.click(
            fn=_generate_statistics,
            inputs=[dataset_state],
            outputs=[numeric_table, categorical_table, missing_table, correlation_table, stats_status],
        )

        apply_filters_button.click(
            fn=_apply_filters,
            inputs=[
                dataset_state,
                numeric_column_dropdown,
                numeric_min_input,
                numeric_max_input,
                categorical_column_dropdown,
                categorical_values,
                date_column_dropdown,
                start_date_picker,
                end_date_picker,
            ],
            outputs=[dataset_state, filter_preview_table, filter_status],
        )

        export_filtered_button.click(
            fn=_download_filtered,
            inputs=[dataset_state],
            outputs=[export_filtered_file],
        )

        def _toggle_controls(selected: str) -> Tuple[Any, Any, Any, Any]:
            return (
                gr.update(visible=selected == "Time Series"),
                gr.update(visible=selected == "Distribution"),
                gr.update(visible=selected == "Category"),
                gr.update(visible=selected == "Scatter"),
            )

        chart_type.change(
            fn=_toggle_controls,
            inputs=[chart_type],
            outputs=[time_series_controls, distribution_controls, category_controls, scatter_controls],
        )

        generate_chart_button.click(
            fn=_generate_chart,
            inputs=[
                dataset_state,
                chart_type,
                ts_date_column,
                ts_value_column,
                ts_aggregation,
                dist_column,
                dist_type,
                category_column,
                category_value_column,
                category_chart_type,
                category_aggregation,
                scatter_x_column,
                scatter_y_column,
                scatter_color_column,
            ],
            outputs=[dataset_state, chart_output, viz_status],
        )

        export_chart_button.click(
            fn=_download_chart,
            inputs=[dataset_state],
            outputs=[export_chart_file],
        )

       
        generate_insights_button.click(
            fn=_generate_insights,
            inputs=[
                dataset_state,
                insight_numeric_column,
                trend_date_column,
                trend_value_column,
                anomaly_column,
            ],
            outputs=[
                top_table,
                bottom_table,
                trend_output,
                anomaly_table,
                insights_status,
            ],
        )

    return demo


if __name__ == "__main__":
    demo = create_dashboard()
    demo.launch(server_name="0.0.0.0", server_port=7860, share=True)