File size: 24,967 Bytes
9fd8a40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
"""
Face Anti-Spoofing Dataset Generator

This application generates synthetic spoof images for face anti-spoofing dataset creation.
Attack types align with iBeta Level 1 and Level 2 standards:

iBeta Level 1 (Basic Attacks):
- Print Attack: Printed photos of the target face
- Display Attack: Screen replay of face images/photos
- Cut Photo Attack: Partially occluded printed photos

iBeta Level 2 (Advanced Attacks):
- Mask Attack: Paper/plastic masks
- Warped Photo Attack: Deformed/repositioned photos
- Eye Frame Attack: Photos with eye cutouts
"""

import gradio as gr
import numpy as np
from PIL import Image, ImageDraw, ImageFilter, ImageEnhance
import io
import base64
import json
from dataclasses import dataclass
from typing import List, Tuple, Optional
import random

# Attack types aligned with iBeta standards
@dataclass
class AttackType:
    """Represents a spoof attack type"""
    name: str
    level: int  # 1 or 2 (iBeta level)
    description: str
    severity: str  # low, medium, high


# Define attack types
ATTACK_TYPES = [
    AttackType("Print Attack", 1, "Printed photograph of the face", "low"),
    AttackType("Display Attack", 1, "Face shown on screen/display", "low"),
    AttackType("Cut Photo Attack", 1, "Partially cut photograph with eye holes", "medium"),
    AttackType("Paper Mask Attack", 2, "Paper-based face mask", "medium"),
    AttackType("Warped Photo Attack", 2, "Warped/deformed photograph", "high"),
    AttackType("Eye Frame Attack", 2, "Photo with eye cutouts and frame", "high"),
]


def generate_print_attack(image: Image.Image, quality: str = "high") -> Image.Image:
    """
    Simulate a printed photograph attack.
    Adds print artifacts like grain, slight blur, color shift.
    """
    img = image.copy()
    
    if quality == "high":
        # Slight blur simulating high-quality print
        img = img.filter(ImageFilter.GaussianBlur(radius=0.5))
        # Add slight noise
        np_img = np.array(img)
        noise = np.random.normal(0, 3, np_img.shape).astype(np.int16)
        np_img = np.clip(np_img.astype(np.int16) + noise, 0, 255).astype(np.uint8)
        img = Image.fromarray(np_img)
    else:
        # Lower quality print
        img = img.filter(ImageFilter.GaussianBlur(radius=1.5))
        np_img = np.array(img)
        noise = np.random.normal(0, 10, np_img.shape).astype(np.int16)
        np_img = np.clip(np_img.astype(np.int16) + noise, 0, 255).astype(np.uint8)
        img = Image.fromarray(np_img)
    
    # Slight color shift (printing ink effect)
    enhancer = ImageEnhance.Color(img)
    img = enhancer.enhance(0.9)
    
    return img


def generate_display_attack(image: Image.Image, screen_type: str = "phone") -> Image.Image:
    """
    Simulate a display replay attack.
    Adds screen artifacts like moiré patterns, reflections.
    """
    img = image.copy()
    
    # Resize to simulate different screen sizes
    if screen_type == "phone":
        img = img.resize((224, 224), Image.LANCZOS)
        img = img.resize((300, 300), Image.LANCZOS)
    else:
        img = img.resize((256, 256), Image.LANCZOS)
        img = img.resize((400, 300), Image.LANCZOS)
    
    # Add screen moiré effect
    np_img = np.array(img)
    moiré = np.zeros_like(np_img)
    for i in range(moiré.shape[0]):
        for j in range(moiré.shape[1]):
            moiré[i, j] = int(15 * np.sin(i * 0.1) * np.sin(j * 0.1))
    
    np_img = np.clip(np_img.astype(np.int16) + moiré, 0, 255).astype(np.uint8)
    img = Image.fromarray(np_img)
    
    # Add slight glow effect
    img = img.filter(ImageFilter.GaussianBlur(radius=0.5))
    
    return img


