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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30a0cde
eb8e393
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30a0cde
eb8e393
 
 
 
30a0cde
 
eb8e393
 
 
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
"""
Lyra/Lune Flow-Matching Inference Space
Author: AbstractPhil
License: MIT

SD1.5-based flow matching with geometric crystalline architectures.
"""

import os
import torch
import gradio as gr
import numpy as np
from PIL import Image
from typing import Optional, Dict
import spaces

from diffusers import (
    UNet2DConditionModel,
    AutoencoderKL,
    DPMSolverMultistepScheduler,
    EulerDiscreteScheduler
)
from transformers import CLIPTextModel, CLIPTokenizer
from huggingface_hub import hf_hub_download


# ============================================================================
# MODEL LOADING
# ============================================================================

class FlowMatchingPipeline:
    """Custom pipeline for flow-matching inference."""
    
    def __init__(
        self,
        vae: AutoencoderKL,
        text_encoder: CLIPTextModel,
        tokenizer: CLIPTokenizer,
        unet: UNet2DConditionModel,
        scheduler,
        device: str = "cuda"
    ):
        self.vae = vae
        self.text_encoder = text_encoder
        self.tokenizer = tokenizer
        self.unet = unet
        self.scheduler = scheduler
        self.device = device
        
        # VAE scaling factor
        self.vae_scale_factor = 0.18215
        
    def encode_prompt(self, prompt: str, negative_prompt: str = ""):
        """Encode text prompts to embeddings."""
        # Positive prompt
        text_inputs = self.tokenizer(
            prompt,
            padding="max_length",
            max_length=self.tokenizer.model_max_length,
            truncation=True,
            return_tensors="pt",
        )
        text_input_ids = text_inputs.input_ids.to(self.device)
        
        with torch.no_grad():
            prompt_embeds = self.text_encoder(text_input_ids)[0]
        
        # Negative prompt
        if negative_prompt:
            uncond_inputs = self.tokenizer(
                negative_prompt,
                padding="max_length",
                max_length=self.tokenizer.model_max_length,
                truncation=True,
                return_tensors="pt",
            )
            uncond_input_ids = uncond_inputs.input_ids.to(self.device)
            
            with torch.no_grad():
                negative_prompt_embeds = self.text_encoder(uncond_input_ids)[0]
        else:
            negative_prompt_embeds = torch.zeros_like(prompt_embeds)
        
        return prompt_embeds, negative_prompt_embeds
    
    @torch.no_grad()
    def __call__(
        self,
        prompt: str,
        negative_prompt: str = "",
        height: int = 512,
        width: int = 512,
        num_inference_steps: int = 20,
        guidance_scale: float = 7.5,
        shift: float = 2.5,
        use_flow_matching: bool = True,
        prediction_type: str = "epsilon",
        seed: Optional[int] = None,
        progress_callback=None
    ):
        """Generate image using flow matching or standard diffusion."""
        
        # Set seed
        if seed is not None:
            generator = torch.Generator(device=self.device).manual_seed(seed)
        else:
            generator = None
        
        # Encode prompts
        prompt_embeds, negative_prompt_embeds = self.encode_prompt(
            prompt, negative_prompt
        )
        
        # Prepare latents
        latent_channels = 4
        latent_height = height // 8
        latent_width = width // 8
        
        latents = torch.randn(
            (1, latent_channels, latent_height, latent_width),
            generator=generator,
            device=self.device,
            dtype=torch.float32
        )
        
        # Set timesteps
        self.scheduler.set_timesteps(num_inference_steps, device=self.device)
        timesteps = self.scheduler.timesteps
        
        # Denoising loop
        for i, t in enumerate(timesteps):
            if progress_callback:
                progress_callback(i, num_inference_steps, f"Step {i+1}/{num_inference_steps}")
            
            # Expand latents for classifier-free guidance
            latent_model_input = torch.cat([latents] * 2) if guidance_scale > 1.0 else latents
            
            # Apply shift for flow matching
            if use_flow_matching and shift > 0:
                # Compute sigma from timestep with shift
                sigma = t.float() / 1000.0
                sigma_shifted = (shift * sigma) / (1 + (shift - 1) * sigma)
                
                # Scale latent input
                scaling = torch.sqrt(1 + sigma_shifted ** 2)
                latent_model_input = latent_model_input / scaling
            
            # Prepare timestep
            timestep = t.expand(latent_model_input.shape[0])
            
            # Predict noise/velocity
            text_embeds = torch.cat([negative_prompt_embeds, prompt_embeds]) if guidance_scale > 1.0 else prompt_embeds
            
            noise_pred = self.unet(
                latent_model_input,
                timestep,
                encoder_hidden_states=text_embeds,
                return_dict=False
            )[0]
            
            # Classifier-free guidance
            if guidance_scale > 1.0:
                noise_pred_uncond, noise_pred_text = noise_pred.chunk(2)
                noise_pred = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)
            
            # Flow matching step
            if use_flow_matching:
                # Manual flow matching update
                sigma = t.float() / 1000.0
                sigma_shifted = (shift * sigma) / (1 + (shift - 1) * sigma)
                
                if prediction_type == "v_prediction":
                    # Convert v-prediction to epsilon
                    v_pred = noise_pred
                    alpha_t = torch.sqrt(1 - sigma_shifted ** 2)
                    sigma_t = sigma_shifted
                    noise_pred = alpha_t * v_pred + sigma_t * latents
                
                # Compute next latent
                dt = -1.0 / num_inference_steps
                latents = latents + dt * noise_pred
            else:
                # Standard scheduler step
                latents = self.scheduler.step(
                    noise_pred, t, latents, return_dict=False
                )[0]
        
        # Decode latents
        latents = latents / self.vae_scale_factor
        
        with torch.no_grad():
            image = self.vae.decode(latents).sample
        
        # Convert to PIL
        image = (image / 2 + 0.5).clamp(0, 1)
        image = image.cpu().permute(0, 2, 3, 1).float().numpy()
        image = (image * 255).round().astype("uint8")
        image = Image.fromarray(image[0])
        
        return image


