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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24b96ad
 
33db58a
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
"""
Data Cleaning & Visualization Project
A Gradio-based interactive dashboard for cleaning raw datasets and generating insights.
"""

import gradio as gr
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from io import StringIO
import warnings

warnings.filterwarnings("ignore")
sns.set_theme(style="whitegrid", palette="muted")
plt.rcParams["figure.figsize"] = (10, 5)
plt.rcParams["figure.dpi"] = 100


# ---------------------------------------------------------------------------
# Helper functions
# ---------------------------------------------------------------------------

def load_dataframe(file):
    """Load CSV or Excel file into a DataFrame."""
    if file is None:
        return None, "Please upload a file first."
    try:
        path = file.name if hasattr(file, "name") else file
        if str(path).lower().endswith((".xlsx", ".xls")):
            df = pd.read_excel(path)
        else:
            df = pd.read_csv(path)
        return df, f"Loaded successfully • Shape: {df.shape[0]} rows × {df.shape[1]} columns"
    except Exception as e:
        return None, f"Error loading file: {str(e)}"


def get_overview(df):
    """Return text overview of the dataset."""
    if df is None:
        return "No data loaded."

    buffer = StringIO()
    df.info(buf=buffer)
    info_str = buffer.getvalue()

    missing = df.isnull().sum()
    missing_pct = (missing / len(df) * 100).round(2)
    missing_df = pd.DataFrame({"Missing Count": missing, "Missing %": missing_pct})
    missing_df = missing_df[missing_df["Missing Count"] > 0].sort_values(
        "Missing Count", ascending=False
    )

    overview = f"""
### Dataset Overview
- **Rows:** {df.shape[0]:,}
- **Columns:** {df.shape[1]}
- **Memory Usage:** {df.memory_usage(deep=True).sum() / 1024**2:.2f} MB
- **Duplicate Rows:** {df.duplicated().sum():,}
- **Numeric Columns:** {len(df.select_dtypes(include=np.number).columns)}
- **Categorical Columns:** {len(df.select_dtypes(include=["object", "category"]).columns)}

### Missing Values
"""
    if missing_df.empty:
        overview += "No missing values found.\n"
    else:
        overview += missing_df.to_markdown() + "\n"

    overview += "\n### Column Data Types\n"
    overview += df.dtypes.to_frame("dtype").to_markdown()

    overview += "\n\n### First 5 Rows\n"
    overview += df.head().to_markdown(index=False)

    return overview


def handle_missing(df, strategy, fill_value=None):
    """Handle missing values according to chosen strategy."""
    if df is None:
        return None, "No data loaded."

    df = df.copy()
    before = df.isnull().sum().sum()

    if strategy == "Drop rows with any missing":
        df = df.dropna()
    elif strategy == "Drop columns with >50% missing":
        thresh = len(df) * 0.5
        df = df.dropna(axis=1, thresh=thresh)
    elif strategy == "Fill numeric with mean":
        num_cols = df.select_dtypes(include=np.number).columns
        df[num_cols] = df[num_cols].fillna(df[num_cols].mean())
    elif strategy == "Fill numeric with median":
        num_cols = df.select_dtypes(include=np.number).columns
        df[num_cols] = df[num_cols].fillna(df[num_cols].median())
    elif strategy == "Fill categorical with mode":
        cat_cols = df.select_dtypes(include=["object", "category"]).columns
        for col in cat_cols:
            mode_val = df[col].mode()
            if not mode_val.empty:
                df[col] = df[col].fillna(mode_val[0])
    elif strategy == "Fill with custom value":
        if fill_value is None or fill_value == "":
            return df, "Please provide a custom fill value."
        try:
            # try numeric conversion
            val = float(fill_value)
            df = df.fillna(val)
        except ValueError:
            df = df.fillna(fill_value)
    elif strategy == "Forward fill":
        df = df.ffill()
    elif strategy == "Backward fill":
        df = df.bfill()

    after = df.isnull().sum().sum()
    msg = f"Missing values: {before} → {after}  |  Rows remaining: {len(df):,}"
    return df, msg


