|
import gradio as gr |
|
import numpy as np |
|
from PIL import Image |
|
import cv2 |
|
from insightface.app import FaceAnalysis |
|
from huggingface_hub import snapshot_download |
|
import time |
|
import subprocess |
|
import os |
|
|
|
|
|
SECURITYLEVELS = ["128", "196", "256"] |
|
FRMODELS = ["AuraFace-v1"] |
|
EXAMPLE_IMAGES_ENROLL = ['./VGGFace2/n000001/0002_01.jpg', './VGGFace2/n000149/0002_01.jpg', './VGGFace2/n000082/0001_02.jpg', './VGGFace2/n000148/0014_01.jpg'] |
|
EXAMPLE_IMAGES_AUTH = ['./VGGFace2/n000001/0013_01.jpg', './VGGFace2/n000149/0019_01.jpg', './VGGFace2/n000082/0003_03.jpg', './VGGFace2/n000148/0043_01.jpg'] |
|
|
|
|
|
face_app = None |
|
DB_SUBJECT_COUNT = 1 |
|
ENROLLED_SEARCH_IMAGES = [] |
|
|
|
|
|
def initialize_face_app(): |
|
"""Initializes the FaceAnalysis model.""" |
|
global face_app |
|
if face_app is None: |
|
print("Initializing FaceAnalysis model...") |
|
snapshot_download("fal/AuraFace-v1", local_dir="./models/auraface") |
|
face_app = FaceAnalysis(name="auraface", providers=["CPUExecutionProvider"], root=".") |
|
face_app.prepare(ctx_id=0, det_size=(128, 128)) |
|
print("FaceAnalysis model initialized.") |
|
return face_app |
|
|
|
def run_binary(bin_path, *args): |
|
"""Runs a compiled binary file and returns the result.""" |
|
if not os.path.isfile(bin_path): |
|
raise gr.Error(f"Error: Compiled binary not found at {bin_path}") |
|
|
|
command = [bin_path] + [str(arg) for arg in args] |
|
print(f"Running command: {' '.join(command)}") |
|
|
|
try: |
|
os.chmod(bin_path, 0o755) |
|
start_time = time.time() |
|
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=True) |
|
duration = time.time() - start_time |
|
print(f"Binary execution successful. Duration: {duration:.2f}s") |
|
return result.stdout, duration |
|
except subprocess.CalledProcessError as e: |
|
print(f"Error executing binary: {e.stderr}") |
|
raise gr.Error(f"Execution failed: {e.stderr}") |
|
except Exception as e: |
|
print(f"An unexpected error occurred: {e}") |
|
raise gr.Error(f"An unexpected error occurred: {str(e)}") |
|
|
|
def extract_embedding(image_path, mode=None): |
|
"""Extracts face embedding from an image path.""" |
|
if image_path is None: |
|
raise gr.Error("Please upload or select an image first.") |
|
|
|
app = initialize_face_app() |
|
|
|
try: |
|
pil_image = Image.open(image_path).convert("RGB") |
|
except Exception as e: |
|
raise gr.Error(f"Failed to open or read image file: {e}") |
|
|
|
cv2_image = np.array(pil_image) |
|
cv2_image = cv2_image[:, :, ::-1] |
|
faces = app.get(cv2_image) |
|
|
|
if not faces: |
|
raise gr.Error("No face detected. Please try another image.") |
|
|
|
embedding = faces[0].normed_embedding |
|
|
|
if mode: |
|
|
|
if mode in ["enroll", "auth"]: |
|
emb_path = f'./{mode}-emb.txt' |
|
|
|
else: |
|
if "VGGFace2" in image_path: |
|
subject = image_path.split('/')[-2] |
|
else: |
|
subject = 'uploadedSubj' |
|
os.makedirs(f'./embeddings/{subject}', exist_ok=True) |
|
emb_path = f'./embeddings/{subject}/{mode}-emb.txt' |
|
|
|
np.savetxt(emb_path, embedding.reshape(1, -1), fmt="%.6f", delimiter=',') |
|
return embedding.tolist(), emb_path |
|
|
|
return embedding.tolist() |
|
|
|
|
|
def create_image_selection_ui(label, gallery_images): |
|
with gr.Group(): |
|
gr.HTML(f'<h3 class="step-header">{label}</h3>') |
|
image_state = gr.State() |
|
image_display = gr.Image(type="filepath", label="Selected Image", interactive=False) |
|
with gr.Tabs(): |
|
with gr.TabItem("Upload"): |
|
image_upload = gr.Image(type="filepath", label=f"Upload Image") |
|
with gr.TabItem("Select from Gallery"): |
|
image_gallery = gr.Gallery(value=gallery_images, columns=4, height="auto", object_fit="contain") |
|
|
|
|
|
def on_select(evt: gr.SelectData): |
|
selected_image = gallery_images[evt.index] |
|
return selected_image, selected_image |
|
|
|
def on_upload(filepath): |
|
return filepath, filepath |
|
|
|
image_upload.change(on_upload, inputs=image_upload, outputs=[image_state, image_display]) |
|
image_gallery.select(on_select, None, outputs=[image_state, image_display]) |
|
|
|
return image_state |
|
|
|
|
|
css = """ |
|
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap'); |
|
:root { |
|
--background: #EEEEEC; --background-alt: #EEEEEC; --card-bg: #FFFFFF; --card-bg-alt: rgba(255, 208, 134, 0.3); |
|
--foreground: #222; --foreground-muted: #333; --accent-orange: rgb(255, 208, 134); |
|
--accent-gradient: linear-gradient(90deg, var(--accent-orange) 0%, #333333 100%); |
|
--font-sans: 'Inter', Arial, Helvetica, sans-serif; --gray-333: #333333; |
|
} |
|
body, .gradio-container { background: var(--background); color: var(--foreground); font-family: var(--font-sans); font-size: 16px; line-height: 1.6; } |
|
.main-header { padding: 1rem; text-align: center; margin-bottom: 2rem; background: var(--gray-333); color: var(--background); border-radius: 15px; } |
|
.main-header h1 { font-size: 2.5rem; font-weight: 700; color: var(--accent-orange); margin:0; } |
|
.main-header p { font-size: 1.1rem; opacity: 0.9; margin: 0.5rem 0 0 0; } |
|
.main-header a { color: var(--background); text-decoration: none; background: transparent; padding: 0.6rem 1.5rem; border-radius: 25px; border: 1px solid var(--accent-orange); font-weight: 500; transition: all 0.3s ease; display: inline-block; margin-top: 1rem; } |
|
.main-header a:hover { background: var(--accent-orange); color: var(--gray-333); } |
|
.section-header { text-align: center; margin: 2rem 0; padding: 0 1rem; } |
|
.section-header h1 { color: var(--foreground); font-size: 2.2rem; font-weight: 600; margin-bottom: 0.5rem; } |
|
.section-header h2 { color: var(--foreground-muted); font-size: 1.5rem; font-weight: 400; margin: 0; } |
|
.narrative-section { background: var(--card-bg); border-top: 4px solid var(--accent-orange); padding: 2rem; margin: 1.5rem 0; border-radius: 12px; box-shadow: 0 4px 15px rgba(0,0,0,0.05); } |
|
.narrative-header { color: var(--foreground); margin: 0 0 1.5rem 0; font-size: 1.8rem; font-weight: 600; } |
|
.step-header { color: var(--foreground); margin: 0 0 1.5rem 0; font-size: 1.3rem; font-weight: 600; } |
|
.info-card { background: var(--card-bg-alt); border: 1px solid var(--accent-orange); border-radius: 12px; padding: 1.5rem; margin: 1.5rem 0; } |
|
.info-card h3 { color: var(--foreground); margin: 0 0 1rem 0; font-size: 1.3rem; font-weight: 600; } |
|
.info-card p { margin: 0 0 1rem 0; color: var(--foreground-muted); line-height: 1.6; } |
|
.warning-card { background: #ffebee; border: 1px solid #c62828; border-radius: 12px; padding: 1.5rem; margin: 1.5rem 0; } |
|
.warning-card h3 { color: #c62828; margin: 0 0 1rem 0; font-size: 1.3rem; font-weight: 600; } |
|
.warning-card p { margin: 0; color: #424242; line-height: 1.6; } |
|
.result-container { padding: 2rem; border-radius: 15px; text-align: center; margin-top: 1rem; color: white; } |
|
.result-container h2 { margin: 0 0 0.5rem 0; font-size: 2rem; font-weight: 600; color: white; } |
|
.result-container p { margin: 0; opacity: 0.95; font-size: 1rem; } |
|
.match-verified { background: linear-gradient(135deg, #4caf50 0%, #45a049 100%); } |
|
.no-match { background: linear-gradient(135deg, #f44336 0%, #d32f2f 100%); } |
|
.icon-lock { font-size: 4rem; margin: 1rem; } |
|
.status-text { font-size: 1.1rem; color: var(--foreground-muted); margin-top: 1rem; } |
|
""" |
|
|
|
|
|
with gr.Blocks(css=css) as demo: |
|
|
|
gr.HTML(""" |
|
<div class="main-header"> |
|
<h1>Suraksh AI</h1> |
|
<p style="color: #EEEEEC; opacity: 0.9;">The Future of Secure Biometrics</p> |
|
<a href="https://suraksh.ai/" target="_blank">π Visit Our Website</a> |
|
</div> |
|
""") |
|
|
|
|
|
|
|
demo.load(lambda: run_binary("./bin/genKeys.bin", "128", "genkeys"), None, None) |
|
demo.load(lambda: run_binary("./bin/search.bin", "128", "genkeys"), None, None) |
|
|
|
|
|
|
|
with gr.Tabs() as mode_tabs: |
|
|
|
with gr.TabItem("ποΈ Face Recognition (1:1)"): |
|
gr.HTML("""<div class="section-header"><h1>Is this the same person?</h1><h2>A one-to-one verification demo.</h2></div>""") |
|
|
|
with gr.Tabs(): |
|
|
|
with gr.TabItem("π¨ The Vulnerable System"): |
|
with gr.Group(elem_classes="narrative-section"): |
|
gr.HTML('<h2 class="narrative-header">The Problem: How Your Face Can Be Stolen</h2>') |
|
gr.HTML("""<div class="warning-card"><h3>β οΈ Your Biometric Data is Exposed!</h3><p>Most systems handle biometric data in plaintext. This means your facial embeddingβa digital map of your faceβcan be stolen and used to reconstruct your image, creating a major privacy risk.</p></div>""") |
|
|
|
with gr.Column(): |
|
gr.HTML('<h3 class="step-header">1. Original Image</h3>') |
|
gr.Image(value=EXAMPLE_IMAGES_ENROLL[2], label="Original Face", interactive=False, show_label=False, container=False) |
|
|
|
gr.HTML('<h3 class="step-header" style="margin-top: 2rem;">2. Simulate Attack: Steal Data</h3><p>An attacker breaches the system and steals the stored facial embedding. Click the button to simulate this theft.</p>') |
|
extract_btn = gr.Button("π± Steal Biometric Data", variant="primary") |
|
|
|
with gr.Group(visible=False) as stolen_data_group: |
|
feature_output = gr.JSON(label="Stolen Feature Vector (Face Embedding)") |
|
gr.HTML('<h3 class="step-header" style="margin-top: 2rem;">3. Simulate Attack: Reconstruct Face</h3><p>Now, the attacker uses the stolen features to create a reconstruction of the face, completely compromising the user\'s privacy.</p>') |
|
reconstruct_btn = gr.Button("π Reconstruct Face from Stolen Data", variant="stop") |
|
|
|
with gr.Group(visible=False) as reconstructed_image_group: |
|
reconstructed_output = gr.Image(label="Reconstructed Face", interactive=False, show_label=False) |
|
|
|
def extract_and_reveal(image_path): |
|
embedding = extract_embedding(image_path) |
|
feature_json = {"embedding": embedding} |
|
return { |
|
feature_output: feature_json, |
|
stolen_data_group: gr.update(visible=True), |
|
extract_btn: gr.update(value="Data Stolen!", interactive=False) |
|
} |
|
|
|
def show_reconstruction(): |
|
reconstructed_image_path = "./static/reconstructed.png" |
|
return { |
|
reconstructed_output: reconstructed_image_path, |
|
reconstructed_image_group: gr.update(visible=True), |
|
reconstruct_btn: gr.update(interactive=False) |
|
} |
|
|
|
extract_btn.click( |
|
fn=extract_and_reveal, |
|
inputs=gr.State(EXAMPLE_IMAGES_ENROLL[0]), |
|
outputs=[feature_output, stolen_data_group, extract_btn] |
|
) |
|
|
|
reconstruct_btn.click( |
|
fn=show_reconstruction, |
|
inputs=None, |
|
outputs=[reconstructed_output, reconstructed_image_group, reconstruct_btn] |
|
) |
|
|
|
|
|
with gr.TabItem("β
The Suraksh.AI Solution"): |
|
with gr.Group(elem_classes="narrative-section"): |
|
gr.HTML('<h2 class="narrative-header">The Solution: Verification with FHE</h2>') |
|
gr.HTML("""<div class="info-card"><h3>The Locked Box Analogy</h3><p>With Suraksh.AI, your biometric data is encrypted inside a "locked box" before it ever leaves your device. We can perform the verification on the encrypted data without ever seeing your real face. It's mathematically impossible for us to decrypt it.</p></div>""") |
|
|
|
with gr.Row(): |
|
with gr.Column(): |
|
rec_ref_img = create_image_selection_ui("1. Provide Reference Image", EXAMPLE_IMAGES_ENROLL) |
|
with gr.Group(visible=False) as rec_ref_features_group: |
|
rec_ref_raw_features = gr.JSON(label="Raw Features (Plaintext)") |
|
rec_ref_encrypted_features = gr.Textbox(label="Encrypted Features (Ciphertext)", interactive=False, lines=5) |
|
with gr.Column(): |
|
rec_probe_img = create_image_selection_ui("2. Provide Probe Image", EXAMPLE_IMAGES_AUTH) |
|
with gr.Group(visible=False) as rec_probe_features_group: |
|
rec_probe_raw_features = gr.JSON(label="Raw Features (Plaintext)") |
|
rec_probe_encrypted_features = gr.Textbox(label="Encrypted Features (Ciphertext)", interactive=False, lines=5) |
|
|
|
with gr.Accordion("Advanced Settings", open=False): |
|
rec_threshold = gr.Slider(-512*5, 512*5, value=133, label="Match Strictness", info="A higher value means a stricter match is required.") |
|
rec_sec_level = gr.Dropdown(SECURITYLEVELS, value="128", label="Security Level") |
|
|
|
rec_run_btn = gr.Button("π Perform Secure 1:1 Match", variant="primary", size="lg") |
|
|
|
rec_status = gr.HTML(elem_classes="status-text") |
|
rec_result = gr.HTML() |
|
|
|
def secure_recognition_flow(ref_img, probe_img, threshold, sec_level): |
|
|
|
yield "Initializing...", "", gr.update(visible=False), None, None, gr.update(visible=False), None, None |
|
|
|
|
|
yield "Extracting reference features...", "", gr.update(visible=False), None, None, gr.update(visible=False), None, None |
|
ref_emb, _ = extract_embedding(ref_img, "enroll") |
|
|
|
yield "Encrypting reference features...", "", gr.update(visible=True), {"embedding": ref_emb}, None, gr.update(visible=False), None, None |
|
run_binary("./bin/encReference.bin", sec_level, "encrypt") |
|
ref_ciphertext, _ = run_binary("./bin/encReference.bin", sec_level, "print") |
|
|
|
|
|
|
|
yield "β
Reference Encrypted. Extracting probe features...", "", gr.update(visible=True), {"embedding": ref_emb}, ref_ciphertext, gr.update(visible=False), None, None |
|
probe_emb, _ = extract_embedding(probe_img, "auth") |
|
|
|
yield "Encrypting probe features...", "", gr.update(visible=True), {"embedding": ref_emb}, ref_ciphertext, gr.update(visible=True), {"embedding": probe_emb}, None |
|
run_binary("./bin/encProbe.bin", sec_level, "encrypt") |
|
probe_ciphertext, _ = run_binary("./bin/encProbe.bin", sec_level, "print") |
|
|
|
|
|
yield "β
Probe Encrypted. Performing Secure Match...", "", gr.update(visible=True), {"embedding": ref_emb}, ref_ciphertext, gr.update(visible=True), {"embedding": probe_emb}, probe_ciphertext |
|
run_binary("./bin/recDecision.bin", sec_level, "decision", threshold) |
|
|
|
yield "β
Match Computed. Decrypting Result...", "", gr.update(visible=True), {"embedding": ref_emb}, ref_ciphertext, gr.update(visible=True), {"embedding": probe_emb}, probe_ciphertext |
|
output, _ = run_binary("./bin/decDecision.bin", sec_level, "decision") |
|
|
|
if output.strip().lower() == "match": |
|
result_html = f"""<div class="result-container match-verified"><h2>β
MATCH VERIFIED</h2><p>Identity successfully confirmed under FHE.</p></div>""" |
|
else: |
|
result_html = f"""<div class="result-container no-match"><h2>β NO MATCH</h2><p>Identity verification failed.</p></div>""" |
|
yield "Done!", result_html, gr.update(visible=True), {"embedding": ref_emb}, ref_ciphertext, gr.update(visible=True), {"embedding": probe_emb}, probe_ciphertext |
|
|
|
rec_run_btn.click( |
|
fn=secure_recognition_flow, |
|
inputs=[rec_ref_img, rec_probe_img, rec_threshold, rec_sec_level], |
|
outputs=[rec_status, rec_result, rec_ref_features_group, rec_ref_raw_features, rec_ref_encrypted_features, rec_probe_features_group, rec_probe_raw_features, rec_probe_encrypted_features] |
|
) |
|
|
|
|
|
with gr.TabItem("π Face Search (1:N)"): |
|
gr.HTML("""<div class="section-header"><h1>Who is this person?</h1><h2>A one-to-many search demo against an encrypted database.</h2></div>""") |
|
|
|
with gr.Tabs(): |
|
|
|
with gr.TabItem("β
The Suraksh.AI Solution"): |
|
with gr.Group(elem_classes="narrative-section"): |
|
gr.HTML('<h2 class="narrative-header">Building and Searching a Secure Database</h2>') |
|
gr.HTML("""<div class="info-card"><h3>From Verification to Identification</h3><p>This demo shows how FHE can be used to search for a person in a database without ever decrypting the database itself. This is ideal for large-scale, privacy-preserving identification systems.</p></div>""") |
|
|
|
with gr.Row(): |
|
with gr.Column(): |
|
gr.HTML('<h3 class="step-header">1. Enroll Subjects into DB</h3>') |
|
search_enroll_img = create_image_selection_ui("Select Image to Enroll", EXAMPLE_IMAGES_ENROLL) |
|
search_enroll_btn = gr.Button("β Encrypt & Add to Database", variant="secondary") |
|
with gr.Group(visible=False) as enroll_features_group: |
|
enroll_raw_features = gr.JSON(label="Raw Features (Plaintext)") |
|
enroll_encrypted_features = gr.Textbox(label="Encrypted Features (Ciphertext)", interactive=False, lines=5) |
|
search_enroll_status = gr.HTML() |
|
|
|
with gr.Column(): |
|
gr.HTML('<h3 class="step-header">2. Search for a Subject</h3>') |
|
search_probe_img = create_image_selection_ui("Select Image to Search", EXAMPLE_IMAGES_AUTH) |
|
search_run_btn = gr.Button("π Perform Secure 1:N Search", variant="primary", size="lg") |
|
with gr.Group(visible=False) as search_features_group: |
|
search_raw_features = gr.JSON(label="Raw Features (Plaintext)") |
|
search_encrypted_features = gr.Textbox(label="Encrypted Features (Ciphertext)", interactive=False, lines=5) |
|
search_status = gr.HTML(elem_classes="status-text") |
|
|
|
search_result = gr.HTML() |
|
search_result_image = gr.Image(label="Found Reference Image", interactive=False, visible=False) |
|
|
|
with gr.Accordion("Advanced Settings", open=False): |
|
search_threshold = gr.Slider(-512*5, 512*5, value=133, label="Match Strictness") |
|
search_sec_level = gr.Dropdown(SECURITYLEVELS, value="128", label="Security Level") |
|
|
|
def secure_enroll_flow(image, sec_level): |
|
global DB_SUBJECT_COUNT, ENROLLED_SEARCH_IMAGES |
|
if image is None: raise gr.Error("Please provide an image to enroll.") |
|
|
|
current_id = DB_SUBJECT_COUNT |
|
|
|
yield "Extracting features...", gr.update(visible=False), None, None |
|
embedding, emb_path = extract_embedding(image, "search_enroll") |
|
|
|
yield "Encrypting features...", gr.update(visible=True), {"embedding": embedding}, None |
|
run_binary("./bin/search.bin", sec_level, "encRef", emb_path, current_id) |
|
ciphertext, _ = run_binary("./bin/search.bin", sec_level, "printVectorCipher", "encRef", "print") |
|
|
|
yield "Adding to secure database...", gr.update(visible=True), {"embedding": embedding}, ciphertext |
|
run_binary("./bin/search.bin", sec_level, "addRef") |
|
|
|
ENROLLED_SEARCH_IMAGES.append(image) |
|
DB_SUBJECT_COUNT += 1 |
|
yield f"β
Subject with ID {current_id} added. Total subjects: {DB_SUBJECT_COUNT - 1}.", gr.update(visible=True), {"embedding": embedding}, ciphertext |
|
|
|
def secure_search_flow(image, threshold, sec_level): |
|
global ENROLLED_SEARCH_IMAGES |
|
if image is None: raise gr.Error("Please provide an image to search.") |
|
|
|
yield "Extracting probe features...", "", gr.update(visible=False), None, None, gr.update(visible=False) |
|
embedding, emb_path = extract_embedding(image, "search_auth") |
|
|
|
yield "Encrypting probe features...", "", gr.update(visible=True), {"embedding": embedding}, None, gr.update(visible=False) |
|
run_binary("./bin/search.bin", sec_level, "encProbe", emb_path) |
|
ciphertext, _ = run_binary("./bin/search.bin", sec_level, "printProbe", "print") |
|
|
|
yield "β
Probe encrypted. Searching database...", "", gr.update(visible=True), {"embedding": embedding}, ciphertext, gr.update(visible=False) |
|
run_binary("./bin/search.bin", sec_level, "search") |
|
|
|
yield "β
Search complete. Decrypting results...", "", gr.update(visible=True), {"embedding": embedding}, ciphertext, gr.update(visible=False) |
|
|
|
output, _ = run_binary("./bin/search.bin", sec_level, "decScoreClear", threshold) |
|
print(f"Search binary output: >>>{output}<<<") |
|
|
|
lines = output.strip().split('\n') |
|
decision = lines[0].lower() |
|
|
|
if decision == "found": |
|
try: |
|
|
|
found_id_line = next(line for line in lines if "id:" in line.lower()) |
|
found_id = int(found_id_line.split(':')[1].strip()) |
|
|
|
|
|
found_image_path = ENROLLED_SEARCH_IMAGES[found_id - 1] |
|
|
|
result_html = f"""<div class="result-container match-verified"><h2>β
SUBJECT FOUND</h2><p>The subject was successfully found in the database with ID {found_id}.</p></div>""" |
|
result_image_update = gr.update(value=found_image_path, visible=True) |
|
|
|
except (StopIteration, IndexError, ValueError): |
|
result_html = f"""<div class="result-container match-verified"><h2>β
SUBJECT FOUND</h2><p>Could not parse ID from binary output: {output.strip()}</p></div>""" |
|
result_image_update = gr.update(visible=False) |
|
else: |
|
result_html = """<div class="result-container no-match"><h2>β NOT FOUND</h2><p>The subject was not found in the database.</p></div>""" |
|
result_image_update = gr.update(visible=False) |
|
|
|
yield "Done!", result_html, gr.update(visible=True), {"embedding": embedding}, ciphertext, result_image_update |
|
|
|
search_enroll_btn.click( |
|
fn=secure_enroll_flow, |
|
inputs=[search_enroll_img, search_sec_level], |
|
outputs=[search_enroll_status, enroll_features_group, enroll_raw_features, enroll_encrypted_features] |
|
) |
|
|
|
search_run_btn.click( |
|
fn=secure_search_flow, |
|
inputs=[search_probe_img, search_threshold, search_sec_level], |
|
outputs=[search_status, search_result, search_features_group, search_raw_features, search_encrypted_features, search_result_image] |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
demo.launch() |