def load_lune_checkpoint(repo_id: str, filename: str, device: str = "cuda"):
    """Load Lune checkpoint from .pt file."""
    print(f"πŸ“₯ Downloading checkpoint: {repo_id}/{filename}")
    
    checkpoint_path = hf_hub_download(
        repo_id=repo_id,
        filename=filename,
        repo_type="model"
    )
    
    print(f"βœ“ Downloaded to: {checkpoint_path}")
    print(f"πŸ“¦ Loading checkpoint...")
    
    checkpoint = torch.load(checkpoint_path, map_location="cpu")
    
    # Initialize UNet with SD1.5 config
    print(f"πŸ—οΈ Initializing SD1.5 UNet...")
    unet = UNet2DConditionModel.from_pretrained(
        "runwayml/stable-diffusion-v1-5",
        subfolder="unet",
        torch_dtype=torch.float32
    )
    
    # Load student weights
    student_state_dict = checkpoint["student"]
    
    # Strip "unet." prefix if present
    cleaned_dict = {}
    for key, value in student_state_dict.items():
        if key.startswith("unet."):
            cleaned_dict[key[5:]] = value
        else:
            cleaned_dict[key] = value
    
    # Load weights
    unet.load_state_dict(cleaned_dict, strict=False)
    
    step = checkpoint.get("gstep", "unknown")
    print(f"βœ… Loaded checkpoint from step {step}")
    
    return unet.to(device)


def initialize_pipeline(model_choice: str, device: str = "cuda"):
    """Initialize the complete pipeline."""
    
    print(f"πŸš€ Initializing {model_choice} pipeline...")
    
    # Load base components
    print("Loading VAE...")
    vae = AutoencoderKL.from_pretrained(
        "runwayml/stable-diffusion-v1-5",
        subfolder="vae",
        torch_dtype=torch.float32
    ).to(device)
    
    print("Loading text encoder...")
    text_encoder = CLIPTextModel.from_pretrained(
        "openai/clip-vit-large-patch14",
        torch_dtype=torch.float32
    ).to(device)
    
    tokenizer = CLIPTokenizer.from_pretrained(
        "openai/clip-vit-large-patch14"
    )
    
    # Load UNet based on model choice
    if model_choice == "Flow-Lune (Latest)":
        # Load latest checkpoint from repo
        repo_id = "AbstractPhil/sd15-flow-lune"
        # Find latest checkpoint - for now use a known one
        filename = "sd15_flow_lune_e34_s34000.pt"
        unet = load_lune_checkpoint(repo_id, filename, device)
    
    elif model_choice == "SD1.5 Base":
        print("Loading SD1.5 base UNet...")
        unet = UNet2DConditionModel.from_pretrained(
            "runwayml/stable-diffusion-v1-5",
            subfolder="unet",
            torch_dtype=torch.float32
        ).to(device)
    
    else:
        raise ValueError(f"Unknown model: {model_choice}")
    
    # Initialize scheduler
    scheduler = EulerDiscreteScheduler.from_pretrained(
        "runwayml/stable-diffusion-v1-5",
        subfolder="scheduler"
    )
    
    print("βœ… Pipeline initialized!")
    
    return FlowMatchingPipeline(
        vae=vae,
        text_encoder=text_encoder,
        tokenizer=tokenizer,
        unet=unet,
        scheduler=scheduler,
        device=device
    )


