Spaces:
Sleeping
Sleeping
Download app.py from LienJ/Lab02-LienJ: direct link, hf CLI and curl.
- Browser
- Download file 2.12 kB
-
https://huggingface.co/spaces/LienJ/Lab02-LienJ/resolve/main/app.py
- Command line
-
hf download hf://spaces/LienJ/Lab02-LienJ/app.py
-
curl -L -o app.py https://huggingface.co/spaces/LienJ/Lab02-LienJ/resolve/main/app.py
2.12 kB
| import gradio as gr | |
| import pandas as pd | |
| import matplotlib.pyplot as plt | |
| app_data_balanced = pd.read_csv("sample_deid_balanced_safe.csv") | |
| def show_balanced_record(row_index): | |
| i = max(0, min(int(float(row_index)), len(app_data_balanced) - 1)) | |
| # Convert 1-row DataFrame to a vertical 2-column DataFrame (field, value) | |
| row_data = app_data_balanced.drop(columns=["_k"]).loc[i] | |
| record = pd.DataFrame({"field": row_data.index, "value": row_data.values}) | |
| k = int(app_data_balanced.loc[i, "_k"]) | |
| verdict = f"k = {k} -> SAFE (Balanced Generalization, Strict k>=5)" | |
| max_k_cap = 15 | |
| fig, ax = plt.subplots(figsize=(6, 4)) | |
| kc = app_data_balanced["_k"].clip(upper=max_k_cap) | |
| bins = [b + 0.5 for b in range(4, max_k_cap + 2)] | |
| ax.hist(kc, bins=bins, color="#1b9e77", edgecolor="white") | |
| ax.axvline(5, color="k", ls="--", lw=1.5, label="k=5 safety bar") | |
| ax.axvline(min(k, max_k_cap), color="blue", ls="-", lw=3, label=f"Selected Patient (k={k})") | |
| ax.set_title("Balanced k-Anonymity Distribution") | |
| ax.set_xlabel(f"k-value (capped at {max_k_cap} for readability)") | |
| ax.set_ylabel("Number of records") | |
| ax.legend() | |
| plt.tight_layout() | |
| return record, verdict, fig | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# Balanced Privacy De-Identification Viewer\n" | |
| "This dashboard runs on the **balanced generalization** dataset (20-yr age bands, 2-digit ZIP). " | |
| "We preserved ~93.5% of the data while maintaining perfect `k>=5` anonymity!") | |
| with gr.Row(): | |
| with gr.Column(): | |
| idx = gr.Slider(minimum=0, maximum=len(app_data_balanced)-1, value=0, step=1, label="Search Record by Row Index") | |
| kmeter = gr.Textbox(label="Re-identification risk (k-anonymity)") | |
| out = gr.Dataframe(label="De-identified record") | |
| with gr.Column(): | |
| plot_out = gr.Plot(label="Risk Distribution") | |
| idx.change(fn=show_balanced_record, inputs=[idx], outputs=[out, kmeter, plot_out]) | |
| demo.load(fn=show_balanced_record, inputs=[idx], outputs=[out, kmeter, plot_out]) | |
| demo.launch() | |