Vishnu Naresh Boddeti commited on
Commit
3ec6126
Β·
1 Parent(s): 6b3069c

initial commit

Browse files
README.md CHANGED
@@ -1,8 +1,8 @@
1
  ---
2
  title: Encrypted Face Matching And Search
3
  emoji: πŸ”₯
4
- colorFrom: gray
5
- colorTo: green
6
  sdk: gradio
7
  sdk_version: 5.38.2
8
  app_file: app.py
 
1
  ---
2
  title: Encrypted Face Matching And Search
3
  emoji: πŸ”₯
4
+ colorFrom: yellow
5
+ colorTo: red
6
  sdk: gradio
7
  sdk_version: 5.38.2
8
  app_file: app.py
VGGFace2/n000001/0002_01.jpg ADDED

Git LFS Details

  • SHA256: fefd04f3c37e07951103cbd4b928b902bbb9eb9b135d91b9be8b568833522c08
  • Pointer size: 129 Bytes
  • Size of remote file: 7.11 kB
VGGFace2/n000001/0013_01.jpg ADDED

Git LFS Details

  • SHA256: 92cf5daba0697dcd1df56837512d19299f503d2ffc1e2d83adb122dfca761813
  • Pointer size: 129 Bytes
  • Size of remote file: 7.32 kB
VGGFace2/n000082/0001_02.jpg ADDED

Git LFS Details

  • SHA256: da1923031611d2ea7933b2621364c682f56818681df36d4dad61e01346e4a2bb
  • Pointer size: 129 Bytes
  • Size of remote file: 5.81 kB
VGGFace2/n000082/0003_03.jpg ADDED

Git LFS Details

  • SHA256: 771b4ed52288112cbcfd7dfe0029ba4fd3dddd8c6908a065748b045a6c2d9dc6
  • Pointer size: 129 Bytes
  • Size of remote file: 6.13 kB
VGGFace2/n000129/0001_01.jpg ADDED

Git LFS Details

  • SHA256: 6ffd30dc9050bd8e7bd5bbb558acc344c1f6637ca61bac578440bfd8c021ee58
  • Pointer size: 129 Bytes
  • Size of remote file: 6.53 kB
VGGFace2/n000129/0006_01.jpg ADDED

Git LFS Details

  • SHA256: 9d9ebb54bc813f3911dcf5ff7ac59b7c49fdf0d519d629d2c276fb87ba12be74
  • Pointer size: 129 Bytes
  • Size of remote file: 6.26 kB
VGGFace2/n000148/0014_01.jpg ADDED

Git LFS Details

  • SHA256: 383de9a536a8666c142f1947028497db8f7ea7b57d6bdcb4407cf4c01051a39b
  • Pointer size: 129 Bytes
  • Size of remote file: 5.53 kB
VGGFace2/n000148/0043_01.jpg ADDED

Git LFS Details

  • SHA256: e2b4fed09c2e7427d6b722efec7faac25e87001f4656ddf138bf32df1cc54f4a
  • Pointer size: 129 Bytes
  • Size of remote file: 6.04 kB
VGGFace2/n000149/0002_01.jpg ADDED

Git LFS Details

  • SHA256: 4c06b3c5265ee7eda7a0bc6771904b8725df344323097fa49167c4f7e4d8a50d
  • Pointer size: 129 Bytes
  • Size of remote file: 7.97 kB
VGGFace2/n000149/0019_01.jpg ADDED

Git LFS Details

  • SHA256: 920730300e5b06d303dcf81cf9b290ecc7a35f9f71c9cee8629af2adeb03de99
  • Pointer size: 129 Bytes
  • Size of remote file: 6.54 kB
VGGFace2/n000394/0007_01.jpg ADDED

Git LFS Details

  • SHA256: 20da2edff7d2497930f03c37d4d22126308bdf17f72d970a3883e2929125e318
  • Pointer size: 129 Bytes
  • Size of remote file: 6.81 kB
VGGFace2/n000394/0018_01.jpg ADDED

