Spaces:
Sleeping
Sleeping
File size: 3,249 Bytes
8a83304 |
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 119 120 121 122 123 124 125 |
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() |