Spaces:
Sleeping
Sleeping
File size: 3,994 Bytes
25a4cc5 | 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 | import numpy as np
import pandas as pd
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import gradio as gr
import spaces
@spaces.GPU
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) |