Git LFS Details

  • SHA256: 3a3963e0e65951aea19cc52713a32c1ea1a530027c25c357af7d016997e94091
  • Pointer size: 129 Bytes
  • Size of remote file: 6.62 kB
app.py ADDED
@@ -0,0 +1,411 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ from PIL import Image
4
+ import cv2
5
+ from insightface.app import FaceAnalysis
6
+ from huggingface_hub import snapshot_download
7
+ import time
8
+ import subprocess
9
+ import os
10
+
11
+ # --- Configuration ---
12
+ SECURITYLEVELS = ["128", "196", "256"]
13
+ FRMODELS = ["AuraFace-v1"]
14
+ EXAMPLE_IMAGES_ENROLL = ['./VGGFace2/n000001/0002_01.jpg', './VGGFace2/n000149/0002_01.jpg', './VGGFace2/n000082/0001_02.jpg', './VGGFace2/n000148/0014_01.jpg']
15
+ EXAMPLE_IMAGES_AUTH = ['./VGGFace2/n000001/0013_01.jpg', './VGGFace2/n000149/0019_01.jpg', './VGGFace2/n000082/0003_03.jpg', './VGGFace2/n000148/0043_01.jpg']
16
+
17
+ # --- Global Variables ---
18
+ face_app = None
19
+ DB_SUBJECT_COUNT = 1
20
+ ENROLLED_SEARCH_IMAGES = []
21
+
22
+ # --- Helper Functions ---
23
+ def initialize_face_app():
24
+ """Initializes the FaceAnalysis model."""
25
+ global face_app
26
+ if face_app is None:
27
+ print("Initializing FaceAnalysis model...")
28
+ snapshot_download("fal/AuraFace-v1", local_dir="./models/auraface")
29
+ face_app = FaceAnalysis(name="auraface", providers=["CPUExecutionProvider"], root=".")
30
+ face_app.prepare(ctx_id=0, det_size=(128, 128))
31
+ print("FaceAnalysis model initialized.")
32
+ return face_app
33
+
34
+ def run_binary(bin_path, *args):
35
+ """Runs a compiled binary file and returns the result."""
36
+ if not os.path.isfile(bin_path):
37
+ raise gr.Error(f"Error: Compiled binary not found at {bin_path}")
38
+
39
+ command = [bin_path] + [str(arg) for arg in args]
40
+ print(f"Running command: {' '.join(command)}")
41
+
42
+ try:
43
+ os.chmod(bin_path, 0o755)
44
+ start_time = time.time()
45
+ result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=True)
46
+ duration = time.time() - start_time
47
+ print(f"Binary execution successful. Duration: {duration:.2f}s")
48
+ return result.stdout, duration
49
+ except subprocess.CalledProcessError as e:
50
+ print(f"Error executing binary: {e.stderr}")
51
+ raise gr.Error(f"Execution failed: {e.stderr}")
52
+ except Exception as e:
53
+ print(f"An unexpected error occurred: {e}")
54
+ raise gr.Error(f"An unexpected error occurred: {str(e)}")
55
+
56
+ def extract_embedding(image_path, mode=None):
57
+ """Extracts face embedding from an image path."""
58
+ if image_path is None:
59
+ raise gr.Error("Please upload or select an image first.")
60
+
61
+ app = initialize_face_app()
62
+
63
+ try:
64
+ pil_image = Image.open(image_path).convert("RGB")
65
+ except Exception as e:
66
+ raise gr.Error(f"Failed to open or read image file: {e}")
67
+
68
+ cv2_image = np.array(pil_image)
69
+ cv2_image = cv2_image[:, :, ::-1]
70
+ faces = app.get(cv2_image)
71
+
72
+ if not faces:
73
+ raise gr.Error("No face detected. Please try another image.")
74
+
75
+ embedding = faces[0].normed_embedding
76
+
77
+ if mode:
78
+ # For 1:1 recognition, save to the respective binary folder
79
+ if mode in ["enroll", "auth"]:
80
+ emb_path = f'./{mode}-emb.txt'
81
+ # For 1:N search, create a subject-specific path in the search folder
82
+ else: # search_enroll, search_auth
83
+ if "VGGFace2" in image_path:
84
+ subject = image_path.split('/')[-2]
85
+ else:
86
+ subject = 'uploadedSubj'
87
+ os.makedirs(f'./embeddings/{subject}', exist_ok=True)
88
+ emb_path = f'./embeddings/{subject}/{mode}-emb.txt'
89
+
90
+ np.savetxt(emb_path, embedding.reshape(1, -1), fmt="%.6f", delimiter=',')
91
+ return embedding.tolist(), emb_path
92
+
93
+ return embedding.tolist()
94
+
95
+ # --- UI Components ---
96
+ def create_image_selection_ui(label, gallery_images):
97
+ with gr.Group():
98
+ gr.HTML(f'<h3 class="step-header">{label}</h3>')
99
+ image_state = gr.State()
100
+ image_display = gr.Image(type="filepath", label="Selected Image", interactive=False)
101
+ with gr.Tabs():
102
+ with gr.TabItem("Upload"):
103
+ image_upload = gr.Image(type="filepath", label=f"Upload Image")
104
+ with gr.TabItem("Select from Gallery"):
105
+ image_gallery = gr.Gallery(value=gallery_images, columns=4, height="auto", object_fit="contain")
106
+
107
+ # Event handlers that directly update both the hidden state and the visible display
108
+ def on_select(evt: gr.SelectData):
109
+ selected_image = gallery_images[evt.index] # Get the actual image path from the gallery list
110
+ return selected_image, selected_image
111
+
112
+ def on_upload(filepath):
113
+ return filepath, filepath
114
+
115
+ image_upload.change(on_upload, inputs=image_upload, outputs=[image_state, image_display])
116
+ image_gallery.select(on_select, None, outputs=[image_state, image_display])
117
+
118
+ return image_state
119
+
120
+ # --- UI Styling and Theming ---
121
+ css = """
122
+ @import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');
123
+ :root {
124
+ --background: #EEEEEC; --background-alt: #EEEEEC; --card-bg: #FFFFFF; --card-bg-alt: rgba(255, 208, 134, 0.3);
125
+ --foreground: #222; --foreground-muted: #333; --accent-orange: rgb(255, 208, 134);
126
+ --accent-gradient: linear-gradient(90deg, var(--accent-orange) 0%, #333333 100%);
127
+ --font-sans: 'Inter', Arial, Helvetica, sans-serif; --gray-333: #333333;
128
+ }
129
+ body, .gradio-container { background: var(--background); color: var(--foreground); font-family: var(--font-sans); font-size: 16px; line-height: 1.6; }
130
+ .main-header { padding: 1rem; text-align: center; margin-bottom: 2rem; background: var(--gray-333); color: var(--background); border-radius: 15px; }
131
+ .main-header h1 { font-size: 2.5rem; font-weight: 700; color: var(--accent-orange); margin:0; }
132
+ .main-header p { font-size: 1.1rem; opacity: 0.9; margin: 0.5rem 0 0 0; }
133
+ .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; }
134
+ .main-header a:hover { background: var(--accent-orange); color: var(--gray-333); }
135
+ .section-header { text-align: center; margin: 2rem 0; padding: 0 1rem; }
136
+ .section-header h1 { color: var(--foreground); font-size: 2.2rem; font-weight: 600; margin-bottom: 0.5rem; }
137
+ .section-header h2 { color: var(--foreground-muted); font-size: 1.5rem; font-weight: 400; margin: 0; }
138
+ .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); }
139
+ .narrative-header { color: var(--foreground); margin: 0 0 1.5rem 0; font-size: 1.8rem; font-weight: 600; }
140
+ .step-header { color: var(--foreground); margin: 0 0 1.5rem 0; font-size: 1.3rem; font-weight: 600; }
141
+ .info-card { background: var(--card-bg-alt); border: 1px solid var(--accent-orange); border-radius: 12px; padding: 1.5rem; margin: 1.5rem 0; }
142
+ .info-card h3 { color: var(--foreground); margin: 0 0 1rem 0; font-size: 1.3rem; font-weight: 600; }
143
+ .info-card p { margin: 0 0 1rem 0; color: var(--foreground-muted); line-height: 1.6; }
144
+ .warning-card { background: #ffebee; border: 1px solid #c62828; border-radius: 12px; padding: 1.5rem; margin: 1.5rem 0; }
145
+ .warning-card h3 { color: #c62828; margin: 0 0 1rem 0; font-size: 1.3rem; font-weight: 600; }
146
+ .warning-card p { margin: 0; color: #424242; line-height: 1.6; }
147
+ .result-container { padding: 2rem; border-radius: 15px; text-align: center; margin-top: 1rem; color: white; }
148
+ .result-container h2 { margin: 0 0 0.5rem 0; font-size: 2rem; font-weight: 600; color: white; }
149
+ .result-container p { margin: 0; opacity: 0.95; font-size: 1rem; }
150
+ .match-verified { background: linear-gradient(135deg, #4caf50 0%, #45a049 100%); }
151
+ .no-match { background: linear-gradient(135deg, #f44336 0%, #d32f2f 100%); }
152
+ .icon-lock { font-size: 4rem; margin: 1rem; }
153
+ .status-text { font-size: 1.1rem; color: var(--foreground-muted); margin-top: 1rem; }
154
+ """
155
+
156
+ # --- Gradio UI Definition ---
157
+ with gr.Blocks(css=css) as demo:
158
+ # --- Header ---
159
+ gr.HTML("""
160
+ <div class="main-header">
161
+ <h1>Suraksh AI</h1>
162
+ <p style="color: #EEEEEC; opacity: 0.9;">The Future of Secure Biometrics</p>
163
+ <a href="https://suraksh.ai/" target="_blank">🌐 Visit Our Website</a>
164
+ </div>
165
+ """)
166
+
167
+ # --- Key Generation on Load ---
168
+ # Generate keys once for each demo when the app starts up
169
+ demo.load(lambda: run_binary("./bin/genKeys.bin", "128", "genkeys"), None, None)
170
+ demo.load(lambda: run_binary("./bin/search.bin", "128", "genkeys"), None, None)
171
+
172
+
173
+ # --- Main Tabs for Demo Mode ---
174
+ with gr.Tabs() as mode_tabs:
175
+ # --- 1:1 Recognition Demo ---
176
+ with gr.TabItem("πŸ‘οΈ Face Recognition (1:1)"):
177
+ gr.HTML("""<div class="section-header"><h1>Is this the same person?</h1><h2>A one-to-one verification demo.</h2></div>""")
178
+
179
+ with gr.Tabs():
180
+ # --- Vulnerable System Tab ---
181
+ with gr.TabItem("🚨 The Vulnerable System"):
182
+ with gr.Group(elem_classes="narrative-section"):
183
+ gr.HTML('<h2 class="narrative-header">The Problem: How Your Face Can Be Stolen</h2>')
184
+ 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>""")
185
+
186
+ with gr.Column():
187
+ gr.HTML('<h3 class="step-header">1. Original Image</h3>')
188
+ gr.Image(value=EXAMPLE_IMAGES_ENROLL[2], label="Original Face", interactive=False, show_label=False, container=False)
189
+
190
+ 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>')
191
+ extract_btn = gr.Button("😱 Steal Biometric Data", variant="primary")
192
+
193
+ with gr.Group(visible=False) as stolen_data_group:
194
+ feature_output = gr.JSON(label="Stolen Feature Vector (Face Embedding)")
195
+ 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>')
196
+ reconstruct_btn = gr.Button("🎭 Reconstruct Face from Stolen Data", variant="stop")
197
+
198
+ with gr.Group(visible=False) as reconstructed_image_group:
199
+ reconstructed_output = gr.Image(label="Reconstructed Face", interactive=False, show_label=False)
200
+
201
+ def extract_and_reveal(image_path):
202
+ embedding = extract_embedding(image_path)
203
+ feature_json = {"embedding": embedding}
204
+ return {
205
+ feature_output: feature_json,
206
+ stolen_data_group: gr.update(visible=True),
207
+ extract_btn: gr.update(value="Data Stolen!", interactive=False)
208
+ }
209
+
210
+ def show_reconstruction():
211
+ reconstructed_image_path = "./static/reconstructed.png"
212
+ return {
213
+ reconstructed_output: reconstructed_image_path,
214
+ reconstructed_image_group: gr.update(visible=True),
215
+ reconstruct_btn: gr.update(interactive=False)
216
+ }
217
+
218
+ extract_btn.click(
219
+ fn=extract_and_reveal,
220
+ inputs=gr.State(EXAMPLE_IMAGES_ENROLL[0]),
221
+ outputs=[feature_output, stolen_data_group, extract_btn]
222
+ )
223
+
224
+ reconstruct_btn.click(
225
+ fn=show_reconstruction,
226
+ inputs=None,
227
+ outputs=[reconstructed_output, reconstructed_image_group, reconstruct_btn]
228
+ )
229
+
230
+ # --- Secure System Tab ---
231
+ with gr.TabItem("βœ… The Suraksh.AI Solution"):
232
+ with gr.Group(elem_classes="narrative-section"):
233
+ gr.HTML('<h2 class="narrative-header">The Solution: Verification with FHE</h2>')
234
+ 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>""")
235
+
236
+ with gr.Row():
237
+ with gr.Column():
238
+ rec_ref_img = create_image_selection_ui("1. Provide Reference Image", EXAMPLE_IMAGES_ENROLL)
239
+ with gr.Group(visible=False) as rec_ref_features_group:
240
+ rec_ref_raw_features = gr.JSON(label="Raw Features (Plaintext)")
241
+ rec_ref_encrypted_features = gr.Textbox(label="Encrypted Features (Ciphertext)", interactive=False, lines=5)
242
+ with gr.Column():
243
+ rec_probe_img = create_image_selection_ui("2. Provide Probe Image", EXAMPLE_IMAGES_AUTH)
244
+ with gr.Group(visible=False) as rec_probe_features_group:
245
+ rec_probe_raw_features = gr.JSON(label="Raw Features (Plaintext)")
246
+ rec_probe_encrypted_features = gr.Textbox(label="Encrypted Features (Ciphertext)", interactive=False, lines=5)
247
+
248
+ with gr.Accordion("Advanced Settings", open=False):
249
+ rec_threshold = gr.Slider(-512*5, 512*5, value=133, label="Match Strictness", info="A higher value means a stricter match is required.")
250
+ rec_sec_level = gr.Dropdown(SECURITYLEVELS, value="128", label="Security Level")
251
+
252
+ rec_run_btn = gr.Button("πŸš€ Perform Secure 1:1 Match", variant="primary", size="lg")
253
+
254
+ rec_status = gr.HTML(elem_classes="status-text")
255
+ rec_result = gr.HTML()
256
+
257
+ def secure_recognition_flow(ref_img, probe_img, threshold, sec_level):
258
+ # Reset UI
259
+ yield "Initializing...", "", gr.update(visible=False), None, None, gr.update(visible=False), None, None
260
+
261
+ # Process Reference Image
262
+ yield "Extracting reference features...", "", gr.update(visible=False), None, None, gr.update(visible=False), None, None
263
+ ref_emb, _ = extract_embedding(ref_img, "enroll")
264
+
265
+ yield "Encrypting reference features...", "", gr.update(visible=True), {"embedding": ref_emb}, None, gr.update(visible=False), None, None
266
+ run_binary("./bin/encReference.bin", sec_level, "encrypt")
267
+ ref_ciphertext, _ = run_binary("./bin/encReference.bin", sec_level, "print")
268
+
269
+
270
+ # Process Probe Image
271
+ yield "βœ… Reference Encrypted. Extracting probe features...", "", gr.update(visible=True), {"embedding": ref_emb}, ref_ciphertext, gr.update(visible=False), None, None
272
+ probe_emb, _ = extract_embedding(probe_img, "auth")
273
+
274
+ yield "Encrypting probe features...", "", gr.update(visible=True), {"embedding": ref_emb}, ref_ciphertext, gr.update(visible=True), {"embedding": probe_emb}, None
275
+ run_binary("./bin/encProbe.bin", sec_level, "encrypt")
276
+ probe_ciphertext, _ = run_binary("./bin/encProbe.bin", sec_level, "print")
277
+
278
+ # Perform Match
279
+ yield "βœ… Probe Encrypted. Performing Secure Match...", "", gr.update(visible=True), {"embedding": ref_emb}, ref_ciphertext, gr.update(visible=True), {"embedding": probe_emb}, probe_ciphertext
280
+ run_binary("./bin/recDecision.bin", sec_level, "decision", threshold)
281
+
282
+ yield "βœ… Match Computed. Decrypting Result...", "", gr.update(visible=True), {"embedding": ref_emb}, ref_ciphertext, gr.update(visible=True), {"embedding": probe_emb}, probe_ciphertext
283
+ output, _ = run_binary("./bin/decDecision.bin", sec_level, "decision")
284
+
285
+ if output.strip().lower() == "match":
286
+ result_html = f"""<div class="result-container match-verified"><h2>βœ… MATCH VERIFIED</h2><p>Identity successfully confirmed under FHE.</p></div>"""
287
+ else:
288
+ result_html = f"""<div class="result-container no-match"><h2>❌ NO MATCH</h2><p>Identity verification failed.</p></div>"""
289
+ yield "Done!", result_html, gr.update(visible=True), {"embedding": ref_emb}, ref_ciphertext, gr.update(visible=True), {"embedding": probe_emb}, probe_ciphertext
290
+
291
+ rec_run_btn.click(
292
+ fn=secure_recognition_flow,
293
+ inputs=[rec_ref_img, rec_probe_img, rec_threshold, rec_sec_level],
294
+ 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]
295
+ )
296
+
297
+ # --- 1:N Search Demo ---
298
+ with gr.TabItem("πŸ” Face Search (1:N)"):
299
+ 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>""")
300
+
301
+ with gr.Tabs():
302
+ # --- Secure System Tab ---
303
+ with gr.TabItem("βœ… The Suraksh.AI Solution"):
304
+ with gr.Group(elem_classes="narrative-section"):
305
+ gr.HTML('<h2 class="narrative-header">Building and Searching a Secure Database</h2>')
306
+ 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>""")
307
+
308
+ with gr.Row():
309
+ with gr.Column():
310
+ gr.HTML('<h3 class="step-header">1. Enroll Subjects into DB</h3>')
311
+ search_enroll_img = create_image_selection_ui("Select Image to Enroll", EXAMPLE_IMAGES_ENROLL)
312
+ search_enroll_btn = gr.Button("βž• Encrypt & Add to Database", variant="secondary")
313
+ with gr.Group(visible=False) as enroll_features_group:
314
+ enroll_raw_features = gr.JSON(label="Raw Features (Plaintext)")
315
+ enroll_encrypted_features = gr.Textbox(label="Encrypted Features (Ciphertext)", interactive=False, lines=5)
316
+ search_enroll_status = gr.HTML()
317
+
318
+ with gr.Column():
319
+ gr.HTML('<h3 class="step-header">2. Search for a Subject</h3>')
320
+ search_probe_img = create_image_selection_ui("Select Image to Search", EXAMPLE_IMAGES_AUTH)
321
+ search_run_btn = gr.Button("πŸš€ Perform Secure 1:N Search", variant="primary", size="lg")
322
+ with gr.Group(visible=False) as search_features_group:
323
+ search_raw_features = gr.JSON(label="Raw Features (Plaintext)")
324
+ search_encrypted_features = gr.Textbox(label="Encrypted Features (Ciphertext)", interactive=False, lines=5)
325
+ search_status = gr.HTML(elem_classes="status-text")
326
+
327
+ search_result = gr.HTML()
328
+ search_result_image = gr.Image(label="Found Reference Image", interactive=False, visible=False)
329
+
330
+ with gr.Accordion("Advanced Settings", open=False):
331
+ search_threshold = gr.Slider(-512*5, 512*5, value=133, label="Match Strictness")
332
+ search_sec_level = gr.Dropdown(SECURITYLEVELS, value="128", label="Security Level")
333
+
334
+ def secure_enroll_flow(image, sec_level):
335
+ global DB_SUBJECT_COUNT, ENROLLED_SEARCH_IMAGES
336
+ if image is None: raise gr.Error("Please provide an image to enroll.")
337
+
338
+ current_id = DB_SUBJECT_COUNT
339
+
340
+ yield "Extracting features...", gr.update(visible=False), None, None
341
+ embedding, emb_path = extract_embedding(image, "search_enroll")
342
+
343
+ yield "Encrypting features...", gr.update(visible=True), {"embedding": embedding}, None
344
+ run_binary("./bin/search.bin", sec_level, "encRef", emb_path, current_id)
345
+ ciphertext, _ = run_binary("./bin/search.bin", sec_level, "printVectorCipher", "encRef", "print")
346
+
347
+ yield "Adding to secure database...", gr.update(visible=True), {"embedding": embedding}, ciphertext
348
+ run_binary("./bin/search.bin", sec_level, "addRef")
349
+
350
+ ENROLLED_SEARCH_IMAGES.append(image)
351
+ DB_SUBJECT_COUNT += 1
352
+ yield f"βœ… Subject with ID {current_id} added. Total subjects: {DB_SUBJECT_COUNT - 1}.", gr.update(visible=True), {"embedding": embedding}, ciphertext
353
+
354
+ def secure_search_flow(image, threshold, sec_level):
355
+ global ENROLLED_SEARCH_IMAGES
356
+ if image is None: raise gr.Error("Please provide an image to search.")
357
+
358
+ yield "Extracting probe features...", "", gr.update(visible=False), None, None, gr.update(visible=False)
359
+ embedding, emb_path = extract_embedding(image, "search_auth")
360
+
361
+ yield "Encrypting probe features...", "", gr.update(visible=True), {"embedding": embedding}, None, gr.update(visible=False)
362
+ run_binary("./bin/search.bin", sec_level, "encProbe", emb_path)
363
+ ciphertext, _ = run_binary("./bin/search.bin", sec_level, "printProbe", "print")
364
+
365
+ yield "βœ… Probe encrypted. Searching database...", "", gr.update(visible=True), {"embedding": embedding}, ciphertext, gr.update(visible=False)
366
+ run_binary("./bin/search.bin", sec_level, "search")
367
+
368
+ yield "βœ… Search complete. Decrypting results...", "", gr.update(visible=True), {"embedding": embedding}, ciphertext, gr.update(visible=False)
369
+ # output, _ = run_binary("./bin/search.bin", sec_level, "decDecisionClear", threshold)
370
+ output, _ = run_binary("./bin/search.bin", sec_level, "decScoreClear", threshold)
371
+ print(f"Search binary output: >>>{output}<<<")
372
+
373
+ lines = output.strip().split('\n')
374
+ decision = lines[0].lower()
375
+
376
+ if decision == "found":
377
+ try:
378
+ # Assuming output is "found\nID: <id>"
379
+ found_id_line = next(line for line in lines if "id:" in line.lower())
380
+ found_id = int(found_id_line.split(':')[1].strip())
381
+
382
+ # Binary ID is 1-based, list is 0-based
383
+ found_image_path = ENROLLED_SEARCH_IMAGES[found_id - 1]
384
+
385
+ 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>"""
386
+ result_image_update = gr.update(value=found_image_path, visible=True)
387
+
388
+ except (StopIteration, IndexError, ValueError):
389
+ 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>"""
390
+ result_image_update = gr.update(visible=False)
391
+ else:
392
+ result_html = """<div class="result-container no-match"><h2>❌ NOT FOUND</h2><p>The subject was not found in the database.</p></div>"""
393
+ result_image_update = gr.update(visible=False)
394
+
395
+ yield "Done!", result_html, gr.update(visible=True), {"embedding": embedding}, ciphertext, result_image_update
396
+
397
+ search_enroll_btn.click(
398
+ fn=secure_enroll_flow,
399
+ inputs=[search_enroll_img, search_sec_level],
400
+ outputs=[search_enroll_status, enroll_features_group, enroll_raw_features, enroll_encrypted_features]
401
+ )
402
+
403
+ search_run_btn.click(
404
+ fn=secure_search_flow,
405
+ inputs=[search_probe_img, search_threshold, search_sec_level],
406
+ outputs=[search_status, search_result, search_features_group, search_raw_features, search_encrypted_features, search_result_image]
407
+ )
408
+
409
+ # --- Launch the Application ---
410
+ if __name__ == "__main__":
411
+ demo.launch()
bin/decDecision.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f7675db4cd1c61743d73b78364ccefa313b408b09713e3b16e19f5a6eee087bb
3
+ size 8045152
bin/encProbe.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3959987099fb521134b6c46c367b2b6f6106bf9bb45eda98108e26710376f078
3
+ size 8030536
bin/encReference.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6705fbd4703ab2bc52d4af5719f5ece369de22d214dda966b124a85803c3a92d
3
+ size 8069184
bin/genKeys.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:30c5e81854404a40c06ac9b727a17907536b1490defb055831921022b25e6e7c
3
+ size 8079376
bin/recDecision.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:97d6a8be345463779f296ae73c70a082ea90e2c41add0f99f6875d75ff753c75
3
+ size 8057936
bin/search.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:179c1865afefee41d161bf77a4cd45d18becbd8d47d8c3bb16b6fd5d006c6093
3
+ size 8294280
lookupTables/Borders_nB_3_dimF_512.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ -0.050880,-0.029846,-0.014102,-0.000000,0.014102,0.029846,0.050880
lookupTables/MFIP_nB_3_dQ_0.001_dimF_512.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ 5,3,2,1,-1,-2,-3,-5
2
+ 3,2,1,0,0,-1,-2,-3
3
+ 2,1,0,0,0,0,-1,-2
4
+ 1,0,0,0,0,0,0,-1
5
+ -1,0,0,0,0,0,0,1
6
+ -2,-1,0,0,0,0,1,2
7
+ -3,-2,-1,0,0,1,2,3
8
+ -5,-3,-2,-1,1,2,3,5
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ gradio
2
+ opencv-python
3
+ pillow
4
+ insightface
5
+ onnxruntime
6
+ numpy
7
+ huggingface_hub
static/.DS_Store ADDED
Binary file (6.15 kB). View file
 
static/logo.png ADDED

Git LFS Details

  • SHA256: efd99a8849fa7f655c6bb3559d8d8866d1eb066e86587b39f9976cd99bbd6e28
  • Pointer size: 131 Bytes
  • Size of remote file: 821 kB
static/original.jpg ADDED

Git LFS Details

  • SHA256: 1cdb04864584242755b9bf7a9397e7b3a1587fd1349e853942cec4b3e661de3c
  • Pointer size: 129 Bytes
  • Size of remote file: 5.81 kB
static/reconstructed.png ADDED

Git LFS Details

  • SHA256: dee0cc9b2a445d67394bc5fe9a573d3825a6233bc24db346876cf22084e4be7e
  • Pointer size: 130 Bytes
  • Size of remote file: 21.4 kB