# ============================================================================
# GLOBAL STATE
# ============================================================================

# Initialize with None, will load on first inference
CURRENT_PIPELINE = None
CURRENT_MODEL = None


def get_pipeline(model_choice: str):
    """Get or create pipeline for selected model."""
    global CURRENT_PIPELINE, CURRENT_MODEL
    
    if CURRENT_PIPELINE is None or CURRENT_MODEL != model_choice:
        CURRENT_PIPELINE = initialize_pipeline(model_choice, device="cuda")
        CURRENT_MODEL = model_choice
    
    return CURRENT_PIPELINE


# ============================================================================
# INFERENCE
# ============================================================================

def estimate_duration(num_steps: int, width: int, height: int) -> int:
    """Estimate GPU duration based on generation parameters."""
    # Base time per step (seconds)
    base_time_per_step = 0.3
    
    # Resolution scaling
    resolution_factor = (width * height) / (512 * 512)
    
    # Total estimate
    estimated = num_steps * base_time_per_step * resolution_factor
    
    # Add 15 seconds for model loading overhead
    return int(estimated + 15)


@spaces.GPU(duration=lambda *args: estimate_duration(args[3], args[5], args[6]))
def generate_image(
    prompt: str,
    negative_prompt: str,
    model_choice: str,
    num_steps: int,
    cfg_scale: float,
    width: int,
    height: int,
    shift: float,
    use_flow_matching: bool,
    prediction_type: str,
    seed: int,
    randomize_seed: bool,
    progress=gr.Progress()
):
    """Generate image with ZeroGPU support."""
    
    # Randomize seed if requested
    if randomize_seed:
        seed = np.random.randint(0, 2**32 - 1)
    
    # Progress tracking
    def progress_callback(step, total, desc):
        progress((step + 1) / total, desc=desc)
    
    try:
        # Get pipeline
        pipeline = get_pipeline(model_choice)
        
        # Generate
        progress(0.05, desc="Starting generation...")
        
        image = pipeline(
            prompt=prompt,
            negative_prompt=negative_prompt,
            height=height,
            width=width,
            num_inference_steps=num_steps,
            guidance_scale=cfg_scale,
            shift=shift,
            use_flow_matching=use_flow_matching,
            prediction_type=prediction_type,
            seed=seed,
            progress_callback=progress_callback
        )
        
        progress(1.0, desc="Complete!")
        
        return image, seed
    
    except Exception as e:
        print(f"❌ Generation failed: {e}")
        raise e


# ============================================================================
# GRADIO UI
# ============================================================================