def generate_cut_photo_attack(image: Image.Image, cut_type: str = "eyes") -> Image.Image:
    """
    Simulate a cut photo attack with eye holes cut out.
    Used to simulate attempts to bypass eye-based liveness detection.
    """
    img = image.copy()
    width, height = img.size
    
    # Create a black background
    background = Image.new('RGB', (width, height), (0, 0, 0))
    
    # Calculate eye positions (approximate)
    left_eye_x = int(width * 0.35)
    left_eye_y = int(height * 0.35)
    right_eye_x = int(width * 0.65)
    right_eye_y = int(height * 0.35)
    eye_radius = int(min(width, height) * 0.08)
    
    draw = ImageDraw.Draw(background)
    
    if cut_type == "eyes":
        # Cut out eye regions
        mask = Image.new('L', (width, height), 0)
        mask_draw = ImageDraw.Draw(mask)
        mask_draw.ellipse(
            (left_eye_x - eye_radius, left_eye_y - eye_radius,
             left_eye_x + eye_radius, left_eye_y + eye_radius),
            fill=255
        )
        mask_draw.ellipse(
            (right_eye_x - eye_radius, right_eye_y - eye_radius,
             right_eye_x + eye_radius, right_eye_y + eye_radius),
            fill=255
        )
    else:
        # Cut out larger region around eyes
        mask = Image.new('L', (width, height), 0)
        mask_draw = ImageDraw.Draw(mask)
        mask_draw.ellipse(
            (left_eye_x - eye_radius*2, left_eye_y - eye_radius*1.5,
             left_eye_x + eye_radius*2, left_eye_y + eye_radius*1.5),
            fill=255
        )
        mask_draw.ellся:
        mask_draw.ellipse(
            (right_eye_x - eye_radius*2, right_eye_y - eye_radius*1.5,
             right_eye_x + eye_radius*2, right_eye_y + eye_radius*1.5),
            fill=255
        )
    
    # Apply the cut
    img.paste(background, mask=mask)
    
    # Add slight paper texture
    np_img = np.array(img)
    noise = np.random.normal(0, 5, np_img.shape).astype(np.int16)
    np_img = np.clip(np_img.astype(np.int16) + noise, 0, 255).astype(np.uint8)
    img = Image.fromarray(np_img)
    
    return img


