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dbe4bd1
 
 
c02b5d2
 
 
 
 
 
 
 
1ebd84a
dbe4bd1
1ebd84a
 
5cbe56c
1ebd84a
 
 
c02b5d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5cbe56c
1ebd84a
dbe4bd1
 
1ebd84a
 
5cbe56c
1ebd84a
5cbe56c
 
1ebd84a
 
5cbe56c
1ebd84a
5cbe56c
1ebd84a
5cbe56c
c02b5d2
1ebd84a
 
 
 
 
5cbe56c
c02b5d2
 
 
 
 
 
 
 
1ebd84a
5cbe56c
 
 
 
 
1ebd84a
 
5cbe56c
 
 
1ebd84a
 
5cbe56c
 
 
 
 
 
 
 
c02b5d2
 
 
 
1ebd84a
5cbe56c
 
 
dbe4bd1
c02b5d2
1ebd84a
 
 
5cbe56c
 
1ebd84a
c02b5d2
 
 
 
 
 
 
 
1ebd84a
5cbe56c
 
 
 
 
1ebd84a
 
5cbe56c
 
 
1ebd84a
5cbe56c
 
 
 
 
 
 
 
 
 
c02b5d2
 
 
 
1ebd84a
5cbe56c
 
 
dbe4bd1
c02b5d2
1ebd84a
 
 
 
 
 
 
 
 
 
 
5cbe56c
1ebd84a
 
 
c02b5d2
 
 
 
 
 
 
 
 
 
 
1ebd84a
5cbe56c
1ebd84a
 
5cbe56c
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
import gradio as gr
import torch
from diffusers import StableDiffusionXLPipeline, StableDiffusionXLImg2ImgPipeline
from PIL import Image, PngImagePlugin
from datetime import datetime
import os
import gc
import time
import spaces
from typing import Optional, Tuple
from huggingface_hub import hf_hub_download
import tempfile
import random

# Global pipeline variables
txt2img_pipe = None
img2img_pipe = None
device = "cuda" if torch.cuda.is_available() else "cpu"

# Hugging Face model configuration
MODEL_REPO = "ajsbsd/CyberRealistic-Pony"
MODEL_FILENAME = "cyberrealisticPony_v110.safetensors"
model_id = f"{MODEL_REPO}/{MODEL_FILENAME}"

# Generation configuration for metadata
generation_config = {
    "vae": "SDXL VAE",
    "sampler": "DPM++ 2M Karras",
    "steps": 20
}

def clear_memory():
    """Clear GPU memory"""
    if torch.cuda.is_available():
        torch.cuda.empty_cache()
    gc.collect()