def remove_duplicates(df):
    """Remove duplicate rows."""
    if df is None:
        return None, "No data loaded."
    before = len(df)
    df = df.drop_duplicates()
    after = len(df)
    return df, f"Duplicates removed: {before - after}  |  Rows remaining: {after:,}"


def detect_outliers_iqr(df, columns=None):
    """Detect outliers using IQR method. Returns boolean mask of outlier rows."""
    if df is None:
        return None
    num_cols = df.select_dtypes(include=np.number).columns.tolist()
    if columns:
        num_cols = [c for c in columns if c in num_cols]
    if not num_cols:
        return pd.Series([False] * len(df), index=df.index)

    outlier_mask = pd.Series([False] * len(df), index=df.index)
    for col in num_cols:
        Q1 = df[col].quantile(0.25)
        Q3 = df[col].quantile(0.75)
        IQR = Q3 - Q1
        lower = Q1 - 1.5 * IQR
        upper = Q3 + 1.5 * IQR
        outlier_mask |= (df[col] < lower) | (df[col] > upper)
    return outlier_mask


def handle_outliers(df, method, columns=None):
    """Handle outliers: remove or cap them."""
    if df is None:
        return None, "No data loaded."

    df = df.copy()
    num_cols = df.select_dtypes(include=np.number).columns.tolist()
    if columns:
        selected = [c.strip() for c in columns.split(",") if c.strip()]
        num_cols = [c for c in selected if c in num_cols]
    if not num_cols:
        return df, "No numeric columns selected for outlier handling."

    mask = detect_outliers_iqr(df, num_cols)
    n_outliers = mask.sum()

    if method == "Remove outlier rows":
        df = df[~mask]
        msg = f"Removed {n_outliers} outlier rows  |  Rows remaining: {len(df):,}"
    elif method == "Cap outliers (winsorize)":
        for col in num_cols:
            Q1 = df[col].quantile(0.25)
            Q3 = df[col].quantile(0.75)
            IQR = Q3 - Q1
            lower = Q1 - 1.5 * IQR
            upper = Q3 + 1.5 * IQR
            df[col] = df[col].clip(lower=lower, upper=upper)
        msg = f"Capped outliers in columns: {', '.join(num_cols)}  |  Outliers affected: {n_outliers}"
    else:
        msg = "No action taken."
    return df, msg


def plot_missing_heatmap(df):
    """Visualize missing values."""
    if df is None or df.empty:
        return None
    fig, ax = plt.subplots(figsize=(12, 5))
    sns.heatmap(df.isnull(), cbar=False, yticklabels=False, cmap="YlOrRd", ax=ax)
    ax.set_title("Missing Values Heatmap (yellow/red = missing)")
    plt.tight_layout()
    return fig


def plot_distributions(df, columns=None):
    """Plot histograms / KDE for numeric columns."""
    if df is None:
        return None
    num_cols = df.select_dtypes(include=np.number).columns.tolist()
    if columns:
        selected = [c.strip() for c in columns.split(",") if c.strip()]
        num_cols = [c for c in selected if c in num_cols]
    if not num_cols:
        return None

    n = min(len(num_cols), 6)
    cols = num_cols[:n]
    fig, axes = plt.subplots(1, n, figsize=(4 * n, 4))
    if n == 1:
        axes = [axes]
    for ax, col in zip(axes, cols):
        sns.histplot(df[col].dropna(), kde=True, ax=ax, color="#4C72B0")
        ax.set_title(col)
        ax.set_xlabel("")
    plt.suptitle("Numeric Distributions", y=1.02)
    plt.tight_layout()
    return fig


