Delete app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269import gradio as grimport pandas as pdfrom faker import Fakerimport ioimport osimport reimport jsonfrom typing import Optional
# ── Example CSV (created at startup, before Blocks definition) ────────────────def _create_example(): if os.path.exists("example.csv"): return sample = pd.DataFrame({ "id": range(1, 6), "first_name": ["Mario", "Lucia", "Giovanni", "Sofia", "Marco"], "last_name": ["Rossi", "Bianchi", "Ferrari", "Esposito", "Romano"], "email": ["mario.rossi@gmail.com", "lucia.b@yahoo.it", "g.ferrari@outlook.com", "sofia.e@libero.it", "m.romano@hotmail.com"], "phone": ["+39 333 1234567", "02 9876543", "+39 347 9988776", "080 5551234", "+39 320 4567890"], "city": ["Roma", "Milano", "Napoli", "Bari", "Torino"], "notes": ["premium customer", "newsletter yes", "B2B", "trial", "enterprise"], }) sample.to_csv("example.csv", index=False)
_create_example()
fake = Faker(["it_IT", "en_US"])Faker.seed(0)
# ── Available anonymization types ─────────────────────────────────────────────ANON_TYPES = { "email": "📧 Email", "first_name": "👤 First Name", "last_name": "👤 Last Name", "full_name": "👤 Full Name", "phone": "📱 Phone", "address": "🏠 Address", "city": "🏙️ City", "postal_code": "📮 Postal Code", "tax_id": "🪪 Tax ID", "date_of_birth": "📅 Date of Birth", "generic": "🔒 Generic",}
TYPE_LABELS = {v: k for k, v in ANON_TYPES.items()} # label → internal key
# ── Automatic type detection ──────────────────────────────────────────────────def _detect_type(series: pd.Series) -> str: name = (series.name or "").lower() sample = series.dropna().astype(str).head(200)
# Column-name heuristics if any(k in name for k in ("email", "mail", "e-mail")): return "email" if any(k in name for k in ("telefon", "phone", "cell", "mobile", "tel")): return "phone" if any(k in name for k in ("full_name", "fullname", "nome_completo")): return "full_name" if any(k in name for k in ("nome", "first", "given")): return "first_name" if any(k in name for k in ("cognome", "surname", "last")): return "last_name" if any(k in name for k in ("indirizzo", "address", "via", "street")): return "address" if any(k in name for k in ("città", "city", "comune", "citta")): return "city" if any(k in name for k in ("cap", "postal", "zip")): return "postal_code" if any(k in name for k in ("cf", "codice_fiscale", "fiscal", "tax")): return "tax_id" if any(k in name for k in ("nascita", "birth", "dob", "birthday")): return "date_of_birth"
# Content-based heuristics if sample.str.match(r"^[A-Za-z0-9._%+\-]+@[A-Za-z0-9.\-]+\.[A-Za-z]{2,}$").mean() > 0.5: return "email" if sample.str.match(r"^[\+\d\s\-\(\)]{7,15}$").mean() > 0.5: return "phone"
return "generic"
# ── Fake-data generators (deterministic per value) ────────────────────────────def _make_fake(value: str, anon_type: str, cache: dict) -> str: if value in cache: return cache[value]
# Deterministic seed: same value → same fake output seed = abs(hash(value)) % (2**32) fake_local = Faker(["it_IT", "en_US"]) fake_local.seed_instance(seed)
generators = { "email": lambda: fake_local.email(), "first_name": lambda: fake_local.first_name(), "last_name": lambda: fake_local.last_name(), "full_name": lambda: fake_local.name(), "phone": lambda: fake_local.phone_number(), "address": lambda: fake_local.street_address(), "city": lambda: fake_local.city(), "postal_code": lambda: fake_local.postcode(), "tax_id": lambda: _fake_tax_id(fake_local), "date_of_birth": lambda: fake_local.date_of_birth(minimum_age=18, maximum_age=80).strftime("%d/%m/%Y"), "generic": lambda: f"ANONYMIZED_{abs(hash(value)) % 100000:05d}", }
result = generators.get(anon_type, generators["generic"])() cache[value] = result return result
def _fake_tax_id(f: Faker) -> str: """Generate a plausible 16-char alphanumeric tax ID (Italian CF format).""" letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" digits = "0123456789" tax_id = ( "".join(f.random_choices(letters, length=3)) + "".join(f.random_choices(letters, length=3)) + "".join(f.random_choices(digits, length=2)) + f.random_choices(letters, length=1)[0] + "".join(f.random_choices(digits, length=2)) + f.random_choices(letters, length=1)[0] + "".join(f.random_choices(digits, length=3)) + f.random_choices(letters, length=1)[0] ) return tax_id
# ── Main logic ────────────────────────────────────────────────────────────────def load_csv(file_obj) -> tuple: """Load the CSV and return preview + column configuration.""" if file_obj is None: return None, gr.update(visible=False), "Upload a CSV file to get started."
# Gradio 5: file_obj is a path string; Gradio 4: file_obj.name file_path = file_obj if isinstance(file_obj, str) else file_obj.name
try: df = pd.read_csv(file_path, encoding="utf-8", on_bad_lines="skip") except UnicodeDecodeError: df = pd.read_csv(file_path, encoding="latin-1", on_bad_lines="skip") except Exception as e: return None, gr.update(visible=False), f"Error reading the file: {e}"
if df.empty or len(df.columns) == 0: return None, gr.update(visible=False), "The CSV file looks empty or malformed."
# Build column configuration (name, detected type, selected) col_config = pd.DataFrame({ "Column": df.columns.tolist(), "Type": [ANON_TYPES[_detect_type(df[c])] for c in df.columns], "Anonymize": [True] * len(df.columns), })
msg = f"✅ Loaded: **{len(df)} rows × {len(df.columns)} columns**. Review the detected types and click **Anonymize**." return df.to_json(orient="split"), gr.update(value=col_config, visible=True), msg
def anonymize(df_json: Optional[str], col_config_df: Optional[pd.DataFrame]) -> tuple: """Anonymize the DataFrame according to configuration.""" if df_json is None: return None, None, "Please upload a CSV first." if col_config_df is None or len(col_config_df) == 0: return None, None, "Column configuration is missing."
df = pd.read_json(io.StringIO(df_json), orient="split") result = df.copy()
processed = 0 for _, row in col_config_df.iterrows(): col_name = row["Column"] do_anon = row["Anonymize"] type_label = row["Type"]
if not do_anon or col_name not in result.columns: continue
anon_type = TYPE_LABELS.get(type_label, "generic") cache: dict = {}
result[col_name] = result[col_name].apply( lambda v: _make_fake(str(v), anon_type, cache) if pd.notna(v) and str(v).strip() != "" else v ) processed += 1
# Export to CSV csv_bytes = result.to_csv(index=False).encode("utf-8") csv_path = "/tmp/anonymized.csv" with open(csv_path, "wb") as f: f.write(csv_bytes)
msg = f"✅ Anonymized **{processed} columns** out of {len(df.columns)} total." return result.head(10), csv_path, msg
# ── UI ────────────────────────────────────────────────────────────────────────DESCRIPTION = """# 🔒 CSV Data Anonymizer — GDPR Ready
Upload a CSV, review the columns detected as sensitive, and download the anonymized version.The mapping is **deterministic**: the same value always produces the same fake data, preserving dataset consistency."""
with gr.Blocks(theme=gr.themes.Soft(), title="Data Anonymizer") as demo: # Internal state df_state = gr.State(None)
gr.Markdown(DESCRIPTION)
with gr.Row(): with gr.Column(scale=1): file_input = gr.File( label="📂 Upload CSV", file_types=[".csv"], type="filepath", ) status_box = gr.Markdown("Upload a CSV file to get started.")
with gr.Column(scale=2): col_editor = gr.Dataframe( label="⚙️ Column configuration (edit Type and Anonymize)", headers=["Column", "Type", "Anonymize"], datatype=["str", "str", "bool"], col_count=(3, "fixed"), visible=False, interactive=True, wrap=True, )
anon_btn = gr.Button("🔒 Anonymize", variant="primary", size="lg")
gr.Markdown("### Result preview (first 10 rows)") preview_out = gr.Dataframe(label="Anonymized preview", interactive=False, wrap=True)
download_out = gr.File(label="⬇️ Download anonymized CSV", visible=False) result_status = gr.Markdown("")
# ── Event handlers ────────────────────────────────────────────────────── file_input.change( fn=load_csv, inputs=[file_input], outputs=[df_state, col_editor, status_box], )
def run_anonymize(df_json, col_config): preview, path, msg = anonymize(df_json, col_config) file_update = gr.update(value=path, visible=path is not None) return preview, file_update, msg
anon_btn.click( fn=run_anonymize, inputs=[df_state, col_editor], outputs=[preview_out, download_out, result_status], )
gr.Examples( examples=[["example.csv"]], inputs=[file_input], label="📋 Try with the example CSV", run_on_click=False, )
gr.Markdown( """---*The file is processed entirely in memory — no data is saved to disk or transmitted to third parties.* """ )
if __name__ == "__main__": demo.launch()app.pyDeleted-269