def create_demo():
    """Create Gradio interface."""
    
    with gr.Blocks(theme=gr.themes.Soft()) as demo:
        gr.Markdown("""
        # πŸŒ™ Lyra/Lune Flow-Matching Image Generation
        
        **Geometric crystalline diffusion with flow matching** by [AbstractPhil](https://huggingface.co/AbstractPhil)
        
        Generate images using SD1.5-based flow matching with pentachoron geometric structures. 
        Achieves high quality with dramatically reduced step counts through geometric efficiency.
        """)
        
        with gr.Row():
            with gr.Column(scale=1):
                # Prompt
                prompt = gr.TextArea(
                    label="Prompt",
                    placeholder="A beautiful landscape with mountains and a lake at sunset...",
                    lines=3
                )
                
                negative_prompt = gr.TextArea(
                    label="Negative Prompt",
                    placeholder="blurry, low quality, distorted...",
                    lines=2
                )
                
                # Model selection
                model_choice = gr.Dropdown(
                    label="Model",
                    choices=[
                        "Flow-Lune (Latest)",
                        "SD1.5 Base"
                    ],
                    value="Flow-Lune (Latest)"
                )
                
                # Flow matching settings
                with gr.Accordion("Flow Matching Settings", open=True):
                    use_flow_matching = gr.Checkbox(
                        label="Enable Flow Matching",
                        value=True,
                        info="Use flow matching ODE integration"
                    )
                    
                    shift = gr.Slider(
                        label="Shift",
                        minimum=0.0,
                        maximum=5.0,
                        value=2.5,
                        step=0.1,
                        info="Flow matching shift parameter (0=disabled, 1-3 typical)"
                    )
                    
                    prediction_type = gr.Radio(
                        label="Prediction Type",
                        choices=["epsilon", "v_prediction"],
                        value="epsilon",
                        info="Type of model prediction"
                    )
                
                # Generation settings
                with gr.Accordion("Generation Settings", open=True):
                    num_steps = gr.Slider(
                        label="Steps",
                        minimum=1,
                        maximum=50,
                        value=20,
                        step=1,
                        info="Flow matching typically needs fewer steps (15-25)"
                    )
                    
                    cfg_scale = gr.Slider(
                        label="CFG Scale",
                        minimum=1.0,
                        maximum=20.0,
                        value=7.5,
                        step=0.5
                    )
                    
                    with gr.Row():
                        width = gr.Slider(
                            label="Width",
                            minimum=256,
                            maximum=1024,
                            value=512,
                            step=64
                        )
                        
                        height = gr.Slider(
                            label="Height",
                            minimum=256,
                            maximum=1024,
                            value=512,
                            step=64
                        )
                    
                    seed = gr.Slider(
                        label="Seed",
                        minimum=0,
                        maximum=2**32 - 1,
                        value=42,
                        step=1
                    )
                    
                    randomize_seed = gr.Checkbox(
                        label="Randomize Seed",
                        value=True
                    )
                
                generate_btn = gr.Button("🎨 Generate", variant="primary", size="lg")
            
            with gr.Column(scale=1):
                output_image = gr.Image(
                    label="Generated Image",
                    type="pil"
                )
                
                output_seed = gr.Number(
                    label="Used Seed",
                    precision=0
                )
                
                gr.Markdown("""
                ### Tips:
                - **Flow matching** works best with 15-25 steps (vs 50+ for standard diffusion)
                - **Shift** controls the flow trajectory (2.0-2.5 recommended for Lune)
                - Lower shift = more direct path, higher shift = more exploration
                - Try **v_prediction** mode if epsilon gives unstable results
                
                ### Model Info:
                - **Flow-Lune**: Trained with flow matching on 500k SD1.5 distillation pairs
                - **SD1.5 Base**: Standard Stable Diffusion 1.5 for comparison
                
                [πŸ“š Learn more about geometric deep learning](https://github.com/AbstractEyes/lattice_vocabulary)
                """)
        
        # Examples
        gr.Examples(
            examples=[
                [
                    "A serene mountain landscape at golden hour, crystal clear lake reflecting snow-capped peaks, photorealistic, 8k",
                    "blurry, low quality",
                    "Flow-Lune (Latest)",
                    20,
                    7.5,
                    512,
                    512,
                    2.5,
                    True,
                    "epsilon",
                    42,
                    False
                ],
                [
                    "A futuristic cyberpunk city at night, neon lights, rain-slicked streets, highly detailed",
                    "low quality, blurry",
                    "Flow-Lune (Latest)",
                    22,
                    8.0,
                    512,
                    512,
                    2.5,
                    True,
                    "epsilon",
                    123,
                    False
                ],
                [
                    "Portrait of a majestic lion, golden mane, dramatic lighting, wildlife photography",
                    "cartoon, painting",
                    "Flow-Lune (Latest)",
                    18,
                    7.0,
                    512,
                    512,
                    2.0,
                    True,
                    "epsilon",
                    456,
                    False
                ]
            ],
            inputs=[
                prompt, negative_prompt, model_choice, num_steps, cfg_scale,
                width, height, shift, use_flow_matching, prediction_type,
                seed, randomize_seed
            ],
            outputs=[output_image, output_seed],
            fn=generate_image,
            cache_examples=False
        )
        
        # Event handlers
        generate_btn.click(
            fn=generate_image,
            inputs=[
                prompt, negative_prompt, model_choice, num_steps, cfg_scale,
                width, height, shift, use_flow_matching, prediction_type,
                seed, randomize_seed
            ],
            outputs=[output_image, output_seed]
        )
    
    return demo


# ============================================================================
# LAUNCH
# ============================================================================

if __name__ == "__main__":
    demo = create_demo()
    demo.queue(max_size=20)
    demo.launch(show_api=False)