File size: 26,987 Bytes
3ec6126
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
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

# --- Configuration ---
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']

# --- Global Variables ---
face_app = None
DB_SUBJECT_COUNT = 1
ENROLLED_SEARCH_IMAGES = []

# --- Helper Functions ---
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:
        # For 1:1 recognition, save to the respective binary folder
        if mode in ["enroll", "auth"]:
            emb_path = f'./{mode}-emb.txt'
        # For 1:N search, create a subject-specific path in the search folder
        else: # search_enroll, search_auth
            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()

# --- UI Components ---
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")
    
    # Event handlers that directly update both the hidden state and the visible display
    def on_select(evt: gr.SelectData):
        selected_image = gallery_images[evt.index]  # Get the actual image path from the gallery list
        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

# --- UI Styling and Theming ---
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; }
"""

# --- Gradio UI Definition ---
with gr.Blocks(css=css) as demo:
    # --- Header ---
    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>
    """)

    # --- Key Generation on Load ---
    # Generate keys once for each demo when the app starts up
    demo.load(lambda: run_binary("./bin/genKeys.bin", "128", "genkeys"), None, None)
    demo.load(lambda: run_binary("./bin/search.bin", "128", "genkeys"), None, None)


    # --- Main Tabs for Demo Mode ---
    with gr.Tabs() as mode_tabs:
        # --- 1:1 Recognition Demo ---
        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():
                # --- Vulnerable System Tab ---
                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]
                        )

                # --- Secure System Tab ---
                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):
                            # Reset UI
                            yield "Initializing...", "", gr.update(visible=False), None, None, gr.update(visible=False), None, None

                            # Process Reference Image
                            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")


                            # Process Probe Image
                            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")

                            # Perform Match
                            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]
                        )

        # --- 1:N Search Demo ---
        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():
                # --- Secure System Tab ---
                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, "decDecisionClear", threshold)
                            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:
                                    # Assuming output is "found\nID: <id>"
                                    found_id_line = next(line for line in lines if "id:" in line.lower())
                                    found_id = int(found_id_line.split(':')[1].strip())
                                    
                                    # Binary ID is 1-based, list is 0-based
                                    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]
                        )

# --- Launch the Application ---
if __name__ == "__main__":
    demo.launch()