import hashlib import json import textwrap import gradio as gr # ---------------------------- # Helpers # ---------------------------- def canonicalize(text: str) -> str: """ Canonicalize input so the demo behaves consistently. - Strips trailing spaces per line - Normalizes newlines """ if text is None: return "" lines = [ln.rstrip() for ln in text.splitlines()] return "\n".join(lines).strip() def fingerprint(text: str) -> str: """ Returns a SHA-256 fingerprint of the canonicalized text. This is presented as a 'digital fingerprint' (no crypto jargon). """ c = canonicalize(text) h = hashlib.sha256(c.encode("utf-8")).hexdigest() return f"sha256:{h}" def compare(original: str, edited: str): o = canonicalize(original) e = canonicalize(edited) if not o: return "", "", "Add an original text to generate a fingerprint." fp_o = fingerprint(o) fp_e = fingerprint(e) if not edited.strip(): status = "Edit the right side and watch what happens to verification." elif fp_o == fp_e: status = "✅ MATCH — the text is unchanged (verification passes)." else: status = "❌ TAMPER DETECTED — the fingerprint changed (verification fails)." return fp_o, fp_e, status # ---------------------------- # Default demo content (safe + generic) # ---------------------------- DEFAULT_RECEIPT = textwrap.dedent(""" RECEIPT (DEMO) Type: Agreement Summary Created: 2025-12-14 Parties: [REDACTED] ↔ [REDACTED] Statement: "Service will be delivered by Friday. Payment due upon delivery." Notes: This is an educational demo. It certifies integrity (unchanged text), not truth. """).strip() # ---------------------------- # UI # ---------------------------- with gr.Blocks(title="Test It Yourself — Verification Demo") as demo: gr.Markdown( """ # Test It Yourself This is a simple educational demo: - The left side is the **original** text. - The right side is the **edited** text. - The system generates a **digital fingerprint** for each. - Change anything on the right → the fingerprint changes → verification fails. **No accounts. No trust required.** """.strip() ) with gr.Row(): original = gr.Textbox( label="Original (demo text)", value=DEFAULT_RECEIPT, lines=14 ) edited = gr.Textbox( label="Edited (try changing one word)", value=DEFAULT_RECEIPT, lines=14 ) with gr.Row(): fp_original = gr.Textbox(label="Original fingerprint", interactive=False) fp_edited = gr.Textbox(label="Edited fingerprint", interactive=False) status = gr.Markdown() gr.Button("Compare / Verify").click( compare, inputs=[original, edited], outputs=[fp_original, fp_edited, status] ) gr.Markdown( """ --- ### What this demo is (and isn’t) - **This demo shows integrity:** whether text changed. - **It does not prove truth:** it can’t verify whether the statement is real. - Production-grade verification systems enforce durability and refusal behavior beyond this demo. """.strip() ) demo.launch()