Spaces:
Sleeping
Sleeping
Download app.py from roseyshi/Data_Analyst_Assistant: direct link, hf CLI and curl.
- Browser
- Download file 3.99 kB
-
https://huggingface.co/spaces/roseyshi/Data_Analyst_Assistant/resolve/main/app.py
- Command line
-
hf download hf://spaces/roseyshi/Data_Analyst_Assistant/app.py
-
curl -L -o app.py https://huggingface.co/spaces/roseyshi/Data_Analyst_Assistant/resolve/main/app.py
3.99 kB
| import numpy as np | |
| import pandas as pd | |
| import matplotlib | |
| matplotlib.use("Agg") | |
| import matplotlib.pyplot as plt | |
| import gradio as gr | |
| import spaces | |
| def _gpu_placeholder(): | |
| return "ok" | |
| MAX_ROWS = 200_000 | |
| def profile(file): | |
| if file is None: | |
| raise gr.Error("Please upload a CSV file first.") | |
| path = getattr(file, "name", file) | |
| try: | |
| df = pd.read_csv(path, nrows=MAX_ROWS) | |
| except Exception as e: | |
| raise gr.Error(f"Could not read the file as a CSV: {e}") | |
| n_rows, n_cols = df.shape | |
| n_dupes = int(df.duplicated().sum()) | |
| total_missing = int(df.isna().sum().sum()) | |
| # ---- Summary ---- | |
| summary = ( | |
| f"### Overview\n" | |
| f"- **Rows:** {n_rows:,} \n" | |
| f"- **Columns:** {n_cols} \n" | |
| f"- **Duplicate rows:** {n_dupes:,} \n" | |
| f"- **Missing cells:** {total_missing:,} \n" | |
| ) | |
| if n_rows == MAX_ROWS: | |
| summary += f"\n*Only the first {MAX_ROWS:,} rows were analysed.*" | |
| # ---- Column table ---- | |
| cols = pd.DataFrame({ | |
| "column": df.columns, | |
| "dtype": df.dtypes.astype(str).values, | |
| "missing": df.isna().sum().values, | |
| "missing_%": (df.isna().mean() * 100).round(1).values, | |
| "unique": df.nunique().values, | |
| }) | |
| # ---- Outliers (IQR rule) on numeric columns ---- | |
| out_rows = [] | |
| for c in df.select_dtypes(include=[np.number]).columns: | |
| s = df[c].dropna() | |
| if s.empty: | |
| continue | |
| q1, q3 = s.quantile(0.25), s.quantile(0.75) | |
| iqr = q3 - q1 | |
| low, high = q1 - 1.5 * iqr, q3 + 1.5 * iqr | |
| n_out = int(((s < low) | (s > high)).sum()) | |
| out_rows.append({ | |
| "column": c, | |
| "outliers": n_out, | |
| "outlier_%": round(n_out / len(s) * 100, 1), | |
| "lower_bound": round(low, 3), | |
| "upper_bound": round(high, 3), | |
| }) | |
| outliers = pd.DataFrame( | |
| out_rows, columns=["column", "outliers", "outlier_%", "lower_bound", "upper_bound"] | |
| ) | |
| # ---- Likely issues ---- | |
| issues = [] | |
| for c in df.columns[df.isna().mean() > 0.5]: | |
| issues.append(f"- **{c}** is more than 50% empty.") | |
| for c in df.columns[df.nunique() <= 1]: | |
| issues.append(f"- **{c}** has a single value (or is empty) and adds no information.") | |
| for c in df.select_dtypes(include="object").columns: | |
| converted = pd.to_numeric(df[c], errors="coerce") | |
| share = converted.notna().sum() / max(df[c].notna().sum(), 1) | |
| if share > 0.8: | |
| issues.append(f"- **{c}** looks numeric but is stored as text. Check for stray symbols or typos.") | |
| if n_dupes: | |
| issues.append(f"- {n_dupes:,} fully duplicated rows found.") | |
| issues_md = "### Likely issues\n" + ("\n".join(issues) if issues else "No obvious issues found.") | |
| # ---- Chart: missing values ---- | |
| fig, ax = plt.subplots(figsize=(7, 4)) | |
| miss = cols.sort_values("missing_%", ascending=False).head(15) | |
| ax.barh(miss["column"][::-1], miss["missing_%"][::-1]) | |
| ax.set_xlabel("% missing") | |
| ax.set_title("Missing values by column (top 15)") | |
| fig.tight_layout() | |
| return summary, issues_md, cols, outliers, fig | |
| with gr.Blocks(title="CSV Data Quality Profiler") as demo: | |
| gr.Markdown( | |
| "# CSV Data Quality Profiler\n" | |
| "Upload a CSV to check missing values, duplicates, outliers and " | |
| "columns with the wrong data type. Nothing is stored, and no AI is used. " | |
| "All checks are deterministic rules." | |
| ) | |
| file_in = gr.File(label="Upload CSV", file_types=[".csv"]) | |
| run_btn = gr.Button("Run checks", variant="primary") | |
| summary_out = gr.Markdown() | |
| issues_out = gr.Markdown() | |
| cols_out = gr.Dataframe(label="Column profile") | |
| outliers_out = gr.Dataframe(label="Outliers (IQR rule)") | |
| plot_out = gr.Plot(label="Missing values") | |
| run_btn.click( | |
| profile, file_in, | |
| [summary_out, issues_out, cols_out, outliers_out, plot_out], | |
| ) | |
| demo.launch(ssr_mode=False) |