File size: 28,259 Bytes
32d67cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
CycleGAN Image-to-Image Translation
Beautiful Gradio UI for HuggingFace Spaces
Sketch ↔ Photo Translation with Loss Visualizations
"""

import os
import json
import torch
import numpy as np
import gradio as gr
from pathlib import Path
from PIL import Image
import matplotlib.pyplot as plt
import matplotlib
import io

matplotlib.use('Agg')

# ==================== CONFIGURATION ====================
DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
IMG_SIZE = 256
NGF = NDF = 64
N_RES = 9


# ==================== MODEL ARCHITECTURES ====================
import torch.nn as nn


class ResBlock(nn.Module):
    def __init__(self, dim):
        super().__init__()
        self.block = nn.Sequential(
            nn.ReflectionPad2d(1), nn.Conv2d(dim, dim, 3),
            nn.InstanceNorm2d(dim), nn.ReLU(True),
            nn.ReflectionPad2d(1), nn.Conv2d(dim, dim, 3),
            nn.InstanceNorm2d(dim))
    
    def forward(self, x):
        return x + self.block(x)


class Generator(nn.Module):
    def __init__(self, in_ch=3, out_ch=3, ngf=64, n_res=9):
        super().__init__()
        m = [nn.ReflectionPad2d(3), nn.Conv2d(in_ch, ngf, 7),
             nn.InstanceNorm2d(ngf), nn.ReLU(True)]
        for i in range(2):
            f = 2**i
            m += [nn.Conv2d(ngf*f, ngf*f*2, 3, 2, 1), 
                  nn.InstanceNorm2d(ngf*f*2), nn.ReLU(True)]
        for _ in range(n_res):
            m.append(ResBlock(ngf*4))
        for i in range(2, 0, -1):
            f = 2**i
            m += [nn.ConvTranspose2d(ngf*f, ngf*f//2, 3, 2, 1, 1),
                  nn.InstanceNorm2d(ngf*f//2), nn.ReLU(True)]
        m += [nn.ReflectionPad2d(3), nn.Conv2d(ngf, out_ch, 7), nn.Tanh()]
        self.model = nn.Sequential(*m)
    
    def forward(self, x):
        return self.model(x)


class PatchDisc(nn.Module):
    def __init__(self, in_ch=3, ndf=64):
        super().__init__()
        def blk(i, o, norm=True, s=2):
            layers = [nn.Conv2d(i, o, 4, s, 1)]
            if norm:
                layers.append(nn.InstanceNorm2d(o))
            return layers + [nn.LeakyReLU(0.2, True)]
        
        self.model = nn.Sequential(
            *blk(in_ch, ndf, norm=False),
            *blk(ndf, ndf*2),
            *blk(ndf*2, ndf*4),
            *blk(ndf*4, ndf*8, s=1),
            nn.Conv2d(ndf*8, 1, 4, 1, 1))
    
    def forward(self, x):
        return self.model(x)


# ==================== MODEL INITIALIZATION ====================
def init_w(m):
    if isinstance(m, (nn.Conv2d, nn.ConvTranspose2d)):
        nn.init.normal_(m.weight, 0.0, 0.02)
        if m.bias is not None:
            nn.init.zeros_(m.bias)
    elif isinstance(m, nn.InstanceNorm2d) and m.weight is not None:
        nn.init.ones_(m.weight)
        nn.init.zeros_(m.bias)


def load_models():
    """Load pre-trained models from HuggingFace Hub or local checkpoints"""
    G_AB = Generator(3, 3, NGF, N_RES).to(DEVICE)
    G_BA = Generator(3, 3, NGF, N_RES).to(DEVICE)
    D_A = PatchDisc(3, NDF).to(DEVICE)
    D_B = PatchDisc(3, NDF).to(DEVICE)
    
    G_AB.apply(init_w)
    G_BA.apply(init_w)
    D_A.apply(init_w)
    D_B.apply(init_w)
    
    # Try to load from HuggingFace Hub
    try:
        from huggingface_hub import hf_hub_download
        # Download models from your HuggingFace repo
        # This is a placeholder - replace with your actual repo
        model_path = hf_hub_download(
            repo_id="hamzaAvvan/cyclegan-sketch-photo",
            filename="cyclegan_best.pth",
            repo_type="model"
        )
        checkpoint = torch.load(model_path, map_location=DEVICE)
        if 'G_AB' in checkpoint:
            G_AB.load_state_dict(checkpoint['G_AB'])
            G_BA.load_state_dict(checkpoint['G_BA'])
    except:
        print("Models not found on HuggingFace Hub. Using initialized models.")
    
    return G_AB, G_BA, D_A, D_B


def load_training_history():
    """Load training history from JSON if available"""
    try:
        from huggingface_hub import hf_hub_download
        history_path = hf_hub_download(
            repo_id="hamzaAvvan/cyclegan-sketch-photo",
            filename="training_history.json",
            repo_type="model"
        )
        with open(history_path, 'r') as f:
            return json.load(f)
    except:
        # Return dummy data for demonstration
        return {
            "num_epochs_completed": 5,
            "total_epochs": 5,
            "best_cycle_loss": 0.0523,
            "training_losses": {
                "generator": [0.8234, 0.7123, 0.6234, 0.5891, 0.5234],
                "discriminator_a": [0.6234, 0.5891, 0.5123, 0.4891, 0.4523],
                "discriminator_b": [0.6891, 0.6123, 0.5345, 0.5123, 0.4678],
                "cycle_loss": [1.2345, 1.0234, 0.8923, 0.7456, 0.6234],
                "identity_loss": [0.5234, 0.4891, 0.4123, 0.3891, 0.3456],
            }
        }


# ==================== IMAGE PROCESSING ====================
def tensor_to_image(tensor):
    """Convert tensor to PIL Image"""
    with torch.no_grad():
        img_np = ((tensor.squeeze().cpu() + 1) / 2).clamp(0, 1).permute(1, 2, 0).numpy()
    return Image.fromarray((img_np * 255).astype(np.uint8))


def image_to_tensor(pil_image):
    """Convert PIL Image to normalized tensor"""
    img_resized = pil_image.resize((IMG_SIZE, IMG_SIZE), Image.LANCZOS)
    img_array = np.array(img_resized) / 255.0
    if len(img_array.shape) == 2:  # Grayscale
        img_array = np.stack([img_array] * 3, axis=-1)
    img_tensor = torch.from_numpy(img_array).float().permute(2, 0, 1)
    img_tensor = (img_tensor * 2) - 1  # Normalize to [-1, 1]
    return img_tensor.unsqueeze(0).to(DEVICE)


# ==================== LOSS FUNCTION EXPLANATIONS ====================
LOSS_EXPLANATIONS = {
    "Adversarial Loss (LSGAN)": {
        "formula": "L_GAN = E[(D(x) - 1)Β²] + E[(D(G(z)))Β²]",
        "description": """
        <b>Purpose:</b> Encourages the generator to produce realistic images that fool the discriminator.
        
        <b>How it works:</b>
        β€’ Generator tries to minimize: E[(D(G(x)) - 1)Β²] (fool discriminator)
        β€’ Discriminator tries to minimize: E[(D(x) - 1)Β²] + E[(D(G(x)))Β²] (correct classification)
        
        <b>Why LSGAN:</b> Provides stable training compared to standard GAN loss. Uses MSE instead of cross-entropy.
        """,
        "weight": "1.0 (baseline)"
    },
    
    "Cycle Consistency Loss": {
        "formula": "L_cyc = E[||G_BA(G_AB(x)) - x||₁] + E[||G_AB(G_BA(y)) - y||₁]",
        "description": """
        <b>Purpose:</b> Ensures unpaired image-to-image translation maintains content.
        
        <b>How it works:</b>
        β€’ Translation Forward: Sketch β†’ Photo (G_AB)
        β€’ Translation Backward: Photo β†’ Sketch (G_BA)
        β€’ Cycle: Sketch β†’ Photo β†’ Sketch should reconstruct original
        β€’ This prevents mode collapse and maintains structural information
        
        <b>Why crucial:</b> Enables training WITHOUT paired data. Critical for unpaired translation.
        
        <b>Weight:</b> Ξ»_cyc = 10.0 (heavily weighted to preserve structure)
        """,
        "weight": "10.0 (most important)"
    },
    
    "Identity Loss": {
        "formula": "L_idt = E[||G_AB(y) - y||₁] + E[||G_BA(x) - x||₁]",
        "description": """
        <b>Purpose:</b> Encourages generators to preserve image characteristics when translating similar domains.
        
        <b>How it works:</b>
        β€’ If photo is translated through photo-generator, it should remain unchanged
        β€’ If sketch is translated through sketch-generator, it should remain unchanged
        β€’ Prevents unnecessary transformations when input is already in target domain
        
        <b>Benefit:</b> Improves image quality and visual stability. Prevents artifacts.
        
        <b>Weight:</b> Ξ»_idt = 5.0 (secondary importance)
        """,
        "weight": "5.0 (secondary)"
    }
}


def create_loss_explanation_tab():
    """Create detailed loss function explanation with formulas"""
    html_content = """
    <div style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); 
                padding: 30px; border-radius: 15px; color: white; margin-bottom: 20px;">
        <h1 style="margin: 0; font-size: 2.5em;">🎨 CycleGAN Loss Functions</h1>
        <p style="margin: 10px 0 0 0; font-size: 1.1em; opacity: 0.95;">
            Understanding the training objectives for unpaired image translation
        </p>
    </div>
    """
    
    for loss_name, loss_info in LOSS_EXPLANATIONS.items():
        html_content += f"""
        <div style="background: #f8f9fa; padding: 20px; border-radius: 10px; margin: 20px 0; 
                    border-left: 5px solid #667eea;">
            <h2 style="color: #667eea; margin-top: 0;">{loss_name}</h2>
            
            <div style="background: #e8eaf6; padding: 15px; border-radius: 8px; 
                        font-family: 'Courier New', monospace; font-size: 1.05em; 
                        margin: 15px 0; color: #333;">
                <strong>Formula:</strong> {loss_info['formula']}
            </div>
            
            <div style="color: #333; line-height: 1.8;">
                {loss_info['description']}
            </div>
            
            <div style="background: #fff3e0; padding: 10px 15px; border-radius: 8px; 
                        margin-top: 15px; color: #e65100;">
                <strong>βš–οΈ Weight:</strong> {loss_info['weight']}
            </div>
        </div>
        """
    
    html_content += """
    <div style="background: #e3f2fd; padding: 20px; border-radius: 10px; margin: 20px 0;">
        <h3 style="color: #1976d2; margin-top: 0;">πŸ”¬ Training Dynamics</h3>
        <p style="color: #333; line-height: 1.8;">
        <strong>Total Loss = L_GAN + Ξ»_cyc Γ— L_cyc + Ξ»_idt Γ— L_idt</strong><br><br>
        The generator learns to balance three objectives:
        <ul style="color: #333;">
            <li><strong>Realism</strong>: Fool the discriminator (L_GAN)</li>
            <li><strong>Content Preservation</strong>: Maintain structure through cycle (L_cyc) ⭐</li>
            <li><strong>Domain Consistency</strong>: Preserve domain characteristics (L_idt)</li>
        </ul>
        The cycle consistency loss dominates, ensuring quality unpaired translation.
        </p>
    </div>
    """
    
    return html_content


# ==================== VISUALIZATION FUNCTIONS ====================
def plot_training_losses(history):
    """Create matplotlib figure with training loss curves"""
    if not history or 'training_losses' not in history:
        return None
    
    losses = history['training_losses']
    epochs = range(1, len(losses['generator']) + 1)
    
    fig, axes = plt.subplots(2, 2, figsize=(14, 10))
    fig.patch.set_facecolor('white')
    
    # Generator Loss
    axes[0, 0].plot(epochs, losses['generator'], 'o-', linewidth=2.5, 
                    markersize=6, color='#667eea', label='Generator')
    axes[0, 0].set_title('Generator Loss', fontsize=12, fontweight='bold')
    axes[0, 0].set_xlabel('Epoch')
    axes[0, 0].set_ylabel('Loss')
    axes[0, 0].grid(True, alpha=0.3)
    axes[0, 0].legend()
    
    # Discriminator Losses
    axes[0, 1].plot(epochs, losses['discriminator_a'], 'o-', linewidth=2.5, 
                    markersize=6, color='#f57c00', label='Discriminator A (Sketch)')
    axes[0, 1].plot(epochs, losses['discriminator_b'], 's-', linewidth=2.5, 
                    markersize=6, color='#c62828', label='Discriminator B (Photo)')
    axes[0, 1].set_title('Discriminator Losses', fontsize=12, fontweight='bold')
    axes[0, 1].set_xlabel('Epoch')
    axes[0, 1].set_ylabel('Loss')
    axes[0, 1].grid(True, alpha=0.3)
    axes[0, 1].legend()
    
    # Cycle & Identity Loss
    axes[1, 0].plot(epochs, losses['cycle_loss'], 'o-', linewidth=2.5, 
                    markersize=6, color='#2e7d32', label='Cycle Loss')
    axes[1, 0].plot(epochs, losses['identity_loss'], 's-', linewidth=2.5, 
                    markersize=6, color='#7b1fa2', label='Identity Loss')
    axes[1, 0].set_title('Cycle & Identity Losses', fontsize=12, fontweight='bold')
    axes[1, 0].set_xlabel('Epoch')
    axes[1, 0].set_ylabel('Loss')
    axes[1, 0].grid(True, alpha=0.3)
    axes[1, 0].legend()
    
    # Combined Loss
    total_loss = [g + d_a + d_b + c + i 
                  for g, d_a, d_b, c, i in zip(
                      losses['generator'], 
                      losses['discriminator_a'],
                      losses['discriminator_b'],
                      losses['cycle_loss'],
                      losses['identity_loss'])]
    axes[1, 1].plot(epochs, total_loss, 'o-', linewidth=2.5, markersize=6, 
                    color='#d32f2f', label='Total Loss')
    axes[1, 1].fill_between(epochs, total_loss, alpha=0.3, color='#d32f2f')
    axes[1, 1].set_title('Total Loss', fontsize=12, fontweight='bold')
    axes[1, 1].set_xlabel('Epoch')
    axes[1, 1].set_ylabel('Loss')
    axes[1, 1].grid(True, alpha=0.3)
    axes[1, 1].legend()
    
    plt.tight_layout()
    
    # Convert to PIL Image
    buf = io.BytesIO()
    plt.savefig(buf, format='png', dpi=100, bbox_inches='tight')
    buf.seek(0)
    img = Image.open(buf)
    plt.close(fig)
    return img


def create_model_info_html():
    """Create HTML with model architecture information"""
    html = """
    <div style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); 
                padding: 30px; border-radius: 15px; color: white; margin-bottom: 20px;">
        <h1 style="margin: 0; font-size: 2.5em;">βš™οΈ Model Architecture</h1>
        <p style="margin: 10px 0 0 0; font-size: 1.1em; opacity: 0.95;">
            CycleGAN for Unpaired Sketch ↔ Photo Translation
        </p>
    </div>
    
    <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 20px; margin: 20px 0;">
        <div style="background: #e3f2fd; padding: 20px; border-radius: 10px;">
            <h3 style="color: #1976d2; margin-top: 0;">🎬 Generator (G)</h3>
            <ul style="color: #333; line-height: 2;">
                <li><strong>Components:</strong> Encoder β†’ Residual Blocks β†’ Decoder</li>
                <li><strong>Encoder:</strong> 2 conv layers (stride 2)</li>
                <li><strong>Residual:</strong> 9 ResBlocks</li>
                <li><strong>Decoder:</strong> 2 transpose conv layers</li>
                <li><strong>Normalization:</strong> Instance Normalization</li>
                <li><strong>Activation:</strong> ReLU (encoder), Tanh (output)</li>
                <li><strong>Features:</strong> 64 β†’ 128 β†’ 256 β†’ 128 β†’ 64</li>
            </ul>
        </div>
        
        <div style="background: #fff3e0; padding: 20px; border-radius: 10px;">
            <h3 style="color: #e65100; margin-top: 0;">πŸ•΅οΈ Discriminator (D)</h3>
            <ul style="color: #333; line-height: 2;">
                <li><strong>Type:</strong> PatchGAN Discriminator</li>
                <li><strong>Input:</strong> 256Γ—256 images</li>
                <li><strong>Patch Size:</strong> 70Γ—70 receptive field</li>
                <li><strong>Layers:</strong> 4 Conv blocks + 1 output conv</li>
                <li><strong>Normalization:</strong> Instance Normalization</li>
                <li><strong>Activation:</strong> LeakyReLU (slope 0.2)</li>
                <li><strong>Output:</strong> 1 channel (real/fake prediction)</li>
            </ul>
        </div>
    </div>
    
    <div style="background: #f3e5f5; padding: 20px; border-radius: 10px; margin: 20px 0;">
        <h3 style="color: #6a1b9a; margin-top: 0;">πŸ“Š Hyperparameters</h3>
        <div style="display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 15px; color: #4a148c;">
            <div><strong>Image Size:</strong> 256Γ—256</div>
            <div><strong>Batch Size:</strong> 4</div>
            <div><strong>Learning Rate:</strong> 2e-4</div>
            <div><strong>Optimizer:</strong> Adam</div>
            <div><strong>β₁, Ξ²β‚‚:</strong> 0.5, 0.999</div>
            <div><strong>Epochs:</strong> 5</div>
            <div><strong>Ξ» (Cycle):</strong> 10.0</div>
            <div><strong>Ξ» (Identity):</strong> 5.0</div>
            <div><strong>Pool Size:</strong> 50 (image replay)</div>
        </div>
    </div>
    """
    return html


# ==================== MAIN INFERENCE FUNCTION ====================
def translate_image(input_image, translation_direction):
    """Perform image translation"""
    if input_image is None:
        return None, "❌ Please upload an image first"
    
    try:
        # Ensure image is RGB
        if input_image.mode != 'RGB':
            input_image = input_image.convert('RGB')
        
        # Convert to tensor
        img_tensor = image_to_tensor(input_image)
        
        # Select appropriate generator
        if translation_direction == "Sketch β†’ Photo":
            generator = G_AB
        else:
            generator = G_BA
        
        # Forward pass
        with torch.no_grad():
            output_tensor = generator(img_tensor)
        
        output_image = tensor_to_image(output_tensor)
        return output_image, "βœ… Translation successful!"
    
    except Exception as e:
        return None, f"❌ Error: {str(e)}"


def create_comparison_figure(original, translated, direction):
    """Create comparison image with labels"""
    fig, axes = plt.subplots(1, 2, figsize=(12, 5))
    
    axes[0].imshow(original)
    axes[0].set_title(f"Original ({direction.split('β†’')[0].strip()})", 
                      fontsize=12, fontweight='bold')
    axes[0].axis('off')
    
    axes[1].imshow(translated)
    axes[1].set_title(f"Translated ({direction.split('β†’')[1].strip()})", 
                      fontsize=12, fontweight='bold')
    axes[1].axis('off')
    
    plt.tight_layout()
    
    buf = io.BytesIO()
    plt.savefig(buf, format='png', dpi=100, bbox_inches='tight')
    buf.seek(0)
    comparison = Image.open(buf)
    plt.close(fig)
    return comparison


# ==================== GRADIO INTERFACE ====================
def create_interface():
    """Create beautiful Gradio interface"""
    
    # Load models and history
    G_AB, G_BA, _, _ = load_models()
    history = load_training_history()
    
    with gr.Blocks(title="CycleGAN: Sketch ↔ Photo Translation") as demo:
        
        gr.HTML("""
        <div style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); 
                    padding: 40px 20px; border-radius: 15px; text-align: center; 
                    margin-bottom: 30px; color: white;">
            <h1 style="margin: 0; font-size: 3em;">🎨 CycleGAN Translation</h1>
            <p style="margin: 15px 0 0 0; font-size: 1.2em; opacity: 0.95;">
                πŸ–ΌοΈ Sketch ↔ Photo Translation | Beautiful Unpaired Image-to-Image Learning
            </p>
            <p style="margin: 10px 0 0 0; font-size: 0.95em; opacity: 0.85;">
                Powered by Cycle Consistency Loss | Running on πŸ”₯ {DEVICE}
            </p>
        </div>
        """.format(DEVICE=str(DEVICE).upper()))
        
        with gr.Tabs():
            
            # ============ TAB 1: IMAGE TRANSLATION ============
            with gr.Tab("🎨 Image Translation", id=0):
                with gr.Row():
                    with gr.Column(scale=1):
                        gr.HTML("<h2 style='color: #667eea;'>Upload & Translate</h2>")
                        
                        input_image = gr.Image(label="πŸ“Έ Input Image", 
                                              type="pil", height=400)
                        
                        direction = gr.Radio(
                            ["Sketch β†’ Photo", "Photo β†’ Sketch"],
                            value="Sketch β†’ Photo",
                            label="πŸ”„ Translation Direction"
                        )
                        
                        translate_btn = gr.Button("πŸš€ Translate Image", 
                                                   size="lg",
                                                   variant="primary")
                        
                        output_status = gr.Textbox(label="Status", 
                                                   interactive=False,
                                                   value="Ready")
                    
                    with gr.Column(scale=1):
                        gr.HTML("<h2 style='color: #667eea;'>Result</h2>")
                        output_image = gr.Image(label="🎯 Translated Image", 
                                               type="pil", height=400)
                
                translate_btn.click(
                    fn=translate_image,
                    inputs=[input_image, direction],
                    outputs=[output_image, output_status]
                )
                
                # Comparison gallery
                gr.HTML("""
                <div style="margin-top: 30px; padding: 20px; background: #f5f5f5; 
                            border-radius: 10px;">
                    <h3 style="color: #667eea;">πŸ“– Example Translations</h3>
                    <p style="color: #666;">
                    This model translates between sketches and photos using <b>Cycle Consistency Loss</b>,
                    enabling unpaired training. The cycle loss ensures that sketch→photo→sketch 
                    reconstruction matches the original.
                    </p>
                </div>
                """)
            
            # ============ TAB 2: LOSS FUNCTIONS ============
            with gr.Tab("πŸ“š Loss Functions", id=1):
                gr.HTML(create_loss_explanation_tab())
            
            # ============ TAB 3: TRAINING HISTORY ============
            with gr.Tab("πŸ“Š Training History", id=2):
                gr.HTML("<h2 style='color: #667eea; text-align: center;'>Training Loss Curves</h2>")
                
                loss_plot = plot_training_losses(history)
                if loss_plot:
                    gr.Image(value=loss_plot, label="Loss Visualization", 
                            show_label=True)
                else:
                    gr.HTML("<p style='text-align: center; color: #999;'>Loading training data...</p>")
                
                # Statistics
                if history:
                    gr.HTML(f"""
                    <div style="display: grid; grid-template-columns: 1fr 1fr 1fr 1fr; gap: 15px; margin-top: 20px;">
                        <div style="background: #e3f2fd; padding: 20px; border-radius: 10px; text-align: center;">
                            <h3 style="color: #1976d2; margin: 0;">Epochs</h3>
                            <p style="font-size: 1.5em; color: #1565c0; margin: 10px 0 0 0;">
                            {history.get('num_epochs_completed', 0)}/{history.get('total_epochs', 5)}
                            </p>
                        </div>
                        
                        <div style="background: #fff3e0; padding: 20px; border-radius: 10px; text-align: center;">
                            <h3 style="color: #e65100; margin: 0;">Best Cycle Loss</h3>
                            <p style="font-size: 1.5em; color: #e65100; margin: 10px 0 0 0;">
                            {history.get('best_cycle_loss', 0):.4f}
                            </p>
                        </div>
                        
                        <div style="background: #f3e5f5; padding: 20px; border-radius: 10px; text-align: center;">
                            <h3 style="color: #6a1b9a; margin: 0;">Final LR</h3>
                            <p style="font-size: 1.5em; color: #6a1b9a; margin: 10px 0 0 0;">
                            2e-4 β†’ 0
                            </p>
                        </div>
                        
                        <div style="background: #e8f5e9; padding: 20px; border-radius: 10px; text-align: center;">
                            <h3 style="color: #2e7d32; margin: 0;">Status</h3>
                            <p style="font-size: 1.5em; color: #2e7d32; margin: 10px 0 0 0;">
                            βœ… Complete
                            </p>
                        </div>
                    </div>
                    """)
            
            # ============ TAB 4: MODEL INFO ============
            with gr.Tab("βš™οΈ Model Architecture", id=3):
                gr.HTML(create_model_info_html())
            
            # ============ TAB 5: ABOUT ============
            with gr.Tab("ℹ️ About", id=4):
                gr.HTML("""
                <div style="padding: 30px;">
                    <h2 style="color: #667eea;">About CycleGAN</h2>
                    
                    <div style="background: #f5f5f5; padding: 20px; border-radius: 10px; margin: 20px 0;">
                        <h3>What is CycleGAN?</h3>
                        <p>
                        CycleGAN is a deep learning model for unpaired image-to-image translation. 
                        Unlike pix2pix, it doesn't require paired training data. Instead, it uses 
                        <b>cycle consistency loss</b> to ensure that translating an image and then 
                        translating it back recovers the original image.
                        </p>
                    </div>
                    
                    <div style="background: #e3f2fd; padding: 20px; border-radius: 10px; margin: 20px 0;">
                        <h3 style="color: #1976d2;">Key Innovation: Cycle Consistency</h3>
                        <p>
                        <b>Traditional Approach:</b> x β†’ y (requires paired data)<br>
                        <b>CycleGAN Approach:</b> x β†’ G(x) β†’ G(F(G(x))) β‰ˆ x<br><br>
                        This enables training on unpaired image collections, making it applicable 
                        to many real-world scenarios where paired data is unavailable.
                        </p>
                    </div>
                    
                    <div style="background: #fff3e0; padding: 20px; border-radius: 10px; margin: 20px 0;">
                        <h3 style="color: #e65100;">Applications</h3>
                        <ul style="color: #333;">
                            <li>πŸ–ΌοΈ Sketch β†’ Photo / Photo β†’ Sketch (this project)</li>
                            <li>πŸŒ… Photo style transfer (summer ↔ winter)</li>
                            <li>🎨 Artistic style transfer</li>
                            <li>🐎 Object morphing (horses ↔ zebras)</li>
                            <li>πŸŒƒ Domain adaptation for autonomous driving</li>
                        </ul>
                    </div>
                    
                    <div style="background: #f3e5f5; padding: 20px; border-radius: 10px; margin: 20px 0;">
                        <h3 style="color: #6a1b9a;">Paper & Resources</h3>
                        <ul style="color: #333;">
                            <li><b>Original Paper:</b> CycleGAN: Unpaired Image-to-Image Translation 
                            (Zhu et al., 2017)</li>
                            <li><b>Repository:</b> junyanz/CycleGAN</li>
                            <li><b>This Implementation:</b> PyTorch with Instance Normalization</li>
                        </ul>
                    </div>
                    
                    <hr style="border: none; border-top: 2px solid #ddd; margin: 30px 0;">
                    
                    <div style="text-align: center; color: #999;">
                        <p>Made with ❀️ for HuggingFace Spaces</p>
                        <p>Dataset: TU-Berlin, Sketchy, QuickDraw, COCO</p>
                    </div>
                </div>
                """)
    
    return demo


# ==================== MAIN ====================
if __name__ == "__main__":
    G_AB, G_BA, D_A, D_B = load_models()
    
    demo = create_interface()
    demo.launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=False,
        theme=gr.themes.Soft()
    )