def plot_boxplots(df, columns=None):
    """Boxplots to show outliers."""
    if df is None:
        return None
    num_cols = df.select_dtypes(include=np.number).columns.tolist()
    if columns:
        selected = [c.strip() for c in columns.split(",") if c.strip()]
        num_cols = [c for c in selected if c in num_cols]
    if not num_cols:
        return None

    n = min(len(num_cols), 6)
    cols = num_cols[:n]
    fig, axes = plt.subplots(1, n, figsize=(3.5 * n, 4))
    if n == 1:
        axes = [axes]
    for ax, col in zip(axes, cols):
        sns.boxplot(y=df[col], ax=ax, color="#55A868")
        ax.set_title(col)
    plt.suptitle("Boxplots (Outlier Detection)", y=1.02)
    plt.tight_layout()
    return fig


def plot_correlation(df):
    """Correlation heatmap for numeric features."""
    if df is None:
        return None
    num_df = df.select_dtypes(include=np.number)
    if num_df.shape[1] < 2:
        return None
    corr = num_df.corr()
    fig, ax = plt.subplots(figsize=(10, 8))
    sns.heatmap(
        corr,
        annot=True,
        fmt=".2f",
        cmap="coolwarm",
        center=0,
        square=True,
        linewidths=0.5,
        ax=ax,
        cbar_kws={"shrink": 0.8},
    )
    ax.set_title("Correlation Matrix")
    plt.tight_layout()
    return fig


def plot_categorical(df, column=None):
    """Bar chart for a categorical column."""
    if df is None:
        return None
    cat_cols = df.select_dtypes(include=["object", "category"]).columns.tolist()
    if not cat_cols:
        return None
    col = column if column and column in cat_cols else cat_cols[0]
    counts = df[col].value_counts().head(15)
    fig, ax = plt.subplots(figsize=(10, 5))
    sns.barplot(x=counts.values, y=counts.index, ax=ax, palette="viridis")
    ax.set_title(f"Top categories in '{col}'")
    ax.set_xlabel("Count")
    plt.tight_layout()
    return fig


def generate_summary_report(df):
    """Generate a storytelling-style text report of key findings."""
    if df is None:
        return "No data available."

    lines = ["# Data Insights Report\n"]
    lines.append(f"**Dataset size after cleaning:** {df.shape[0]:,} rows × {df.shape[1]} columns\n")

    # Missing
    missing_total = df.isnull().sum().sum()
    lines.append(f"- Remaining missing values: **{missing_total}**\n")

    # Duplicates
    lines.append(f"- Duplicate rows remaining: **{df.duplicated().sum()}**\n")

    # Numeric summary
    num_df = df.select_dtypes(include=np.number)
    if not num_df.empty:
        lines.append("\n## Numeric Features Summary\n")
        desc = num_df.describe().T[["mean", "std", "min", "50%", "max"]].round(2)
        lines.append(desc.to_markdown())
        lines.append("\n")

        # Strongest correlations
        if num_df.shape[1] >= 2:
            corr = num_df.corr().abs()
            # zero out diagonal without mutating original
            corr_no_diag = corr.where(~np.eye(corr.shape[0], dtype=bool), 0)
            if corr_no_diag.max().max() > 0:
                max_pair = corr_no_diag.stack().idxmax()
                lines.append(
                    f"\n**Strongest correlation:** `{max_pair[0]}` ↔ `{max_pair[1]}` "
                    f"(r = {num_df.corr().loc[max_pair]:.3f})\n"
                )

    # Categorical
    cat_cols = df.select_dtypes(include=["object", "category"]).columns
    if len(cat_cols) > 0:
        lines.append("\n## Categorical Features\n")
        for col in cat_cols[:5]:
            top = df[col].value_counts().head(3)
            lines.append(f"- **{col}** top values: " + ", ".join(f"`{k}` ({v})" for k, v in top.items()))
        lines.append("\n")

    lines.append("\n---\n*Report generated by the Data Cleaning & Visualization dashboard.*")
    return "\n".join(lines)