def add_metadata_and_save(image: Image.Image, prompt: str, negative_prompt: str, seed: int, steps: int, guidance: float, strength: Optional[float] = None):
    """Embed generation metadata into a PNG and save it."""
    # Create temporary file with unique name
    temp_path = tempfile.mktemp(suffix=".png")
    
    meta = PngImagePlugin.PngInfo()
    meta.add_text("Prompt", prompt)
    meta.add_text("NegativePrompt", negative_prompt)
    meta.add_text("Model", model_id)
    meta.add_text("VAE", generation_config["vae"])
    meta.add_text("Sampler", generation_config["sampler"])
    meta.add_text("Steps", str(steps))
    meta.add_text("CFG_Scale", str(guidance))
    if strength is not None:
        meta.add_text("Strength", str(strength))
    meta.add_text("Seed", str(seed))
    meta.add_text("Date", datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
    
    image.save(temp_path, "PNG", pnginfo=meta)
    return temp_path

def load_models():
    """Load both text2img and img2img pipelines optimized for Spaces"""
    global txt2img_pipe, img2img_pipe
    
    try:
        print("Loading CyberRealistic Pony models...")
        
        # Download model file using huggingface_hub
        print(f"Downloading model from {MODEL_REPO}...")
        model_path = hf_hub_download(
            repo_id=MODEL_REPO,
            filename=MODEL_FILENAME,
            cache_dir="/tmp/hf_cache"  # Use tmp for Spaces
        )
        print(f"Model downloaded to: {model_path}")
        
        # Load Text2Img pipeline
        if txt2img_pipe is None:
            txt2img_pipe = StableDiffusionXLPipeline.from_single_file(
                model_path,
                torch_dtype=torch.float16 if device == "cuda" else torch.float32,
                use_safetensors=True,
                variant="fp16" if device == "cuda" else None
            )
            
            # Aggressive memory optimizations for Spaces
            txt2img_pipe.enable_attention_slicing()
            txt2img_pipe.enable_vae_slicing()
            
            if device == "cuda":
                txt2img_pipe.enable_model_cpu_offload()
                txt2img_pipe.enable_sequential_cpu_offload()
            else:
                txt2img_pipe = txt2img_pipe.to(device)
        
        # Share components for Img2Img to save memory
        if img2img_pipe is None:
            img2img_pipe = StableDiffusionXLImg2ImgPipeline(
                vae=txt2img_pipe.vae,
                text_encoder=txt2img_pipe.text_encoder,
                text_encoder_2=txt2img_pipe.text_encoder_2,
                tokenizer=txt2img_pipe.tokenizer,
                tokenizer_2=txt2img_pipe.tokenizer_2,
                unet=txt2img_pipe.unet,
                scheduler=txt2img_pipe.scheduler,
            )
            
            # Same optimizations
            img2img_pipe.enable_attention_slicing()
            img2img_pipe.enable_vae_slicing()
            
            if device == "cuda":
                img2img_pipe.enable_model_cpu_offload()
                img2img_pipe.enable_sequential_cpu_offload()
        
        print("Models loaded successfully!")
        return True
        
    except Exception as e:
        print(f"Error loading models: {e}")
        return False

def enhance_prompt(prompt: str, add_quality_tags: bool = True) -> str:
    """Enhance prompt with Pony-style tags"""
    if not prompt.strip():
        return prompt
        
    if prompt.startswith("score_") or not add_quality_tags:
        return prompt
        
    quality_tags = "score_9, score_8_up, score_7_up, masterpiece, best quality, highly detailed"
    return f"{quality_tags}, {prompt}"

def validate_dimensions(width: int, height: int) -> Tuple[int, int]:
    """Ensure dimensions are valid for SDXL"""
    width = ((width + 63) // 64) * 64
    height = ((height + 63) // 64) * 64
    
    # More conservative limits for Spaces
    width = max(512, min(1024, width))
    height = max(512, min(1024, height))
    
    return width, height

def format_status_with_metadata(generation_time: float, width: int, height: int, prompt: str, negative_prompt: str, seed: int, steps: int, guidance: float, strength: Optional[float] = None):
    """Format status message with generation metadata"""
    status_parts = [
        f"βœ… Generated in {generation_time:.1f}s ({width}Γ—{height})",
        f"🎯 Prompt: {prompt[:50]}..." if len(prompt) > 50 else f"🎯 Prompt: {prompt}",
        f"🚫 Negative: {negative_prompt[:30]}..." if negative_prompt and len(negative_prompt) > 30 else f"🚫 Negative: {negative_prompt or 'None'}",
        f"🎲 Seed: {seed}",
        f"πŸ“ Steps: {steps}",
        f"πŸŽ›οΈ CFG: {guidance}"
    ]
    
    if strength is not None:
        status_parts.append(f"πŸ’ͺ Strength: {strength}")
    
    return "\n".join(status_parts)

@spaces.GPU(duration=60)  # GPU decorator for Spaces
def generate_txt2img(prompt, negative_prompt, num_steps, guidance_scale, width, height, seed, add_quality_tags):
    """Generate image from text prompt with Spaces GPU support"""
    global txt2img_pipe
    
    if not prompt.strip():
        return None, "Please enter a prompt"
    
    # Lazy load models
    if txt2img_pipe is None:
        if not load_models():
            return None, "Failed to load models. Please try again."
    
    try:
        clear_memory()
        
        # Validate dimensions
        width, height = validate_dimensions(width, height)
        
        # Handle seed
        if seed == -1:
            seed = random.randint(0, 2147483647)
        
        # Set seed
        generator = torch.Generator(device=device).manual_seed(int(seed))
        
        # Enhance prompt
        enhanced_prompt = enhance_prompt(prompt, add_quality_tags)
        
        print(f"Generating: {enhanced_prompt[:100]}...")
        start_time = time.time()
        
        # Generate with lower memory usage
        with torch.no_grad():
            result = txt2img_pipe(
                prompt=enhanced_prompt,
                negative_prompt=negative_prompt or "",
                num_inference_steps=min(int(num_steps), 30),  # Limit steps for Spaces
                guidance_scale=float(guidance_scale),
                width=width,
                height=height,
                generator=generator
            )
        
        generation_time = time.time() - start_time
        
        # Save with metadata - returns file path
        png_path = add_metadata_and_save(
            result.images[0], enhanced_prompt, negative_prompt or "", 
            seed, num_steps, guidance_scale
        )
        
        # Format status with metadata
        status = format_status_with_metadata(
            generation_time, width, height, enhanced_prompt, 
            negative_prompt or "", seed, num_steps, guidance_scale
        )
        
        return png_path, status
        
    except Exception as e:
        return None, f"Generation failed: {str(e)}"
    finally:
        clear_memory()

@spaces.GPU(duration=60)  # GPU decorator for Spaces
def generate_img2img(input_image, prompt, negative_prompt, num_steps, guidance_scale, strength, seed, add_quality_tags):
    """Generate image from input image + text prompt with Spaces GPU support"""
    global img2img_pipe
    
    if input_image is None:
        return None, "Please upload an input image"
    
    if not prompt.strip():
        return None, "Please enter a prompt"
    
    # Lazy load models
    if img2img_pipe is None:
        if not load_models():
            return None, "Failed to load models. Please try again."
    
    try:
        clear_memory()
        
        # Handle seed
        if seed == -1:
            seed = random.randint(0, 2147483647)
        
        # Set seed
        generator = torch.Generator(device=device).manual_seed(int(seed))
        
        # Enhance prompt
        enhanced_prompt = enhance_prompt(prompt, add_quality_tags)
        
        # Process input image
        if isinstance(input_image, Image.Image):
            if input_image.mode != 'RGB':
                input_image = input_image.convert('RGB')
            
            # Conservative resize for Spaces
            max_size = 768
            input_image.thumbnail((max_size, max_size), Image.Resampling.LANCZOS)
            
            w, h = input_image.size
            w, h = validate_dimensions(w, h)
            input_image = input_image.resize((w, h), Image.Resampling.LANCZOS)
        
        print(f"Transforming: {enhanced_prompt[:100]}...")
        start_time = time.time()
        
        with torch.no_grad():
            result = img2img_pipe(
                prompt=enhanced_prompt,
                negative_prompt=negative_prompt or "",
                image=input_image,
                num_inference_steps=min(int(num_steps), 30),  # Limit steps
                guidance_scale=float(guidance_scale),
                strength=float(strength),
                generator=generator
            )
        
        generation_time = time.time() - start_time
        
        # Save with metadata - returns file path
        png_path = add_metadata_and_save(
            result.images[0], enhanced_prompt, negative_prompt or "", 
            seed, num_steps, guidance_scale, strength
        )
        
        # Format status with metadata
        status = format_status_with_metadata(
            generation_time, w, h, enhanced_prompt, 
            negative_prompt or "", seed, num_steps, guidance_scale, strength
        )
        
        return png_path, status
        
    except Exception as e:
        return None, f"Transformation failed: {str(e)}"
    finally:
        clear_memory()

# Example prompts for inspiration
EXAMPLE_PROMPTS = [
    "beautiful anime girl with long flowing hair, cherry blossoms, soft lighting",
    "cyberpunk cityscape at night, neon lights, rain reflections, detailed architecture",
    "majestic dragon flying over mountains, fantasy landscape, dramatic clouds",
    "cute anthropomorphic fox character, forest background, magical atmosphere",
    "elegant woman in Victorian dress, portrait, ornate background, vintage style",
    "futuristic robot with glowing eyes, metallic surface, sci-fi environment",
    "mystical unicorn in enchanted forest, rainbow mane, sparkles, ethereal lighting",
    "steampunk airship floating in sky, gears and brass, adventure scene"
]

def set_example_prompt():
    """Return a random example prompt"""
    return random.choice(EXAMPLE_PROMPTS)

# Simplified negative prompt for better performance
DEFAULT_NEGATIVE = """
(low quality:1.3), (worst quality:1.3), (bad quality:1.2), blurry, noisy, ugly, deformed, 
(text, watermark:1.4), (extra limbs:1.3), (bad hands:1.3), (bad anatomy:1.2)
"""

# Gradio interface optimized for Spaces
with gr.Blocks(
    title="CyberRealistic Pony Generator", 
    theme=gr.themes.Soft()
) as demo:
    gr.Markdown("""
    # 🎨 CyberRealistic Pony Image Generator
    
    Generate high-quality images using the CyberRealistic Pony SDXL model.
    
    ⚠️ **Note**: First generation may take longer as the model loads. GPU time is limited on Spaces.
    πŸ“‹ **Metadata**: All generated images include embedded metadata (prompt, settings, seed, etc.)
    """)
    
    with gr.Tabs():
        with gr.TabItem("🎨 Text to Image"):
            with gr.Row():
                with gr.Column():
                    with gr.Row():
                        txt2img_prompt = gr.Textbox(
                            label="Prompt",
                            placeholder="beautiful landscape, mountains, sunset",
                            lines=2,
                            scale=4
                        )
                        txt2img_example_btn = gr.Button("🎲 Random Example", scale=1)
                    
                    with gr.Accordion("Advanced Settings", open=False):
                        txt2img_negative = gr.Textbox(
                            label="Negative Prompt",
                            value=DEFAULT_NEGATIVE,
                            lines=2
                        )
                        
                        txt2img_quality_tags = gr.Checkbox(
                            label="Add Quality Tags",
                            value=True
                        )
                        
                        with gr.Row():
                            txt2img_steps = gr.Slider(10, 30, 20, step=1, label="Steps")
                            txt2img_guidance = gr.Slider(1.0, 15.0, 7.5, step=0.5, label="Guidance")
                        
                        with gr.Row():
                            txt2img_width = gr.Slider(512, 1024, 768, step=64, label="Width")
                            txt2img_height = gr.Slider(512, 1024, 768, step=64, label="Height")
                        
                        txt2img_seed = gr.Slider(
                            minimum=-1, maximum=2147483647, value=-1, step=1,
                            label="Seed (-1 for random)"
                        )
                    
                    txt2img_btn = gr.Button("🎨 Generate", variant="primary", size="lg")
                
                with gr.Column():
                    txt2img_output = gr.File(label="Generated PNG with Metadata", file_types=[".png"])
                    txt2img_status = gr.Textbox(label="Generation Info", interactive=False, lines=6)
        
        with gr.TabItem("πŸ–ΌοΈ Image to Image"):
            with gr.Row():
                with gr.Column():
                    img2img_input = gr.Image(label="Input Image", type="pil", height=250)
                    
                    with gr.Row():
                        img2img_prompt = gr.Textbox(
                            label="Prompt",
                            placeholder="digital painting style, vibrant colors",
                            lines=2,
                            scale=4
                        )
                        img2img_example_btn = gr.Button("🎲 Random Example", scale=1)
                    
                    with gr.Accordion("Advanced Settings", open=False):
                        img2img_negative = gr.Textbox(
                            label="Negative Prompt",
                            value=DEFAULT_NEGATIVE,
                            lines=2
                        )
                        
                        img2img_quality_tags = gr.Checkbox(
                            label="Add Quality Tags",
                            value=True
                        )
                        
                        with gr.Row():
                            img2img_steps = gr.Slider(10, 30, 20, step=1, label="Steps")
                            img2img_guidance = gr.Slider(1.0, 15.0, 7.5, step=0.5, label="Guidance")
                        
                        img2img_strength = gr.Slider(
                            0.1, 1.0, 0.75, step=0.05, 
                            label="Strength (Higher = more creative)"
                        )
                        
                        img2img_seed = gr.Slider(
                            minimum=-1, maximum=2147483647, value=-1, step=1,
                            label="Seed (-1 for random)"
                        )
                    
                    img2img_btn = gr.Button("πŸ–ΌοΈ Transform", variant="primary", size="lg")
                
                with gr.Column():
                    img2img_output = gr.File(label="Generated PNG with Metadata", file_types=[".png"])
                    img2img_status = gr.Textbox(label="Generation Info", interactive=False, lines=6)
    
    # Event handlers
    txt2img_btn.click(
        fn=generate_txt2img,
        inputs=[txt2img_prompt, txt2img_negative, txt2img_steps, txt2img_guidance, 
                txt2img_width, txt2img_height, txt2img_seed, txt2img_quality_tags],
        outputs=[txt2img_output, txt2img_status]
    )
    
    img2img_btn.click(
        fn=generate_img2img,
        inputs=[img2img_input, img2img_prompt, img2img_negative, img2img_steps, img2img_guidance, 
                img2img_strength, img2img_seed, img2img_quality_tags],
        outputs=[img2img_output, img2img_status]
    )
    
    # Example prompt buttons
    txt2img_example_btn.click(
        fn=set_example_prompt,
        outputs=[txt2img_prompt]
    )
    
    img2img_example_btn.click(
        fn=set_example_prompt,
        outputs=[img2img_prompt]
    )

print(f"πŸš€ CyberRealistic Pony Generator initialized on {device}")

if __name__ == "__main__":
    demo.launch()