Spaces:
Runtime error
Runtime error
File size: 10,090 Bytes
7ffe940 2e93998 6477473 7ffe940 6477473 7ffe940 6477473 7ffe940 6477473 7ffe940 6477473 7ffe940 6477473 7ffe940 6477473 7ffe940 6477473 2e93998 6477473 2e93998 6477473 2e93998 6477473 2e93998 6477473 2e93998 6477473 2e93998 6477473 7ffe940 6477473 |
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 |
import gradio as gr
import time
import random
from PIL import Image
import numpy as np
# --- Custom CSS for Styling ---
CUSTOM_CSS = """
:root {
--primary: #6366f1;
--primary-dark: #4f46e5;
--secondary: #8b5cf6;
--accent: #ec4899;
--dark: #1e293b;
--darker: #0f172a;
--light: #f1f5f9;
--lighter: #f8fafc;
--text: #475569;
--border: #e2e8f0;
--success: #10b981;
--warning: #f59e0b;
--error: #ef4444;
--gradient: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
--card-shadow: 0 10px 40px rgba(0, 0, 0, 0.1);
}
/* Apply background to the main Gradio container body */
.gradio-container {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
color: var(--text);
position: relative;
padding: 0 !important;
}
/* Background pattern */
.gradio-container::before {
content: '';
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: url('data:image/svg+xml,%3Csvg width="60" height="60" viewBox="0 0 60 60" xmlns="http://www.w3.org/2000/svg"%3E%3Cg fill="none" fill-rule="evenodd"%3E%3Cg fill="%23ffffff" fill-opacity="0.05"%3E%3Cpath d="M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z"/%3E%3C/g%3E%3C/g%3E%3C/svg%3E');
pointer-events: none;
z-index: 1;
}
#main-wrapper {
max-width: 1400px;
margin: 0 auto;
padding: 2rem;
position: relative;
z-index: 2;
}
#custom-header {
background: rgba(255, 255, 255, 0.95);
backdrop-filter: blur(20px);
border-radius: 20px;
padding: 2rem;
margin-bottom: 2rem;
box-shadow: var(--card-shadow);
animation: slideDown 0.5s ease-out;
}
.header-content {
display: flex;
justify-content: space-between;
align-items: center;
flex-wrap: wrap;
gap: 1rem;
}
.logo {
display: flex;
align-items: center;
gap: 1rem;
}
.logo-icon {
width: 50px;
height: 50px;
background: var(--gradient);
border-radius: 12px;
display: flex;
align-items: center;
justify-content: center;
color: white;
font-weight: bold;
font-size: 1.5rem;
}
#main-title {
font-size: 1.8rem;
background: var(--gradient);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
margin: 0;
padding: 0;
}
.model-badge {
background: linear-gradient(135deg, var(--primary), var(--secondary));
color: white;
padding: 0.5rem 1rem;
border-radius: 20px;
font-size: 0.9rem;
font-weight: 500;
}
#control-panel {
background: rgba(255, 255, 255, 0.95);
backdrop-filter: blur(20px);
border-radius: 20px;
padding: 2rem;
box-shadow: var(--card-shadow);
animation: slideUp 0.5s ease-out;
min-width: 300px;
}
#images-panel {
background: rgba(255, 255, 255, 0.95);
backdrop-filter: blur(20px);
border-radius: 20px;
padding: 2rem;
box-shadow: var(--card-shadow);
animation: slideUp 0.5s ease-out 0.1s both;
}
/* Target Gradio components for styling */
#prompt-box textarea {
min-height: 80px !important;
}
#prompt-box textarea, #negative-prompt-box textarea {
padding: 1rem !important;
border-radius: 12px !important;
font-size: 1rem !important;
}
#prompt-box textarea:focus {
border-color: var(--primary) !important;
box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1) !important;
}
#negative-prompt-box textarea {
background: var(--lighter) !important;
}
#negative-prompt-box textarea:focus {
border-color: var(--error) !important;
box-shadow: 0 0 0 3px rgba(239, 68, 68, 0.1) !important;
}
#generate-btn {
padding: 1rem 2rem;
background: var(--gradient) !important;
color: white !important;
border: none;
border-radius: 12px !important;
font-size: 1.1rem !important;
font-weight: 600 !important;
cursor: pointer;
transition: all 0.3s ease;
position: relative;
overflow: hidden;
min-height: 60px;
}
#generate-btn:hover {
transform: translateY(-2px);
box-shadow: 0 10px 30px rgba(99, 102, 241, 0.3) !important;
}
.panel-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 1.5rem;
}
.panel-title {
font-size: 1.3rem;
font-weight: 600;
color: var(--dark);
}
.image-count {
background: var(--light);
padding: 0.5rem 1rem;
border-radius: 20px;
font-size: 0.9rem;
color: var(--dark);
font-weight: 600;
}
/* Gallery styling */
#imagesGrid .gallery-item {
border-radius: 16px !important;
overflow: hidden;
transition: all 0.3s ease;
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
}
#imagesGrid .gallery-item:hover {
transform: scale(1.02);
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
}
@keyframes slideDown {
from { opacity: 0; transform: translateY(-30px); }
to { opacity: 1; transform: translateY(0); }
}
@keyframes slideUp {
from { opacity: 0; transform: translateY(30px); }
to { opacity: 1; transform: translateY(0); }
}
@media (max-width: 1024px) {
#main-wrapper {
padding: 1rem;
}
.main-content-row {
flex-direction: column;
}
#control-panel {
min-width: unset;
}
}
"""
# HTML for the custom header/logo section
HEADER_HTML = """
<div id="custom-header">
<div class="header-content">
<div class="logo">
<div class="logo-icon">SDXL</div>
<div>
<h1 id="main-title">Stable Diffusion XL Demo</h1>
<p style="color: var(--text); font-size: 0.9rem; margin: 0;">Advanced Image Generation AI</p>
<p style="margin-top: 5px; font-size: 0.9rem;">
Built with <a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank" style="color: var(--primary); text-decoration: none; font-weight: 600;">anycoder</a>
</p>
</div>
</div>
<div class="model-badge">
stabilityai/stable-diffusion-xl-base-1.0
</div>
</div>
</div>
"""
# --- Mock Functionality ---
def generate_dummy_image(seed):
"""Generates a unique, colorful 512x512 PIL image to simulate output."""
np.random.seed(int(seed) % 1000)
size = 512
img_data = np.random.rand(size, size, 3) * 255
# Add some pattern based on the seed
color_shift = np.random.randint(0, 255, 3)
img_data = (img_data + color_shift) % 255
# Simple radial gradient effect
y, x = np.ogrid[-size/2:size/2, -size/2:size/2]
r = np.sqrt(x*x + y*y)
r_max = np.max(r)
r_norm = r / r_max
for i in range(3):
img_data[:, :, i] = img_data[:, :, i] * (1 - r_norm * 0.5) + 255 * r_norm * (i/3)
return Image.fromarray(img_data.astype(np.uint8))
def generate_images(prompt: str, negative_prompt: str, total_count: int):
"""
Simulates SDXL image generation, streaming progress updates.
Yields intermediate states for loading visualization.
"""
if not prompt or prompt.strip() == "":
prompt = "A majestic random scene, vibrant colors, fantasy art, volumetric light"
# 1. Immediate state update: disable button and start progress indicator
yield {
image_count_display: gr.Markdown(value='<span class="image-count">Generating...</span>'),
generate_btn: gr.Button(interactive=False, value="Generating..."),
}
# 2. Simulate generation time with progress streaming
num_steps = 20
for i in range(num_steps):
time.sleep(0.05 + random.random() * 0.05)
progress_text = f"Generating... Step {i+1}/{num_steps} ({(i+1)/num_steps*100:.0f}%)"
yield {
image_count_display: gr.Markdown(value=f'<span class="image-count">{progress_text}</span>')
}
# 3. Final Output Generation (4 images)
images = []
for i in range(4):
seed = time.time() + i + random.random()
img = generate_dummy_image(seed)
caption = f"Prompt: {prompt[:45]}..."
if negative_prompt and negative_prompt.strip():
caption += f" [No: {negative_prompt[:20]}...]"
images.append((img, caption))
new_total = total_count + 4
# 4. Return the final state updates
return {
output_gallery: images,
image_count_display: gr.Markdown(f'<span class="image-count">**{new_total} images** generated in total.</span>'),
total_count: new_total,
generate_btn: gr.Button(interactive=True, value="Generate 4 Images")
}
# --- Gradio Application Definition ---
with gr.Blocks(
title="SDXL Image Generator",
css=CUSTOM_CSS,
theme=gr.themes.Soft(),
fill_height=True
) as demo:
# State to keep track of total generated images
total_count = gr.State(value=0)
# Custom Header (containing the required attribution)
gr.HTML(HEADER_HTML)
with gr.Row(elem_id="main-wrapper", elem_classes=["main-content-row"]):
# --- Control Panel (Left Column) ---
with gr.Column(scale=1, elem_id="control-panel"):
gr.Markdown("## ⚙️ Generation Settings", container=False)
# Prompt Input
prompt_input = gr.Textbox(
label="Prompt", |