def generate_paper_mask_attack(image: Image.Image, mask_style: str = "flat") -> Image.Image:
    """
    Simulate a paper-based mask attack.
    Creates a simplified face shape on paper.
    """
    img = image.copy()
    width, height = img.size
    
    # Create a face-shaped mask
    mask = Image.new('L', (width, height), 0)
    draw = ImageDraw.Draw(mask)
    
    # Draw oval face shape
    face_center_x = width // 2
    face_center_y = height // 2
    face_width = int(width * 0.7)
    face_height = int(height * 0.8)
    
    draw.ellipse(
        (face_center_x - face_width//2, face_center_y - face_height//2,
         face_center_x + face_width//2, face_center_y + face_height//2),
        fill=255
    )
    
    # Create RGB mask for pasting
    mask_rgb = Image.merge('RGB', [mask, mask, mask])
    
    # Apply the mask
    img = Image.composite(img, Image.new('RGB', img.size, (128, 128, 128)), mask)
    
    if mask_style == "curled":
        # Add curling effect at edges
        img = img.filter(ImageFilter.GaussianBlur(radius=2))
    else:
        # Flat paper effect
        img = img.filter(ImageFilter.GaussianBlur(radius=0.5))
    
    # Add paper texture
    np_img = np.array(img)
    paper_noise = np.random.normal(0, 8, np_img.shape).astype(np.int16)
    np_img = np.clip(np_img.astype(np.int16) + paper_noise, 0, 255).astype(np.uint8)
    img = Image.fromarray(np_img)
    
    return img


def generate_warped_photo_attack(image: Image.Image, warp_type: str = "moderate") -> Image.Image:
    """
    Simulate a warped/deformed photo attack.
    Creates non-rigid deformations in the face image.
    """
    img = image.copy()
    width, height = img.size
    
    # Apply different warping based on type
    if warp_type == "slight":
        # Very subtle warping
        coeffs = [(1.02, 0.01, -0.01), (0.01, 1.01, -0.02), (0, 0, 1)]
    elif warp_type == "moderate":
        # Moderate warping
        coeffs = [(1.05, 0.02, -0.03), (0.02, 1.03, -0.02), (0, 0, 1)]
    else:  # severe
        # More pronounced warping
        coeffs = [(1.08, 0.03, -0.05), (0.03, 1.06, -0.03), (0, 0, 1)]
    
    # Apply affine transformation
    img = img.transform(
        (width, height),
        Image.AFFINE,
        coeffs[:2],
        Image.BICUBIC
    )
    
    # Add slight blur
    img = img.filter(ImageFilter.GaussianBlur(radius=0.5))
    
    return img


def generate_eye_frame_attack(image: Image.Image, frame_type: str = "plastic") -> Image.Image:
    """
    Simulate an eye frame attack with cutouts for eyes.
    Includes a physical frame around the photo.
    """
    img = image.copy()
    width, height = img.size
    
    # Create image with frame border
    border_size = int(min(width, height) * 0.1)
    new_width = width + 2 * border_size
    new_height = height + 2 * border_size
    
    # Create new canvas with frame
    if frame_type == "plastic":
        frame_color = (30, 30, 30)  # Dark plastic frame
    else:
        frame_color = (200, 180, 140)  # Wood frame
    
    canvas = Image.new('RGB', (new_width, new_height), frame_color)
    
    # Paste original image in center
    canvas.paste(img, (border_size, border_size))
    
    # Add frame border details
    draw = ImageDraw.Draw(canvas)
    
    # Draw inner border
    inner_border = int(border_size * 0.2)
    draw.rectangle(
        [border_size - inner_border, border_size - inner_border,
         new_width - border_size + inner_border, new_height - border_size + inner_border],
        outline=(200, 200, 200) if frame_type == "plastic" else (150, 130, 90),
        width=3
    )
    
    # Calculate eye positions in the pasted image
    left_eye_x = border_size + int(width * 0.35)
    left_eye_y = border_size + int(height * 0.35)
    right_eye_x = border_size + int(width * 0.65)
    right_eye_y = border_size + int(height * 0.35)
    eye_radius = int(min(width, height) * 0.06)
    
    # Cut out eye holes
    mask = Image.new('L', canvas.size, 0)
    mask_draw = ImageDraw.Draw(mask)
    mask_draw.ellipse(
        (left_eye_x - eye_radius, left_eye_y - eye_radius,
         left_eye_x + eye_radius, left_eye_y + eye_radius),
        fill=255
    )
    mask_draw.ellipse(
        (right_eye_x - eye_radius, right_eye_y - eye_radius,
         right_eye_x + eye_radius, right_eye_y + eye_radius),
        fill=255
    )
    
    # Apply the cutouts
    np_canvas = np.array(canvas)
    np_mask = np.array(mask)
    
    # Darken the cutout regions (simulating background behind frame)
    np_canvas = np.where(np_mask[:, :, np.newaxis] == 255, np_canvas * 0.3, np_canvas)
    
    result = Image.fromarray(np_canvas.astype(np.uint8))
    
    # Add frame texture
    np_result = np.array(result)
    frame_texture = np.random.normal(0, 3, np_result.shape).astype(np.int16)
    np_result = np.clip(np_result.astype(np.int16) + frame_texture, 0, 255).astype(np.uint8)
    result = Image.fromarray(np_result)
    
    return result


def generate_spoof_image(
    reference_image: Image.Image,
    attack_type: str,
    quality_variant: str = "standard"
) -> Tuple[Image.Image, dict]:
    """
    Generate a spoof image based on the selected attack type.
    
    Args:
        reference_image: The input face image to generate spoof from
        attack_type: Type of spoof attack
        quality_variant: Quality variation of the attack
    
    Returns:
        Tuple of (spoof_image, metadata_dict)
    """
    img = reference_image.copy()
    
    # Convert to RGB if needed
    if img.mode != 'RGB':
        img = img.convert('RGB')
    
    metadata = {
        "attack_type": attack_type,
        "quality_variant": quality_variant,
        "ibeta_level": None,
        "spoof_indicators": []
    }
    
    if attack_type == "Print Attack":
        quality = "high" if quality_variant == "high_quality" else "low"
        result = generate_print_attack(img, quality)
        metadata["ibeta_level"] = 1
        metadata["spoof_indicators"] = [
            "print_texture_artifact",
            "moiré_pattern_possible",
            "flat_surface_indicator"
        ]
    
    elif attack_type == "Display Attack":
        screen = "phone" if quality_variant == "mobile" else "monitor"
        result = generate_display_attack(img, screen)
        metadata["ibeta_level"] = 1
        metadata["spoof_indicators"] = [
            "screen_reflection",
            "moiré_pattern",
            "backlight_artifact"
        ]
    
    elif attack_type == "Cut Photo Attack":
        cut_type = "eyes" if quality_variant == "standard" else "large"
        result = generate_cut_photo_attack(img, cut_type)
        metadata["ibeta_level"] = 1
        metadata["spoof_indicators"] = [
            "photo_cut_marks",
            "inconsistent_occlusion",
            "background_discontinuity"
        ]
    
    elif attack_type == "Paper Mask Attack":
        mask_style = "curled" if quality_variant == "worn" else "flat"
        result = generate_paper_mask_attack(img, mask_style)
        metadata["ibeta_level"] = 2
        metadata["spoof_indicators"] = [
            "mask_edge_artifact",
            "flat_surface_texture",
            "inconsistent_skin_texture"
        ]
    
    elif attack_type == "Warped Photo Attack":
        warp_type = "slight" if quality_variant == "minimal" else "moderate"
        result = generate_warped_photo_attack(img, warp_type)
        metadata["ibeta_level"] = 2
        metadata["spoof_indicators"] = [
            "geometric_distortion",
            "inconsistent_perspective",
            "non_rigid_deformation"
        ]
    
    elif attack_type == "Eye Frame Attack":
        frame_type = "plastic" if quality_variant == "standard" else "wooden"
        result = generate_eye_frame_attack(img, frame_type)
        metadata["ibeta_level"] = 2
        metadata["spoof_indicators"] = [
            "frame_artifact",
            "eye_cutout_marks",
            "inconsistent_depth"
        ]
    
    else:
        result = img.copy()
        metadata["error"] = "Unknown attack type"
    
    return result, metadata


def create_dataset_preview(
    reference_image: Image.Image,
    selected_attacks: List[str],
    generate_all: bool = False
) -> Tuple[Image.Image, str, dict]:
    """
    Create a preview of generated spoof images.
    
    Returns:
        Preview image, attack info summary, and dataset metadata
    """
    if reference_image is None:
        return None, "Please upload a reference image first.", {}
    
    if not generate_all and not selected_attacks:
        return None, "Please select at least one attack type.", {}
    
    attacks_to_generate = ATTACK_TYPES if generate_all else [
        at for at in ATTACK_TYPES if at.name in selected_attacks
    ]
    
    # Create a grid preview
    cols = min(len(attacks_to_generate), 3)
    rows = (len(attacks_to_generate) + cols - 1) // cols
    
    img = reference_image.copy()
    if img.mode != 'RGB':
        img = img.convert('RGB')
    
    # Resize for consistent preview
    preview_size = (200, 200)
    img = img.resize(preview_size, Image.LANCZOS)
    
    # Calculate grid dimensions
    cell_width = preview_size[0] + 20
    cell_height = preview_size[1] + 40
    
    grid_width = cols * cell_width
    grid_height = rows * cell_height + 60
    
    # Create preview canvas
    preview = Image.new('RGB', (grid_width, grid_height), (245, 245, 245))
    draw = ImageDraw.Draw(preview)
    
    # Title
    from PIL import ImageFont
    try:
        font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 14)
    except:
        font = ImageFont.load_default()
    
    draw.text((10, 10), "Generated Spoof Samples Preview", fill=(50, 50, 50), font=font)
    
    # Generate and place each attack
    dataset_metadata = {
        "total_samples": len(attacks_to_generate),
        "ibeta_level_1_count": sum(1 for a in attacks_to_generate if a.level == 1),
        "ibeta_level_2_count": sum(1 for a in attacks_to_generate if a.level == 2),
        "attacks": []
    }
    
    for idx, attack in enumerate(attacks_to_generate):
        col = idx % cols
        row = idx // cols
        
        x = col * cell_width + 10
        y = row * cell_height + 40
        
        # Generate spoof image
        spoof_img, metadata = generate_spoof_image(
            reference_image,
            attack.name,
            "standard"
        )
        
        spoof_img = spoof_img.resize(preview_size, Image.LANCZOS)
        
        # Paste into preview
        preview.paste(spoof_img, (x, y))
        
        # Draw attack name
        name_y = y + preview_size[1] + 5
        level_text = f"[L{attack.level}]"
        draw.text((x, name_y), level_text, fill=(100, 100, 100), font=font)
        draw.text((x + 30, name_y), attack.name[:15], fill=(50, 50, 50), font=font)
        
        dataset_metadata["attacks"].append({
            "attack_type": attack.name,
            "ibeta_level": attack.level,
            "severity": attack.severity,
            "description": attack.description,
            "metadata": metadata
        })
    
    # Create summary
    summary = (
        f"Dataset Preview Generated:\n"
        f"• Total Samples: {dataset_metadata['total_samples']}\n"
        f"• iBeta Level 1: {dataset_metadata['ibeta_level_1_count']} attacks\n"
        f"• iBeta Level 2: {dataset_metadata['ibeta_level_2_count']} attacks\n"
        f"• Attacks: {', '.join(a.name for a in attacks_to_generate)}"
    )
    
    return preview, summary, dataset_metadata