# ---------------------------------------------------------------------------
# Gradio UI callbacks
# ---------------------------------------------------------------------------

state_df = gr.State(value=None)


def on_upload(file, current_df):
    df, msg = load_dataframe(file)
    if df is not None:
        return df, msg, get_overview(df), df.head(20)
    return current_df, msg, "Upload failed.", None


def on_clean_missing(df, strategy, fill_value):
    if df is None:
        return None, "Upload data first.", None, None
    new_df, msg = handle_missing(df, strategy, fill_value)
    return new_df, msg, get_overview(new_df), new_df.head(20)


def on_remove_dupes(df):
    if df is None:
        return None, "Upload data first.", None, None
    new_df, msg = remove_duplicates(df)
    return new_df, msg, get_overview(new_df), new_df.head(20)


def on_outliers(df, method, columns):
    if df is None:
        return None, "Upload data first.", None, None
    new_df, msg = handle_outliers(df, method, columns)
    return new_df, msg, get_overview(new_df), new_df.head(20)


def on_visualize(df, dist_cols, box_cols, cat_col):
    if df is None:
        return None, None, None, None, "Upload & clean data first."
    fig_miss = plot_missing_heatmap(df)
    fig_dist = plot_distributions(df, dist_cols)
    fig_box = plot_boxplots(df, box_cols)
    fig_corr = plot_correlation(df)
    fig_cat = plot_categorical(df, cat_col)
    report = generate_summary_report(df)
    return fig_miss, fig_dist, fig_box, fig_corr, fig_cat, report


def download_cleaned(df):
    if df is None:
        return None
    path = "/tmp/cleaned_dataset.csv"
    df.to_csv(path, index=False)
    return path


# ---------------------------------------------------------------------------
# Build the Gradio interface
# ---------------------------------------------------------------------------

CUSTOM_CSS = """
.gradio-container { max-width: 1200px !important; }
.header-text { text-align: center; margin-bottom: 1rem; }
"""

