Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import matplotlib
|
| 4 |
+
matplotlib.use("Agg")
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import spaces
|
| 8 |
+
|
| 9 |
+
@spaces.GPU
|
| 10 |
+
def _gpu_placeholder():
|
| 11 |
+
return "ok"
|
| 12 |
+
|
| 13 |
+
MAX_ROWS = 200_000
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def profile(file):
|
| 17 |
+
if file is None:
|
| 18 |
+
raise gr.Error("Please upload a CSV file first.")
|
| 19 |
+
|
| 20 |
+
path = getattr(file, "name", file)
|
| 21 |
+
try:
|
| 22 |
+
df = pd.read_csv(path, nrows=MAX_ROWS)
|
| 23 |
+
except Exception as e:
|
| 24 |
+
raise gr.Error(f"Could not read the file as a CSV: {e}")
|
| 25 |
+
|
| 26 |
+
n_rows, n_cols = df.shape
|
| 27 |
+
n_dupes = int(df.duplicated().sum())
|
| 28 |
+
total_missing = int(df.isna().sum().sum())
|
| 29 |
+
|
| 30 |
+
# ---- Summary ----
|
| 31 |
+
summary = (
|
| 32 |
+
f"### Overview\n"
|
| 33 |
+
f"- **Rows:** {n_rows:,} \n"
|
| 34 |
+
f"- **Columns:** {n_cols} \n"
|
| 35 |
+
f"- **Duplicate rows:** {n_dupes:,} \n"
|
| 36 |
+
f"- **Missing cells:** {total_missing:,} \n"
|
| 37 |
+
)
|
| 38 |
+
if n_rows == MAX_ROWS:
|
| 39 |
+
summary += f"\n*Only the first {MAX_ROWS:,} rows were analysed.*"
|
| 40 |
+
|
| 41 |
+
# ---- Column table ----
|
| 42 |
+
cols = pd.DataFrame({
|
| 43 |
+
"column": df.columns,
|
| 44 |
+
"dtype": df.dtypes.astype(str).values,
|
| 45 |
+
"missing": df.isna().sum().values,
|
| 46 |
+
"missing_%": (df.isna().mean() * 100).round(1).values,
|
| 47 |
+
"unique": df.nunique().values,
|
| 48 |
+
})
|
| 49 |
+
|
| 50 |
+
# ---- Outliers (IQR rule) on numeric columns ----
|
| 51 |
+
out_rows = []
|
| 52 |
+
for c in df.select_dtypes(include=[np.number]).columns:
|
| 53 |
+
s = df[c].dropna()
|
| 54 |
+
if s.empty:
|
| 55 |
+
continue
|
| 56 |
+
q1, q3 = s.quantile(0.25), s.quantile(0.75)
|
| 57 |
+
iqr = q3 - q1
|
| 58 |
+
low, high = q1 - 1.5 * iqr, q3 + 1.5 * iqr
|
| 59 |
+
n_out = int(((s < low) | (s > high)).sum())
|
| 60 |
+
out_rows.append({
|
| 61 |
+
"column": c,
|
| 62 |
+
"outliers": n_out,
|
| 63 |
+
"outlier_%": round(n_out / len(s) * 100, 1),
|
| 64 |
+
"lower_bound": round(low, 3),
|
| 65 |
+
"upper_bound": round(high, 3),
|
| 66 |
+
})
|
| 67 |
+
outliers = pd.DataFrame(
|
| 68 |
+
out_rows, columns=["column", "outliers", "outlier_%", "lower_bound", "upper_bound"]
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
# ---- Likely issues ----
|
| 72 |
+
issues = []
|
| 73 |
+
for c in df.columns[df.isna().mean() > 0.5]:
|
| 74 |
+
issues.append(f"- **{c}** is more than 50% empty.")
|
| 75 |
+
for c in df.columns[df.nunique() <= 1]:
|
| 76 |
+
issues.append(f"- **{c}** has a single value (or is empty) and adds no information.")
|
| 77 |
+
for c in df.select_dtypes(include="object").columns:
|
| 78 |
+
converted = pd.to_numeric(df[c], errors="coerce")
|
| 79 |
+
share = converted.notna().sum() / max(df[c].notna().sum(), 1)
|
| 80 |
+
if share > 0.8:
|
| 81 |
+
issues.append(f"- **{c}** looks numeric but is stored as text. Check for stray symbols or typos.")
|
| 82 |
+
if n_dupes:
|
| 83 |
+
issues.append(f"- {n_dupes:,} fully duplicated rows found.")
|
| 84 |
+
issues_md = "### Likely issues\n" + ("\n".join(issues) if issues else "No obvious issues found.")
|
| 85 |
+
|
| 86 |
+
# ---- Chart: missing values ----
|
| 87 |
+
fig, ax = plt.subplots(figsize=(7, 4))
|
| 88 |
+
miss = cols.sort_values("missing_%", ascending=False).head(15)
|
| 89 |
+
ax.barh(miss["column"][::-1], miss["missing_%"][::-1])
|
| 90 |
+
ax.set_xlabel("% missing")
|
| 91 |
+
ax.set_title("Missing values by column (top 15)")
|
| 92 |
+
fig.tight_layout()
|
| 93 |
+
|
| 94 |
+
return summary, issues_md, cols, outliers, fig
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
with gr.Blocks(title="CSV Data Quality Profiler") as demo:
|
| 98 |
+
gr.Markdown(
|
| 99 |
+
"# CSV Data Quality Profiler\n"
|
| 100 |
+
"Upload a CSV to check missing values, duplicates, outliers and "
|
| 101 |
+
"columns with the wrong data type. Nothing is stored, and no AI is used. "
|
| 102 |
+
"All checks are deterministic rules."
|
| 103 |
+
)
|
| 104 |
+
file_in = gr.File(label="Upload CSV", file_types=[".csv"])
|
| 105 |
+
run_btn = gr.Button("Run checks", variant="primary")
|
| 106 |
+
|
| 107 |
+
summary_out = gr.Markdown()
|
| 108 |
+
issues_out = gr.Markdown()
|
| 109 |
+
cols_out = gr.Dataframe(label="Column profile")
|
| 110 |
+
outliers_out = gr.Dataframe(label="Outliers (IQR rule)")
|
| 111 |
+
plot_out = gr.Plot(label="Missing values")
|
| 112 |
+
|
| 113 |
+
run_btn.click(
|
| 114 |
+
profile, file_in,
|
| 115 |
+
[summary_out, issues_out, cols_out, outliers_out, plot_out],
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
demo.launch(ssr_mode=False)
|