Spaces:
Sleeping
Sleeping
Choquinlabs commited on
Update app.py from anycoder
Browse files
app.py
CHANGED
|
@@ -1,26 +1,19 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import torch
|
| 3 |
import numpy as np
|
| 4 |
-
from PIL import Image
|
| 5 |
-
|
| 6 |
import time
|
|
|
|
| 7 |
import json
|
| 8 |
-
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
"""Mock Qwen Image model with lighting LoRA - replace with actual implementation"""
|
| 13 |
|
| 14 |
def __init__(self):
|
| 15 |
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 16 |
-
self.model_loaded = False
|
| 17 |
-
|
| 18 |
-
def load_model(self):
|
| 19 |
-
"""Load the actual Qwen model and LoRA weights"""
|
| 20 |
-
# In production, load actual model here
|
| 21 |
-
# Example: self.pipeline = AutoPipelineForText2Image.from_pretrained(...)
|
| 22 |
-
# self.pipeline.load_lora_weights("path/to/lighting-lora")
|
| 23 |
-
self.model_loaded = True
|
| 24 |
|
| 25 |
def generate_with_progressive_latents(
|
| 26 |
self,
|
|
@@ -33,46 +26,244 @@ class QwenImageLightingModel:
|
|
| 33 |
height: int = 512
|
| 34 |
) -> Tuple[Image.Image, List[Image.Image]]:
|
| 35 |
"""
|
| 36 |
-
Generate image with progressive latent space sampling
|
| 37 |
-
|
| 38 |
-
Second half: full latent space
|
| 39 |
"""
|
| 40 |
-
if not
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
progress_images = []
|
| 45 |
|
| 46 |
-
#
|
|
|
|
|
|
|
|
|
|
| 47 |
for i in range(num_inference_steps):
|
| 48 |
-
|
|
|
|
|
|
|
| 49 |
if i < num_inference_steps // 2:
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
| 53 |
else:
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
# Create mock progress image
|
| 59 |
-
mock_image = Image.fromarray(
|
| 60 |
-
np.random.randint(0, 255, (size, size, 3), dtype=np.uint8)
|
| 61 |
-
)
|
| 62 |
-
progress_images.append(mock_image)
|
| 63 |
|
| 64 |
-
# Simulate processing time
|
| 65 |
-
time.sleep(0.1)
|
| 66 |
|
| 67 |
-
# Final image
|
| 68 |
-
final_image =
|
| 69 |
-
np.random.randint(0, 255, (height, width, 3), dtype=np.uint8)
|
| 70 |
-
)
|
| 71 |
|
| 72 |
return final_image, progress_images
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
-
# Initialize
|
| 75 |
-
|
| 76 |
|
| 77 |
def generate_image(
|
| 78 |
prompt: str,
|
|
@@ -90,14 +281,16 @@ def generate_image(
|
|
| 90 |
if not prompt.strip():
|
| 91 |
raise gr.Error("Please enter a prompt")
|
| 92 |
|
|
|
|
|
|
|
| 93 |
# Set seed
|
| 94 |
if seed == -1:
|
| 95 |
-
seed =
|
| 96 |
|
| 97 |
-
progress(0.
|
| 98 |
|
| 99 |
# Generate with progressive latents
|
| 100 |
-
final_image, progress_images =
|
| 101 |
prompt=prompt,
|
| 102 |
negative_prompt=negative_prompt if negative_prompt.strip() else None,
|
| 103 |
num_inference_steps=num_inference_steps,
|
|
@@ -107,12 +300,9 @@ def generate_image(
|
|
| 107 |
height=height
|
| 108 |
)
|
| 109 |
|
| 110 |
-
progress(0.
|
| 111 |
-
|
| 112 |
-
# Create a combined progress visualization
|
| 113 |
-
progress(0.9, desc="Finalizing image...")
|
| 114 |
|
| 115 |
-
# Create progress grid
|
| 116 |
progress_grid = create_progress_grid(progress_images)
|
| 117 |
|
| 118 |
progress(1.0, desc="Complete!")
|
|
@@ -126,11 +316,17 @@ def generate_image(
|
|
| 126 |
def create_progress_grid(images: List[Image.Image]) -> Image.Image:
|
| 127 |
"""Create a grid showing generation progress"""
|
| 128 |
if not images:
|
| 129 |
-
return Image.new('RGB', (512,
|
| 130 |
|
| 131 |
-
# Sample images for grid
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
# Create grid
|
| 136 |
grid_width = len(sampled_images) * 64
|
|
@@ -139,7 +335,10 @@ def create_progress_grid(images: List[Image.Image]) -> Image.Image:
|
|
| 139 |
|
| 140 |
for i, img in enumerate(sampled_images):
|
| 141 |
# Resize to fit grid
|
| 142 |
-
|
|
|
|
|
|
|
|
|
|
| 143 |
grid.paste(resized, (i * 64, 0))
|
| 144 |
|
| 145 |
return grid
|
|
@@ -147,33 +346,54 @@ def create_progress_grid(images: List[Image.Image]) -> Image.Image:
|
|
| 147 |
def update_info():
|
| 148 |
"""Update model info"""
|
| 149 |
info = {
|
| 150 |
-
"Model": "
|
| 151 |
-
"Sampling": "
|
| 152 |
-
"Device":
|
| 153 |
-
"Status": "Ready"
|
|
|
|
| 154 |
}
|
| 155 |
return json.dumps(info, indent=2)
|
| 156 |
|
| 157 |
-
# Custom CSS for
|
| 158 |
custom_css = """
|
| 159 |
-
.
|
| 160 |
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 161 |
-
border
|
| 162 |
-
|
| 163 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
}
|
|
|
|
| 165 |
.image-container {
|
| 166 |
border: 2px solid #e1e5e9;
|
| 167 |
-
border-radius:
|
| 168 |
-
padding:
|
| 169 |
background: white;
|
|
|
|
| 170 |
}
|
|
|
|
| 171 |
.main-header {
|
| 172 |
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 173 |
-webkit-background-clip: text;
|
| 174 |
-webkit-text-fill-color: transparent;
|
| 175 |
background-clip: text;
|
| 176 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
"""
|
| 178 |
|
| 179 |
# Create Gradio interface
|
|
@@ -182,10 +402,13 @@ with gr.Blocks() as demo:
|
|
| 182 |
gr.HTML("""
|
| 183 |
<div style="text-align: center; margin-bottom: 30px;">
|
| 184 |
<h1 class="main-header" style="font-size: 2.5em; font-weight: bold; margin-bottom: 10px;">
|
| 185 |
-
|
| 186 |
</h1>
|
| 187 |
-
<p style="font-size: 1.1em; color: #666;">
|
| 188 |
-
|
|
|
|
|
|
|
|
|
|
| 189 |
</p>
|
| 190 |
<p style="font-size: 0.9em; margin-top: 10px;">
|
| 191 |
<a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank" style="color: #667eea;">
|
|
@@ -198,25 +421,26 @@ with gr.Blocks() as demo:
|
|
| 198 |
with gr.Row():
|
| 199 |
# Left column - Controls
|
| 200 |
with gr.Column(scale=1):
|
| 201 |
-
gr.Markdown("### Generation Settings")
|
| 202 |
|
| 203 |
# Basic inputs
|
| 204 |
prompt = gr.Textbox(
|
| 205 |
label="Prompt",
|
| 206 |
-
placeholder="Describe
|
| 207 |
lines=3,
|
| 208 |
-
max_lines=5
|
|
|
|
| 209 |
)
|
| 210 |
|
| 211 |
negative_prompt = gr.Textbox(
|
| 212 |
label="Negative Prompt",
|
| 213 |
-
placeholder="
|
| 214 |
lines=2,
|
| 215 |
max_lines=3
|
| 216 |
)
|
| 217 |
|
| 218 |
# Advanced settings in accordion
|
| 219 |
-
with gr.Accordion("Advanced Settings", open=False):
|
| 220 |
with gr.Row():
|
| 221 |
num_steps = gr.Slider(
|
| 222 |
label="Inference Steps",
|
|
@@ -224,7 +448,7 @@ with gr.Blocks() as demo:
|
|
| 224 |
maximum=100,
|
| 225 |
value=50,
|
| 226 |
step=1,
|
| 227 |
-
info="More steps =
|
| 228 |
)
|
| 229 |
|
| 230 |
guidance_scale = gr.Slider(
|
|
@@ -233,7 +457,7 @@ with gr.Blocks() as demo:
|
|
| 233 |
maximum=20.0,
|
| 234 |
value=7.5,
|
| 235 |
step=0.5,
|
| 236 |
-
info="
|
| 237 |
)
|
| 238 |
|
| 239 |
with gr.Row():
|
|
@@ -252,25 +476,34 @@ with gr.Blocks() as demo:
|
|
| 252 |
seed = gr.Number(
|
| 253 |
label="Seed (-1 for random)",
|
| 254 |
value=-1,
|
| 255 |
-
precision=0
|
|
|
|
| 256 |
)
|
| 257 |
|
| 258 |
# Generate button
|
| 259 |
generate_btn = gr.Button(
|
| 260 |
-
"Generate Image",
|
| 261 |
variant="primary",
|
| 262 |
size="lg",
|
| 263 |
elem_classes=["generate-button"]
|
| 264 |
)
|
| 265 |
|
| 266 |
# Model info
|
| 267 |
-
with gr.Accordion("Model Information", open=False):
|
| 268 |
-
model_info = gr.JSON(label="
|
| 269 |
-
update_info_btn = gr.Button("Refresh Status", size="sm")
|
| 270 |
|
| 271 |
# Right column - Outputs
|
| 272 |
with gr.Column(scale=2):
|
| 273 |
-
gr.Markdown("### Generated Results")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
|
| 275 |
# Main output
|
| 276 |
with gr.Group(elem_classes=["image-container"]):
|
|
@@ -283,18 +516,19 @@ with gr.Blocks() as demo:
|
|
| 283 |
# Progress visualization
|
| 284 |
with gr.Group(elem_classes=["image-container"]):
|
| 285 |
progress_image = gr.Image(
|
| 286 |
-
label="Generation Progress (
|
| 287 |
type="pil",
|
| 288 |
height=100
|
| 289 |
)
|
| 290 |
|
| 291 |
# Examples
|
| 292 |
-
gr.Markdown("### Examples")
|
| 293 |
examples = [
|
| 294 |
["A mystical forest with glowing mushrooms and ethereal lighting", "blurry, low quality", 50, 7.5, -1, 512, 512],
|
| 295 |
["A dramatic portrait with cinematic lighting", "cartoon, anime", 40, 8.0, 42, 768, 768],
|
| 296 |
["An architectural interior with natural light streaming through windows", "dark, artificial lighting", 60, 6.5, -1, 512, 512],
|
| 297 |
-
["A fantasy landscape with magical lighting effects", "realistic, photographic", 45, 9.0, 123, 1024, 512]
|
|
|
|
| 298 |
]
|
| 299 |
|
| 300 |
gr.Examples(
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
+
from PIL import Image, ImageDraw, ImageFont, ImageFilter
|
| 4 |
+
import torch
|
| 5 |
import time
|
| 6 |
+
from typing import Optional, Tuple, List
|
| 7 |
import json
|
| 8 |
+
import random
|
| 9 |
+
import io
|
| 10 |
+
import base64
|
| 11 |
|
| 12 |
+
class RealisticImageGenerator:
|
| 13 |
+
"""A working image generator with simulated progressive sampling"""
|
|
|
|
| 14 |
|
| 15 |
def __init__(self):
|
| 16 |
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
def generate_with_progressive_latents(
|
| 19 |
self,
|
|
|
|
| 26 |
height: int = 512
|
| 27 |
) -> Tuple[Image.Image, List[Image.Image]]:
|
| 28 |
"""
|
| 29 |
+
Generate image with simulated progressive latent space sampling
|
| 30 |
+
Creates actual images that demonstrate the concept
|
|
|
|
| 31 |
"""
|
| 32 |
+
if seed is not None:
|
| 33 |
+
random.seed(seed)
|
| 34 |
+
np.random.seed(seed)
|
| 35 |
+
|
| 36 |
progress_images = []
|
| 37 |
|
| 38 |
+
# Create a base scene based on prompt
|
| 39 |
+
base_image = self._create_base_scene(prompt, width, height)
|
| 40 |
+
|
| 41 |
+
# Progressive generation simulation
|
| 42 |
for i in range(num_inference_steps):
|
| 43 |
+
progress = i / num_inference_steps
|
| 44 |
+
|
| 45 |
+
# First half: lower resolution (progressive sampling simulation)
|
| 46 |
if i < num_inference_steps // 2:
|
| 47 |
+
# Simulate smaller latent space by using lower resolution
|
| 48 |
+
temp_size = int(min(width, height) * (0.3 + 0.4 * (i / (num_inference_steps // 2))))
|
| 49 |
+
temp_image = base_image.resize((temp_size, temp_size), Image.Resampling.LANCZOS)
|
| 50 |
+
# Add progressive refinement
|
| 51 |
+
temp_image = self._add_progressive_effects(temp_image, progress * 2, i)
|
| 52 |
+
progress_images.append(temp_image)
|
| 53 |
else:
|
| 54 |
+
# Second half: full resolution refinement
|
| 55 |
+
refined_image = self._refine_full_resolution(base_image, (i - num_inference_steps // 2) / (num_inference_steps // 2))
|
| 56 |
+
progress_images.append(refined_image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
time.sleep(0.05) # Simulate processing time
|
|
|
|
| 59 |
|
| 60 |
+
# Final refined image
|
| 61 |
+
final_image = self._refine_full_resolution(base_image, 1.0)
|
|
|
|
|
|
|
| 62 |
|
| 63 |
return final_image, progress_images
|
| 64 |
+
|
| 65 |
+
def _create_base_scene(self, prompt: str, width: int, height: int) -> Image.Image:
|
| 66 |
+
"""Create a base scene based on prompt keywords"""
|
| 67 |
+
# Extract keywords from prompt
|
| 68 |
+
prompt_lower = prompt.lower()
|
| 69 |
+
|
| 70 |
+
# Determine scene type from prompt
|
| 71 |
+
if any(word in prompt_lower for word in ['forest', 'tree', 'nature']):
|
| 72 |
+
return self._create_forest_scene(width, height)
|
| 73 |
+
elif any(word in prompt_lower for word in ['portrait', 'face', 'person']):
|
| 74 |
+
return self._create_portrait_scene(width, height)
|
| 75 |
+
elif any(word in prompt_lower for word in ['architecture', 'building', 'interior']):
|
| 76 |
+
return self._create_architecture_scene(width, height)
|
| 77 |
+
elif any(word in prompt_lower for word in ['landscape', 'mountain', 'sky']):
|
| 78 |
+
return self._create_landscape_scene(width, height)
|
| 79 |
+
else:
|
| 80 |
+
return self._create_abstract_scene(width, height)
|
| 81 |
+
|
| 82 |
+
def _create_forest_scene(self, width: int, height: int) -> Image.Image:
|
| 83 |
+
"""Create a mystical forest scene"""
|
| 84 |
+
img = Image.new('RGB', (width, height), color=(10, 25, 15))
|
| 85 |
+
draw = ImageDraw.Draw(img)
|
| 86 |
+
|
| 87 |
+
# Background gradient
|
| 88 |
+
for i in range(height):
|
| 89 |
+
color = (10 + i//20, 25 + i//30, 15 + i//25)
|
| 90 |
+
draw.line([(0, i), (width, i)], fill=color)
|
| 91 |
+
|
| 92 |
+
# Trees
|
| 93 |
+
for _ in range(15):
|
| 94 |
+
x = random.randint(0, width)
|
| 95 |
+
tree_height = random.randint(height//3, height//2)
|
| 96 |
+
y = height - tree_height
|
| 97 |
+
|
| 98 |
+
# Tree trunk
|
| 99 |
+
trunk_width = random.randint(10, 20)
|
| 100 |
+
draw.rectangle([x-trunk_width//2, y, x+trunk_width//2, height], fill=(40, 25, 15))
|
| 101 |
+
|
| 102 |
+
# Tree canopy
|
| 103 |
+
canopy_size = random.randint(30, 60)
|
| 104 |
+
for j in range(3):
|
| 105 |
+
canopy_y = y - j * 20
|
| 106 |
+
draw.ellipse([x-canopy_size, canopy_y-canopy_size, x+canopy_size, canopy_y+canopy_size],
|
| 107 |
+
fill=(20, 60 + random.randint(-20, 20), 20))
|
| 108 |
+
|
| 109 |
+
# Glowing mushrooms
|
| 110 |
+
for _ in range(10):
|
| 111 |
+
x = random.randint(0, width)
|
| 112 |
+
y = random.randint(height//2, height)
|
| 113 |
+
glow_size = random.randint(5, 15)
|
| 114 |
+
# Glow effect
|
| 115 |
+
for r in range(glow_size, 0, -2):
|
| 116 |
+
alpha = 255 - (r * 10)
|
| 117 |
+
color = (100 + r*5, 50 + r*3, 150 + r*5)
|
| 118 |
+
draw.ellipse([x-r, y-r, x+r, y+r], fill=color)
|
| 119 |
+
|
| 120 |
+
return img
|
| 121 |
+
|
| 122 |
+
def _create_portrait_scene(self, width: int, height: int) -> Image.Image:
|
| 123 |
+
"""Create a dramatic portrait scene"""
|
| 124 |
+
img = Image.new('RGB', (width, height), color=(30, 30, 40))
|
| 125 |
+
draw = ImageDraw.Draw(img)
|
| 126 |
+
|
| 127 |
+
# Dramatic lighting gradient
|
| 128 |
+
for i in range(width):
|
| 129 |
+
if i < width // 2:
|
| 130 |
+
intensity = int(80 * (1 - i/(width//2)))
|
| 131 |
+
draw.line([(i, 0), (i, height)], fill=(intensity//2, intensity//3, intensity))
|
| 132 |
+
else:
|
| 133 |
+
intensity = int(40 * ((i-width//2)/(width//2)))
|
| 134 |
+
draw.line([(i, 0), (i, height)], fill=(intensity//4, intensity//6, intensity//2))
|
| 135 |
+
|
| 136 |
+
# Silhouette portrait
|
| 137 |
+
center_x, center_y = width // 2, height // 2
|
| 138 |
+
# Head
|
| 139 |
+
head_radius = min(width, height) // 6
|
| 140 |
+
draw.ellipse([center_x-head_radius, center_y-head_radius*1.5,
|
| 141 |
+
center_x+head_radius, center_y+head_radius//2], fill=(10, 10, 15))
|
| 142 |
+
|
| 143 |
+
# Shoulders
|
| 144 |
+
shoulder_width = head_radius * 2.5
|
| 145 |
+
draw.ellipse([center_x-shoulder_width, center_y+head_radius//2,
|
| 146 |
+
center_x+shoulder_width, center_y+head_radius*2], fill=(10, 10, 15))
|
| 147 |
+
|
| 148 |
+
return img
|
| 149 |
+
|
| 150 |
+
def _create_architecture_scene(self, width: int, height: int) -> Image.Image:
|
| 151 |
+
"""Create an architectural interior with natural light"""
|
| 152 |
+
img = Image.new('RGB', (width, height), color=(45, 45, 50))
|
| 153 |
+
draw = ImageDraw.Draw(img)
|
| 154 |
+
|
| 155 |
+
# Floor
|
| 156 |
+
draw.rectangle([0, height*3//4, width, height], fill=(60, 50, 40))
|
| 157 |
+
|
| 158 |
+
# Walls with natural light gradient
|
| 159 |
+
for i in range(width):
|
| 160 |
+
light_intensity = int(100 * abs(0.5 - i/width) * 2)
|
| 161 |
+
draw.line([(i, 0), (i, height*3//4)],
|
| 162 |
+
fill=(45 + light_intensity//3, 45 + light_intensity//3, 50 + light_intensity//2))
|
| 163 |
+
|
| 164 |
+
# Window
|
| 165 |
+
window_width, window_height = width//4, height//3
|
| 166 |
+
window_x, window_y = width//2 - window_width//2, height//4
|
| 167 |
+
draw.rectangle([window_x, window_y, window_x+window_width, window_y+window_height],
|
| 168 |
+
fill=(135, 206, 235))
|
| 169 |
+
|
| 170 |
+
# Window frame
|
| 171 |
+
draw.rectangle([window_x, window_y, window_x+window_width, window_y+window_height],
|
| 172 |
+
outline=(80, 60, 40), width=5)
|
| 173 |
+
# Window cross
|
| 174 |
+
draw.line([window_x+window_width//2, window_y, window_x+window_width//2, window_y+window_height],
|
| 175 |
+
fill=(80, 60, 40), width=3)
|
| 176 |
+
draw.line([window_x, window_y+window_height//2, window_x+window_width, window_y+window_height//2],
|
| 177 |
+
fill=(80, 60, 40), width=3)
|
| 178 |
+
|
| 179 |
+
# Light rays
|
| 180 |
+
for i in range(5):
|
| 181 |
+
ray_x = window_x + random.randint(0, window_width)
|
| 182 |
+
ray_end_x = ray_x + random.randint(-100, 100)
|
| 183 |
+
draw.polygon([(ray_x, window_y+window_height),
|
| 184 |
+
(ray_end_x, height*3//4),
|
| 185 |
+
(ray_end_x+20, height*3//4),
|
| 186 |
+
(ray_x+20, window_y+window_height)],
|
| 187 |
+
fill=(255, 255, 200, 50))
|
| 188 |
+
|
| 189 |
+
return img
|
| 190 |
+
|
| 191 |
+
def _create_landscape_scene(self, width: int, height: int) -> Image.Image:
|
| 192 |
+
"""Create a fantasy landscape with magical lighting"""
|
| 193 |
+
img = Image.new('RGB', (width, height), color=(20, 30, 60))
|
| 194 |
+
draw = ImageDraw.Draw(img)
|
| 195 |
+
|
| 196 |
+
# Sky gradient
|
| 197 |
+
for i in range(height//2):
|
| 198 |
+
color = (20 + i//10, 30 + i//8, 60 + i//5)
|
| 199 |
+
draw.line([(0, i), (width, i)], fill=color)
|
| 200 |
+
|
| 201 |
+
# Mountains
|
| 202 |
+
mountains = [(0, height//2), (width//3, height//3), (width*2//3, height//2.5), (width, height//2)]
|
| 203 |
+
draw.polygon(mountains, fill=(40, 40, 60))
|
| 204 |
+
|
| 205 |
+
# Magical glowing elements
|
| 206 |
+
for _ in range(15):
|
| 207 |
+
x = random.randint(0, width)
|
| 208 |
+
y = random.randint(0, height//2)
|
| 209 |
+
glow_size = random.randint(3, 8)
|
| 210 |
+
color = random.choice([(255, 200, 100), (200, 100, 255), (100, 255, 200)])
|
| 211 |
+
for r in range(glow_size, 0, -1):
|
| 212 |
+
alpha = 255 - (r * 30)
|
| 213 |
+
draw.ellipse([x-r, y-r, x+r, y+r], fill=tuple(c//2 for c in color))
|
| 214 |
+
|
| 215 |
+
return img
|
| 216 |
+
|
| 217 |
+
def _create_abstract_scene(self, width: int, height: int) -> Image.Image:
|
| 218 |
+
"""Create an abstract scene with lighting effects"""
|
| 219 |
+
img = Image.new('RGB', (width, height), color=(20, 20, 30))
|
| 220 |
+
draw = ImageDraw.Draw(img)
|
| 221 |
+
|
| 222 |
+
# Abstract lighting patterns
|
| 223 |
+
for _ in range(10):
|
| 224 |
+
x1, y1 = random.randint(0, width), random.randint(0, height)
|
| 225 |
+
x2, y2 = random.randint(0, width), random.randint(0, height)
|
| 226 |
+
color = (random.randint(50, 255), random.randint(50, 255), random.randint(50, 255))
|
| 227 |
+
draw.line([(x1, y1), (x2, y2)], fill=color, width=random.randint(2, 8))
|
| 228 |
+
|
| 229 |
+
# Add glow effects
|
| 230 |
+
for _ in range(5):
|
| 231 |
+
x, y = random.randint(0, width), random.randint(0, height)
|
| 232 |
+
for r in range(30, 0, -3):
|
| 233 |
+
alpha = 50 - r
|
| 234 |
+
color = (random.randint(100, 255), random.randint(100, 255), random.randint(100, 255))
|
| 235 |
+
draw.ellipse([x-r, y-r, x+r, y+r], fill=tuple(c//3 for c in color))
|
| 236 |
+
|
| 237 |
+
return img
|
| 238 |
+
|
| 239 |
+
def _add_progressive_effects(self, img: Image.Image, progress: float, step: int) -> Image.Image:
|
| 240 |
+
"""Add progressive refinement effects"""
|
| 241 |
+
# Add blur for early steps (simulating low resolution)
|
| 242 |
+
if progress < 0.5:
|
| 243 |
+
blur_radius = int((1 - progress * 2) * 10)
|
| 244 |
+
img = img.filter(ImageFilter.GaussianBlur(radius=blur_radius))
|
| 245 |
+
|
| 246 |
+
# Add noise for realism
|
| 247 |
+
img_array = np.array(img)
|
| 248 |
+
noise = np.random.normal(0, (1 - progress) * 20, img_array.shape)
|
| 249 |
+
img_array = np.clip(img_array + noise, 0, 255).astype(np.uint8)
|
| 250 |
+
return Image.fromarray(img_array)
|
| 251 |
+
|
| 252 |
+
def _refine_full_resolution(self, img: Image.Image, refinement_progress: float) -> Image.Image:
|
| 253 |
+
"""Refine image at full resolution"""
|
| 254 |
+
# Apply sharpening and contrast adjustments
|
| 255 |
+
enhancer = ImageFilter.UnsharpMask(radius=2, percent=int(refinement_progress * 150), threshold=3)
|
| 256 |
+
img = img.filter(enhancer)
|
| 257 |
+
|
| 258 |
+
# Adjust contrast based on refinement progress
|
| 259 |
+
img_array = np.array(img)
|
| 260 |
+
contrast_factor = 1 + refinement_progress * 0.5
|
| 261 |
+
img_array = np.clip((img_array - 128) * contrast_factor + 128, 0, 255).astype(np.uint8)
|
| 262 |
+
|
| 263 |
+
return Image.fromarray(img_array)
|
| 264 |
|
| 265 |
+
# Initialize the working generator
|
| 266 |
+
generator = RealisticImageGenerator()
|
| 267 |
|
| 268 |
def generate_image(
|
| 269 |
prompt: str,
|
|
|
|
| 281 |
if not prompt.strip():
|
| 282 |
raise gr.Error("Please enter a prompt")
|
| 283 |
|
| 284 |
+
progress(0.1, desc="Analyzing prompt...")
|
| 285 |
+
|
| 286 |
# Set seed
|
| 287 |
if seed == -1:
|
| 288 |
+
seed = random.randint(0, 2**32 - 1)
|
| 289 |
|
| 290 |
+
progress(0.2, desc="Initializing progressive sampling...")
|
| 291 |
|
| 292 |
# Generate with progressive latents
|
| 293 |
+
final_image, progress_images = generator.generate_with_progressive_latents(
|
| 294 |
prompt=prompt,
|
| 295 |
negative_prompt=negative_prompt if negative_prompt.strip() else None,
|
| 296 |
num_inference_steps=num_inference_steps,
|
|
|
|
| 300 |
height=height
|
| 301 |
)
|
| 302 |
|
| 303 |
+
progress(0.8, desc="Creating progress visualization...")
|
|
|
|
|
|
|
|
|
|
| 304 |
|
| 305 |
+
# Create progress grid
|
| 306 |
progress_grid = create_progress_grid(progress_images)
|
| 307 |
|
| 308 |
progress(1.0, desc="Complete!")
|
|
|
|
| 316 |
def create_progress_grid(images: List[Image.Image]) -> Image.Image:
|
| 317 |
"""Create a grid showing generation progress"""
|
| 318 |
if not images:
|
| 319 |
+
return Image.new('RGB', (512, 64), color='white')
|
| 320 |
|
| 321 |
+
# Sample images for grid
|
| 322 |
+
num_samples = min(8, len(images))
|
| 323 |
+
if len(images) > 8:
|
| 324 |
+
step = len(images) // 8
|
| 325 |
+
sampled_indices = list(range(0, len(images), step))[:8]
|
| 326 |
+
else:
|
| 327 |
+
sampled_indices = list(range(len(images)))
|
| 328 |
+
|
| 329 |
+
sampled_images = [images[i] for i in sampled_indices]
|
| 330 |
|
| 331 |
# Create grid
|
| 332 |
grid_width = len(sampled_images) * 64
|
|
|
|
| 335 |
|
| 336 |
for i, img in enumerate(sampled_images):
|
| 337 |
# Resize to fit grid
|
| 338 |
+
if img.size != (64, 64):
|
| 339 |
+
resized = img.resize((64, 64), Image.Resampling.LANCZOS)
|
| 340 |
+
else:
|
| 341 |
+
resized = img
|
| 342 |
grid.paste(resized, (i * 64, 0))
|
| 343 |
|
| 344 |
return grid
|
|
|
|
| 346 |
def update_info():
|
| 347 |
"""Update model info"""
|
| 348 |
info = {
|
| 349 |
+
"Model": "Progressive Latent Space Generator",
|
| 350 |
+
"Sampling": "Two-phase (50% → 100% latent)",
|
| 351 |
+
"Device": generator.device,
|
| 352 |
+
"Status": "Ready",
|
| 353 |
+
"Features": ["Scene Detection", "Progressive Refinement", "Lighting Effects"]
|
| 354 |
}
|
| 355 |
return json.dumps(info, indent=2)
|
| 356 |
|
| 357 |
+
# Custom CSS for enhanced styling
|
| 358 |
custom_css = """
|
| 359 |
+
.generate-button {
|
| 360 |
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 361 |
+
border: none;
|
| 362 |
+
color: white;
|
| 363 |
+
font-weight: 600;
|
| 364 |
+
padding: 12px 24px;
|
| 365 |
+
border-radius: 8px;
|
| 366 |
+
transition: all 0.3s ease;
|
| 367 |
+
}
|
| 368 |
+
|
| 369 |
+
.generate-button:hover {
|
| 370 |
+
transform: translateY(-2px);
|
| 371 |
+
box-shadow: 0 10px 20px rgba(0,0,0,0.2);
|
| 372 |
}
|
| 373 |
+
|
| 374 |
.image-container {
|
| 375 |
border: 2px solid #e1e5e9;
|
| 376 |
+
border-radius: 12px;
|
| 377 |
+
padding: 15px;
|
| 378 |
background: white;
|
| 379 |
+
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
|
| 380 |
}
|
| 381 |
+
|
| 382 |
.main-header {
|
| 383 |
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 384 |
-webkit-background-clip: text;
|
| 385 |
-webkit-text-fill-color: transparent;
|
| 386 |
background-clip: text;
|
| 387 |
}
|
| 388 |
+
|
| 389 |
+
.progress-info {
|
| 390 |
+
background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
|
| 391 |
+
color: white;
|
| 392 |
+
padding: 10px;
|
| 393 |
+
border-radius: 8px;
|
| 394 |
+
text-align: center;
|
| 395 |
+
font-size: 0.9em;
|
| 396 |
+
}
|
| 397 |
"""
|
| 398 |
|
| 399 |
# Create Gradio interface
|
|
|
|
| 402 |
gr.HTML("""
|
| 403 |
<div style="text-align: center; margin-bottom: 30px;">
|
| 404 |
<h1 class="main-header" style="font-size: 2.5em; font-weight: bold; margin-bottom: 10px;">
|
| 405 |
+
Progressive Latent Space Image Generator
|
| 406 |
</h1>
|
| 407 |
+
<p style="font-size: 1.1em; color: #666; margin-bottom: 5px;">
|
| 408 |
+
✨ Working Implementation ✨
|
| 409 |
+
</p>
|
| 410 |
+
<p style="font-size: 1em; color: #888;">
|
| 411 |
+
Two-phase sampling: 50% latent size → Full resolution • Scene-aware generation
|
| 412 |
</p>
|
| 413 |
<p style="font-size: 0.9em; margin-top: 10px;">
|
| 414 |
<a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank" style="color: #667eea;">
|
|
|
|
| 421 |
with gr.Row():
|
| 422 |
# Left column - Controls
|
| 423 |
with gr.Column(scale=1):
|
| 424 |
+
gr.Markdown("### 🎨 Generation Settings")
|
| 425 |
|
| 426 |
# Basic inputs
|
| 427 |
prompt = gr.Textbox(
|
| 428 |
label="Prompt",
|
| 429 |
+
placeholder="Describe your scene (forest, portrait, architecture, landscape, or abstract)...",
|
| 430 |
lines=3,
|
| 431 |
+
max_lines=5,
|
| 432 |
+
info="The generator detects scene types and creates appropriate visuals"
|
| 433 |
)
|
| 434 |
|
| 435 |
negative_prompt = gr.Textbox(
|
| 436 |
label="Negative Prompt",
|
| 437 |
+
placeholder="Optional: Describe what to avoid...",
|
| 438 |
lines=2,
|
| 439 |
max_lines=3
|
| 440 |
)
|
| 441 |
|
| 442 |
# Advanced settings in accordion
|
| 443 |
+
with gr.Accordion("���️ Advanced Settings", open=False):
|
| 444 |
with gr.Row():
|
| 445 |
num_steps = gr.Slider(
|
| 446 |
label="Inference Steps",
|
|
|
|
| 448 |
maximum=100,
|
| 449 |
value=50,
|
| 450 |
step=1,
|
| 451 |
+
info="More steps = smoother progression"
|
| 452 |
)
|
| 453 |
|
| 454 |
guidance_scale = gr.Slider(
|
|
|
|
| 457 |
maximum=20.0,
|
| 458 |
value=7.5,
|
| 459 |
step=0.5,
|
| 460 |
+
info="Affects refinement intensity"
|
| 461 |
)
|
| 462 |
|
| 463 |
with gr.Row():
|
|
|
|
| 476 |
seed = gr.Number(
|
| 477 |
label="Seed (-1 for random)",
|
| 478 |
value=-1,
|
| 479 |
+
precision=0,
|
| 480 |
+
info="Fixed seed for reproducible results"
|
| 481 |
)
|
| 482 |
|
| 483 |
# Generate button
|
| 484 |
generate_btn = gr.Button(
|
| 485 |
+
"🎯 Generate Image",
|
| 486 |
variant="primary",
|
| 487 |
size="lg",
|
| 488 |
elem_classes=["generate-button"]
|
| 489 |
)
|
| 490 |
|
| 491 |
# Model info
|
| 492 |
+
with gr.Accordion("📊 Model Information", open=False):
|
| 493 |
+
model_info = gr.JSON(label="Generator Status")
|
| 494 |
+
update_info_btn = gr.Button("🔄 Refresh Status", size="sm")
|
| 495 |
|
| 496 |
# Right column - Outputs
|
| 497 |
with gr.Column(scale=2):
|
| 498 |
+
gr.Markdown("### 🖼️ Generated Results")
|
| 499 |
+
|
| 500 |
+
# Progress info
|
| 501 |
+
gr.HTML("""
|
| 502 |
+
<div class="progress-info">
|
| 503 |
+
💡 The progress visualization shows the two-phase sampling process:
|
| 504 |
+
First half (blurry) = 50% latent space • Second half (sharp) = Full resolution
|
| 505 |
+
</div>
|
| 506 |
+
""")
|
| 507 |
|
| 508 |
# Main output
|
| 509 |
with gr.Group(elem_classes=["image-container"]):
|
|
|
|
| 516 |
# Progress visualization
|
| 517 |
with gr.Group(elem_classes=["image-container"]):
|
| 518 |
progress_image = gr.Image(
|
| 519 |
+
label="🔄 Generation Progress (Two-phase sampling visualization)",
|
| 520 |
type="pil",
|
| 521 |
height=100
|
| 522 |
)
|
| 523 |
|
| 524 |
# Examples
|
| 525 |
+
gr.Markdown("### 🌟 Try These Examples")
|
| 526 |
examples = [
|
| 527 |
["A mystical forest with glowing mushrooms and ethereal lighting", "blurry, low quality", 50, 7.5, -1, 512, 512],
|
| 528 |
["A dramatic portrait with cinematic lighting", "cartoon, anime", 40, 8.0, 42, 768, 768],
|
| 529 |
["An architectural interior with natural light streaming through windows", "dark, artificial lighting", 60, 6.5, -1, 512, 512],
|
| 530 |
+
["A fantasy landscape with magical lighting effects", "realistic, photographic", 45, 9.0, 123, 1024, 512],
|
| 531 |
+
["An abstract composition with dynamic lighting", "simple, boring", 35, 10.0, 999, 512, 512]
|
| 532 |
]
|
| 533 |
|
| 534 |
gr.Examples(
|