with gr.Blocks(
    title="Data Cleaning & Visualization",
    theme=gr.themes.Soft(primary_hue="blue", secondary_hue="slate"),
    css=CUSTOM_CSS,
) as demo:

    gr.Markdown(
        """
        # Data Cleaning & Visualization Project
        **Upload a raw dataset → clean missing values, outliers & duplicates → explore insights with interactive charts.**
        """
    )

    current_df = gr.State(None)

    with gr.Tab("1. Upload & Overview"):
        with gr.Row():
            file_input = gr.File(
                label="Upload CSV or Excel file",
                file_types=[".csv", ".xlsx", ".xls"],
            )
            status_msg = gr.Textbox(label="Status", interactive=False)

        overview_md = gr.Markdown("Upload a file to see the dataset overview.")
        preview_table = gr.Dataframe(label="Data Preview (first 20 rows)", interactive=False)

        file_input.change(
            on_upload,
            inputs=[file_input, current_df],
            outputs=[current_df, status_msg, overview_md, preview_table],
        )

    with gr.Tab("2. Clean Data"):
        gr.Markdown("### Handle Missing Values")
        with gr.Row():
            missing_strategy = gr.Dropdown(
                choices=[
                    "Drop rows with any missing",
                    "Drop columns with >50% missing",
                    "Fill numeric with mean",
                    "Fill numeric with median",
                    "Fill categorical with mode",
                    "Fill with custom value",
                    "Forward fill",
                    "Backward fill",
                ],
                value="Fill numeric with median",
                label="Missing-value strategy",
            )
            custom_fill = gr.Textbox(
                label="Custom fill value (only for 'Fill with custom value')",
                placeholder="e.g. 0 or Unknown",
            )
        btn_missing = gr.Button("Apply Missing-Value Handling", variant="primary")

        gr.Markdown("### Remove Duplicates")
        btn_dupes = gr.Button("Remove Duplicate Rows", variant="secondary")

        gr.Markdown("### Handle Outliers (IQR method)")
        with gr.Row():
            outlier_method = gr.Dropdown(
                choices=["Remove outlier rows", "Cap outliers (winsorize)"],
                value="Cap outliers (winsorize)",
                label="Outlier handling method",
            )
            outlier_cols = gr.Textbox(
                label="Columns (comma-separated, leave blank for all numeric)",
                placeholder="e.g. age, salary, price",
            )
        btn_outliers = gr.Button("Apply Outlier Handling", variant="primary")

        clean_status = gr.Textbox(label="Cleaning Status", interactive=False)
        clean_overview = gr.Markdown()
        clean_preview = gr.Dataframe(label="Cleaned Preview", interactive=False)

        btn_missing.click(
            on_clean_missing,
            inputs=[current_df, missing_strategy, custom_fill],
            outputs=[current_df, clean_status, clean_overview, clean_preview],
        )
        btn_dupes.click(
            on_remove_dupes,
            inputs=[current_df],
            outputs=[current_df, clean_status, clean_overview, clean_preview],
        )
        btn_outliers.click(
            on_outliers,
            inputs=[current_df, outlier_method, outlier_cols],
            outputs=[current_df, clean_status, clean_overview, clean_preview],
        )

        gr.Markdown("---")
        download_btn = gr.Button("Download Cleaned CSV")
        download_file = gr.File(label="Download")
        download_btn.click(download_cleaned, inputs=[current_df], outputs=[download_file])

    with gr.Tab("3. Visualize & Insights"):
        gr.Markdown("Select columns (optional) and generate plots + an automatic insights report.")
        with gr.Row():
            dist_cols = gr.Textbox(
                label="Distribution columns (comma-separated)",
                placeholder="Leave blank for first 6 numeric columns",
            )
            box_cols = gr.Textbox(
                label="Boxplot columns (comma-separated)",
                placeholder="Leave blank for first 6 numeric columns",
            )
            cat_col = gr.Textbox(
                label="Categorical column for bar chart",
                placeholder="Leave blank for first categorical column",
            )
        btn_viz = gr.Button("Generate Visualizations & Report", variant="primary")

        with gr.Row():
            plot_miss = gr.Plot(label="Missing Values Heatmap")
            plot_corr = gr.Plot(label="Correlation Matrix")
        with gr.Row():
            plot_dist = gr.Plot(label="Distributions")
            plot_box = gr.Plot(label="Boxplots")
        plot_cat = gr.Plot(label="Categorical Breakdown")
        insights_md = gr.Markdown(label="Insights Report")

        btn_viz.click(
            on_visualize,
            inputs=[current_df, dist_cols, box_cols, cat_col],
            outputs=[plot_miss, plot_dist, plot_box, plot_corr, plot_cat, insights_md],
        )

    with gr.Tab("About"):
        gr.Markdown(
            """
            ## Project Goals
            This interactive dashboard demonstrates the complete workflow of a **Data Cleaning & Visualization** project:

            ### Key Features Implemented
            - **Handle missing values** – multiple strategies (drop, mean/median/mode fill, ffill/bfill, custom)
            - **Remove duplicates**
            - **Detect & treat outliers** using the IQR method (remove or winsorize/cap)
            - **Visualizations** with Matplotlib & Seaborn:
              - Missing-value heatmap
              - Histograms / KDE distributions
              - Boxplots for outlier inspection
              - Correlation matrix
              - Categorical bar charts
            - **Automated insights report** that tells a short data story

            ### Expected Learning Outcomes
            - Data preprocessing techniques
            - Exploratory data visualization
            - Storytelling with data

            ### How to use
            1. Upload any CSV or Excel file in **Tab 1**
            2. Clean the data step-by-step in **Tab 2**
            3. Generate charts and the insights report in **Tab 3**
            4. Download the cleaned dataset when ready

            Built with **Gradio**, **Pandas**, **Matplotlib** & **Seaborn**.
            """
        )

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