Spaces:
Running
on
Zero
Running
on
Zero
File size: 20,371 Bytes
045e583 3e75e0e 2fc9c57 71e00ae 82c44f1 71e00ae 82c44f1 3e75e0e 2fc9c57 3e75e0e 2fc9c57 3e75e0e 2fc9c57 3e75e0e 2fc9c57 3e75e0e 2fc9c57 3e75e0e 2fc9c57 3739851 2fc9c57 3739851 71e00ae 3739851 71e00ae 3739851 28d25b7 3739851 71e00ae 3739851 71e00ae 82c44f1 71e00ae 3739851 71e00ae 3739851 71e00ae 3739851 71e00ae 3739851 71e00ae 82c44f1 71e00ae 82c44f1 71e00ae 3739851 71e00ae 82c44f1 71e00ae 3739851 71e00ae 3739851 |
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 |
import os
import subprocess
import sys
import importlib.util
import time
# Set environment variables
os.environ["TRANSFORMERS_NO_ADVISORY_WARNINGS"] = "1"
os.environ["TRANSFORMERS_COMPILER_DISABLED"] = "1"
# Function to install required packages
def install_required_packages():
required_packages = [
"warmup_scheduler",
"torchtools"
]
github_repos = {
"warmup_scheduler": "git+https://github.com/ildoonet/pytorch-gradual-warmup-lr.git",
"torchtools": "git+https://github.com/pabloppp/pytorch-tools.git"
}
missing_packages = []
# First check which packages need to be installed
for package in required_packages:
if importlib.util.find_spec(package) is None:
missing_packages.append(package)
print(f"{package} needs to be installed")
# Install missing packages
for package in missing_packages:
print(f"Installing {package}...")
try:
if package in github_repos:
subprocess.check_call([
sys.executable, "-m", "pip", "install", github_repos[package]
])
else:
subprocess.check_call([
sys.executable, "-m", "pip", "install", package
])
print(f"{package} installed successfully")
# Wait a moment to ensure the package is available for import
time.sleep(1)
except subprocess.CalledProcessError as e:
print(f"Failed to install {package}: {e}")
# If there were any packages installed, try to force a refresh of sys.modules
if missing_packages:
print("Refreshing Python module cache...")
for package in missing_packages:
if package in sys.modules:
del sys.modules[package]
# Create patches for missing modules if they can't be installed
def create_module_patches():
# Create a patch for torchtools.transforms if it doesn't exist
if importlib.util.find_spec("torchtools") is None or importlib.util.find_spec("torchtools.transforms") is None:
print("Creating patch for torchtools.transforms...")
# Create the directory structure
os.makedirs("torchtools/transforms", exist_ok=True)
# Create __init__.py files
with open("torchtools/__init__.py", "w") as f:
f.write("# Patch for torchtools\n")
# Create a simplified SmartCrop class
with open("torchtools/transforms/__init__.py", "w") as f:
f.write("""# Patch for torchtools.transforms
import torch
import torch.nn.functional as F
class SmartCrop:
def __init__(self, size=None, scale=None, preserve_aspect_ratio=True):
self.size = size
self.scale = scale
self.preserve_aspect_ratio = preserve_aspect_ratio
def __call__(self, image):
# Basic placeholder implementation that resizes the image
# For actual smart cropping, a more complex implementation would be needed
if self.size is not None:
return F.interpolate(image.unsqueeze(0), size=self.size, mode='bilinear', align_corners=False).squeeze(0)
elif self.scale is not None:
h, w = image.shape[-2:]
new_h, new_w = int(h * self.scale), int(w * self.scale)
return F.interpolate(image.unsqueeze(0), size=(new_h, new_w), mode='bilinear', align_corners=False).squeeze(0)
return image
""")
# Add the patch directory to the system path
sys.path.insert(0, os.path.abspath('./'))
print("Torchtools patch created successfully")
# Install required packages
print("Checking and installing required packages...")
install_required_packages()
# Create patch modules for any missing dependencies
print("Creating patches for any missing modules...")
create_module_patches()
# Give a moment for the system to register newly installed packages
time.sleep(2)
# Now continue with the imports
print("Importing the required modules...")
import yaml
import torch
sys.path.append(os.path.abspath('./'))
# Try importing the modules
try:
from inference.utils import *
from train import WurstCoreB
from gdf import DDPMSampler
from train import WurstCore_t2i as WurstCoreC
print("Successfully imported all required modules!")
except ImportError as e:
print(f"Warning: Import error: {e}")
print("Continuing with the application setup...")
import numpy as np
import random
import argparse
import gradio as gr
import spaces
from huggingface_hub import hf_hub_url
from huggingface_hub import hf_hub_download
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--height', type=int, default=2560, help='image height')
parser.add_argument('--width', type=int, default=5120, help='image width')
parser.add_argument('--seed', type=int, default=123, help='random seed')
parser.add_argument('--dtype', type=str, default='bf16', help='if bf16 does not work, change it to float32')
parser.add_argument('--config_c', type=str,
default='configs/training/t2i.yaml', help='config file for stage c, latent generation')
parser.add_argument('--config_b', type=str,
default='configs/inference/stage_b_1b.yaml', help='config file for stage b, latent decoding')
parser.add_argument('--prompt', type=str,
default='A photo-realistic image of a west highland white terrier in the garden, high quality, detail rich, 8K', help='text prompt')
parser.add_argument('--num_image', type=int, default=1, help='how many images generated')
parser.add_argument('--output_dir', type=str, default='figures/output_results/', help='output directory for generated image')
parser.add_argument('--stage_a_tiled', action='store_true', help='whether or not to use tiled decoding for stage a to save memory')
parser.add_argument('--pretrained_path', type=str, default='models/ultrapixel_t2i.safetensors', help='pretrained path of newly added parameter of UltraPixel')
args = parser.parse_args()
return args
def clear_image():
return None
def load_message(height, width, seed, prompt, args, stage_a_tiled):
args.height = height
args.width = width
args.seed = seed
args.prompt = prompt + ' rich detail, 4k, high quality'
args.stage_a_tiled = stage_a_tiled
return args
@spaces.GPU(duration=120)
def get_image(height, width, seed, prompt, cfg, timesteps, stage_a_tiled):
global args
args = load_message(height, width, seed, prompt, args, stage_a_tiled)
torch.manual_seed(args.seed)
random.seed(args.seed)
np.random.seed(args.seed)
dtype = torch.bfloat16 if args.dtype == 'bf16' else torch.float
captions = [args.prompt] * args.num_image
height, width = args.height, args.width
batch_size = 1
height_lr, width_lr = get_target_lr_size(height / width, std_size=32)
stage_c_latent_shape, stage_b_latent_shape = calculate_latent_sizes(height, width, batch_size=batch_size)
stage_c_latent_shape_lr, stage_b_latent_shape_lr = calculate_latent_sizes(height_lr, width_lr, batch_size=batch_size)
# Stage C Parameters
extras.sampling_configs['cfg'] = 4
extras.sampling_configs['shift'] = 1
extras.sampling_configs['timesteps'] = 20
extras.sampling_configs['t_start'] = 1.0
extras.sampling_configs['sampler'] = DDPMSampler(extras.gdf)
# Stage B Parameters
extras_b.sampling_configs['cfg'] = 1.1
extras_b.sampling_configs['shift'] = 1
extras_b.sampling_configs['timesteps'] = 10
extras_b.sampling_configs['t_start'] = 1.0
for _, caption in enumerate(captions):
batch = {'captions': [caption] * batch_size}
conditions_b = core_b.get_conditions(batch, models_b, extras_b, is_eval=True, is_unconditional=False)
unconditions_b = core_b.get_conditions(batch, models_b, extras_b, is_eval=True, is_unconditional=True)
with torch.no_grad():
models.generator.cuda()
print('STAGE C GENERATION***************************')
with torch.cuda.amp.autocast(dtype=dtype):
sampled_c = generation_c(batch, models, extras, core, stage_c_latent_shape, stage_c_latent_shape_lr, device)
models.generator.cpu()
torch.cuda.empty_cache()
conditions_b = core_b.get_conditions(batch, models_b, extras_b, is_eval=True, is_unconditional=False)
unconditions_b = core_b.get_conditions(batch, models_b, extras_b, is_eval=True, is_unconditional=True)
conditions_b['effnet'] = sampled_c
unconditions_b['effnet'] = torch.zeros_like(sampled_c)
print('STAGE B + A DECODING***************************')
with torch.cuda.amp.autocast(dtype=dtype):
sampled = decode_b(conditions_b, unconditions_b, models_b, stage_b_latent_shape, extras_b, device, stage_a_tiled=args.stage_a_tiled)
torch.cuda.empty_cache()
imgs = show_images(sampled)
return imgs[0]
css = """
footer {
visibility: hidden;
}
/* Main container styling */
#col-container {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
background-color: #f8f9fa;
border-radius: 15px;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
}
/* Header styling */
h1 {
text-align: center;
color: #ff6b00;
font-size: 2.5rem;
margin-bottom: 20px;
font-weight: 700;
text-shadow: 1px 1px 2px rgba(0,0,0,0.1);
}
/* Button styling */
button.primary {
background-color: #ff6b00 !important;
color: white !important;
border: none !important;
border-radius: 8px !important;
padding: 10px 20px !important;
font-weight: 600 !important;
transition: all 0.3s ease !important;
}
button.primary:hover {
background-color: #e55f00 !important;
transform: translateY(-2px);
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2) !important;
}
/* Input field styling */
input[type="text"] {
border-radius: 8px !important;
border: 2px solid #ddd !important;
padding: 12px !important;
font-size: 1rem !important;
transition: all 0.3s ease !important;
}
input[type="text"]:focus {
border-color: #ff6b00 !important;
box-shadow: 0 0 0 3px rgba(255, 107, 0, 0.2) !important;
}
/* Output image container */
.output-image {
border-radius: 12px;
overflow: hidden;
box-shadow: 0 8px 20px rgba(0, 0, 0, 0.15);
margin: 20px 0;
transition: all 0.3s ease;
}
.output-image:hover {
transform: scale(1.02);
}
/* Accordion styling */
.accordion {
border-radius: 10px !important;
overflow: hidden !important;
margin: 15px 0 !important;
border: 1px solid #eaeaea !important;
}
/* Example gallery */
.examples-gallery {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
gap: 15px;
margin-top: 20px;
}
.example-item {
background-color: white;
border-radius: 10px;
padding: 10px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
transition: all 0.3s ease;
}
.example-item:hover {
transform: translateY(-5px);
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
}
/* Loading animation */
@keyframes pulse {
0% { opacity: 0.6; }
50% { opacity: 1; }
100% { opacity: 0.6; }
}
.loading {
animation: pulse 1.5s infinite;
text-align: center;
padding: 20px;
color: #ff6b00;
font-weight: bold;
}
"""
with gr.Blocks(theme="soft", css=css) as demo:
with gr.Column(elem_id="col-container"):
gr.Markdown("<h1>UHD Image Generator (5120×4096)</h1>")
with gr.Group():
with gr.Row():
with gr.Column(scale=5):
prompt = gr.Textbox(
label="Text Prompt",
max_lines=2,
placeholder="Enter your prompt (e.g., 'A majestic mountain landscape with snow')",
elem_id="prompt-input"
)
with gr.Column(scale=1):
generate_button = gr.Button("Generate Image", variant="primary", elem_id="generate-btn")
clear_button = gr.Button("Clear", elem_id="clear-btn")
# Loading indicator
with gr.Row(visible=False) as loading_indicator:
gr.Markdown('<div class="loading">Generating your ultra high resolution image... This may take a minute...</div>')
# Output image with nicer styling
output_img = gr.Image(label="Generated Image", elem_classes="output-image")
with gr.Accordion("Advanced Settings", open=False):
with gr.Row():
with gr.Column():
seed = gr.Number(
label="Random Seed",
value=123,
step=1,
minimum=0,
)
cfg = gr.Slider(
label="CFG Scale (Creativity vs. Prompt Adherence)",
minimum=3,
maximum=10,
step=0.1,
value=4
)
with gr.Column():
with gr.Row():
width = gr.Slider(
label="Width",
minimum=1536,
maximum=5120,
step=32,
value=4096
)
height = gr.Slider(
label="Height",
minimum=1536,
maximum=4096,
step=32,
value=2304
)
timesteps = gr.Slider(
label="Timesteps (Quality vs. Speed)",
minimum=10,
maximum=50,
step=1,
value=20
)
stage_a_tiled = gr.Checkbox(
label="Use Tiled Decoding (Lower Memory Usage)",
value=False
)
# Aspect ratio presets
with gr.Row():
gr.Markdown("### Quick Aspect Ratio Presets")
with gr.Row():
preset_landscape = gr.Button("Landscape (16:9)", size="sm")
preset_portrait = gr.Button("Portrait (9:16)", size="sm")
preset_square = gr.Button("Square (1:1)", size="sm")
preset_ultrawide = gr.Button("Ultrawide (21:9)", size="sm")
# Examples with better organization
gr.Markdown("### Example Prompts")
with gr.Row():
example_tabs = gr.Tabs([
gr.TabItem("Nature", gr.Examples(
examples=[
"A detailed view of a blooming magnolia tree, with large, white flowers and dark green leaves, set against a clear blue sky.",
"A majestic view of snow-covered mountains with a calm lake against a blue sky background",
],
inputs=[prompt],
outputs=[output_img],
)),
gr.TabItem("Animals", gr.Examples(
examples=[
"A crocodile wearing a sweater",
"A cute golden retriever puppy chasing a red ball on a green lawn",
],
inputs=[prompt],
outputs=[output_img],
)),
gr.TabItem("Anime", gr.Examples(
examples=[
"A vibrant anime scene of a young girl with long, flowing pink hair, big sparkling blue eyes, and a school uniform, standing under a cherry blossom tree with petals falling around her.",
],
inputs=[prompt],
outputs=[output_img],
)),
gr.TabItem("Architecture", gr.Examples(
examples=[
"A cozy, rustic log cabin nestled in a snow-covered forest, with smoke rising from the stone chimney, warm lights glowing from the windows, and a path of footprints leading to the front door.",
],
inputs=[prompt],
outputs=[output_img],
)),
])
# Function to set aspect ratio presets
def set_landscape():
return 5120, 2880
def set_portrait():
return 2880, 4096
def set_square():
return 3584, 3584
def set_ultrawide():
return 5120, 2160
# Connect buttons to functions
preset_landscape.click(set_landscape, outputs=[width, height])
preset_portrait.click(set_portrait, outputs=[width, height])
preset_square.click(set_square, outputs=[width, height])
preset_ultrawide.click(set_ultrawide, outputs=[width, height])
# Connect events
generate_button.click(
lambda: gr.update(visible=True),
outputs=[loading_indicator]
).then(
get_image,
inputs=[height, width, seed, prompt, cfg, timesteps, stage_a_tiled],
outputs=[output_img]
).then(
lambda: gr.update(visible=False),
outputs=[loading_indicator]
)
clear_button.click(clear_image, inputs=[], outputs=[output_img])
def download_with_wget(url, save_path):
try:
subprocess.run(['wget', url, '-O', save_path], check=True)
print(f"Downloaded to {save_path}")
except subprocess.CalledProcessError as e:
print(f"Error downloading file: {e}")
def download_model():
urls = [
'https://huggingface.co/stabilityai/StableWurst/resolve/main/stage_a.safetensors',
'https://huggingface.co/stabilityai/StableWurst/resolve/main/previewer.safetensors',
'https://huggingface.co/stabilityai/StableWurst/resolve/main/effnet_encoder.safetensors',
'https://huggingface.co/stabilityai/StableWurst/resolve/main/stage_b_lite_bf16.safetensors',
'https://huggingface.co/stabilityai/StableWurst/resolve/main/stage_c_bf16.safetensors',
]
for file_url in urls:
hf_hub_download(repo_id="stabilityai/stable-cascade", filename=file_url.split('/')[-1], local_dir='models')
hf_hub_download(repo_id="roubaofeipi/UltraPixel", filename='ultrapixel_t2i.safetensors', local_dir='models')
if __name__ == "__main__":
args = parse_args()
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
download_model()
config_file = args.config_c
with open(config_file, "r", encoding="utf-8") as file:
loaded_config = yaml.safe_load(file)
core = WurstCoreC(config_dict=loaded_config, device=device, training=False)
# SETUP STAGE B
config_file_b = args.config_b
with open(config_file_b, "r", encoding="utf-8") as file:
config_file_b = yaml.safe_load(file)
core_b = WurstCoreB(config_dict=config_file_b, device=device, training=False)
extras = core.setup_extras_pre()
models = core.setup_models(extras)
models.generator.eval().requires_grad_(False)
print("STAGE C READY")
extras_b = core_b.setup_extras_pre()
models_b = core_b.setup_models(extras_b, skip_clip=True)
models_b = WurstCoreB.Models(
**{**models_b.to_dict(), 'tokenizer': models.tokenizer, 'text_model': models.text_model}
)
models_b.generator.bfloat16().eval().requires_grad_(False)
print("STAGE B READY")
pretrained_path = args.pretrained_path
sdd = torch.load(pretrained_path, map_location='cpu')
collect_sd = {}
for k, v in sdd.items():
collect_sd[k[7:]] = v
models.train_norm.load_state_dict(collect_sd)
models.generator.eval()
models.train_norm.eval()
demo.launch(debug=True, share=True) |