def export_dataset_metadata(metadata: dict) -> str:
    """Export dataset metadata as JSON string."""
    return json.dumps(metadata, indent=2)


# Custom theme for the app
def create_custom_theme():
    """Create a custom theme for the anti-spoofing dataset generator."""
    return gr.themes.Soft(
        primary_hue="red",
        secondary_hue="orange",
        neutral_hue="slate",
        font=gr.themes.GoogleFont("Inter"),
        text_size="lg",
        spacing_size="lg",
        radius_size="md"
    ).set(
        button_primary_background_fill="*primary_600",
        button_primary_background_fill_hover="*primary_700",
        block_title_text_weight="600",
        body_text_weight="500",
    )


# Gradio 6 App
with gr.Blocks() as demo:
    # Custom CSS for styling
    custom_css = """
    .spoof-header {
        background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
        padding: 20px;
        border-radius: 12px;
        margin-bottom: 20px;
    }
    .attack-card {
        background: white;
        border-radius: 10px;
        padding: 15px;
        box-shadow: 0 2px 8px rgba(0,0,0,0.1);
        margin: 10px 0;
    }
    .level-badge {
        background: linear-gradient(135deg, #f97316 0%, #ef4444 100%);
        color: white;
        padding: 4px 12px;
        border-radius: 20px;
        font-size: 12px;
        font-weight: bold;
    }
    .level-badge-1 {
        background: linear-gradient(135deg, #22c55e 0%, #16a34a 100%);
    }
    .level-badge-2 {
        background: linear-gradient(135deg, #f97316 0%, #ef4444 100%);
    }
    .info-box {
        background: #fef3c7;
        border-left: 4px solid #f59e0b;
        padding: 12px;
        border-radius: 4px;
        margin: 10px 0;
    }
    .metadata-box {
        background: #f1f5f9;
        border: 1px solid #e2e8f0;
        padding: 15px;
        border-radius: 8px;
        font-family: monospace;
        font-size: 12px;
        max-height: 300px;
        overflow-y: auto;
    }
    """
    
    # Header with branding
    with gr.Group(elem_classes=["spoof-header"]):
        gr.Markdown(
            """
            # 🔒 Face Anti-Spoofing Dataset Generator
            
            Generate synthetic spoof images for face anti-spoofing dataset creation.
            Aligned with **iBeta Level 1 & 2** standards.
            
            *Built with [anycoder](https://huggingface.co/spaces/akhaliq/anycoder)*
            """
        )
    
    # Info boxes about iBeta levels
    with gr.Row():
        with gr.Column(scale=1):
            gr.Markdown(
                """
                ### 📋 iBeta Level 1 (Basic)
                - **Print Attack**: Printed photos
                - **Display Attack**: Screen replays
                - **Cut Photo Attack**: Partially occluded
                """,
                elem_classes=["attack-card"]
            )
        with gr.Column(scale=1):
            gr.Markdown(
                """
                ### ⚠️ iBeta Level 2 (Advanced)
                - **Paper Mask Attack**: Paper masks
                - **Warped Photo Attack**: Deformed photos
                - **Eye Frame Attack**: Eye cutout frames
                """,
                elem_classes=["attack-card"]
            )
    
    # Main input section
    with gr.Row():
        with gr.Column(scale=1):
            gr.Markdown("### 📤 Reference Image")
            reference_image = gr.Image(
                label="Upload Live Face Image",
                type="pil",
                sources=["upload"],
                height=300
            )
            
            gr.Markdown("### 🎯 Attack Selection")
            attack_checkbox = gr.CheckboxGroup(
                choices=[at.name for at in ATTACK_TYPES],
                value=[ATTACK_TYPES[0].name],  # Default to first attack
                label="Select Attack Types",
                info="Choose which spoof attacks to generate"
            )
            
            generate_all_checkbox = gr.Checkbox(
                value=False,
                label="Generate All Attack Types",
                info="Generate samples for all available attacks"
            )
            
            generate_btn = gr.Button(
                "Generate Spoof Samples",
                variant="primary",
                size="lg"
            )
        
        with gr.Column(scale=1):
            gr.Markdown("### 📊 Preview & Output")
            preview_output = gr.Image(
                label="Generated Spoof Samples",
                type="pil",
                height=400
            )
            
            summary_output = gr.Textbox(
                label="Generation Summary",
                lines=6,
                interactive=False
            )
    
    # Metadata section
    with gr.Accordion("📄 Dataset Metadata (JSON)", open=True):
        metadata_output = gr.Code(
            label="Metadata",
            language="json",
            elem_classes=["metadata-box"]
        )
    
    # Attack details section
    with gr.Accordion("ℹ️ Attack Type Details", open=False):
        gr.Markdown(
            """
            | Attack Type | iBeta Level | Severity | Description |
            |-------------|-------------|----------|-------------|
            | Print Attack | 1 | Low | Printed photograph with typical print artifacts |
            | Display Attack | 1 | Low | Face displayed on screen with moiré patterns |
            | Cut Photo Attack | 1 | Medium | Printed photo with eye cutouts |
            | Paper Mask Attack | 2 | Medium | Flat paper-based face mask |
            | Warped Photo Attack | 2 | High | Deformed photograph with geometric distortion |
            | Eye Frame Attack | 2 | High | Photo with eye cutouts and physical frame |
            
            ### Spoof Indicators
            Each generated sample includes metadata with expected spoof indicators for training:
            - Texture artifacts (print, paper)
            - Moiré patterns (display)
            - Geometric distortions (warped)
            - Occlusion patterns (cut, frame)
            - Surface inconsistencies (mask)
            """
        )
    
    # Event handlers
    def handle_generate(ref_img, attacks, generate_all):
        preview, summary, metadata = create_dataset_preview(
            ref_img,
            attacks,
            generate_all
        )
        metadata_json = export_dataset_metadata(metadata)
        return preview, summary, metadata_json
    
    generate_btn.click(
        fn=handle_generate,
        inputs=[reference_image, attack_checkbox, generate_all_checkbox],
        outputs=[preview_output, summary_output, metadata_output]
    )
    
    # Update when "Generate All" changes
    generate_all_checkbox.change(
        fn=lambda x: gr.CheckboxGroup(interactive=not x),
        inputs=generate_all_checkbox,
        outputs=attack_checkbox
    )
    
    # Live preview on image change
    reference_image.change(
        fn=handle_generate,
        inputs=[reference_image, attack_checkbox, generate_all_checkbox],
        outputs=[preview_output, summary_output, metadata_output]
    )

# Launch with Gradio 6 theme and configuration
demo.launch(
    theme=create_custom_theme(),
    css=custom_css if 'custom_css' in dir() else None,
    footer_links=[
        {"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"},
        {"label": "Documentation", "url": "https://gradio.app"},
    ],
    title="Face Anti-Spoofing Dataset Generator",
    description="Generate synthetic spoof images aligned with iBeta Level 1 & 2 standards",
)