Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,31 +1,21 @@
|
|
| 1 |
import spaces
|
| 2 |
-
import os
|
| 3 |
import gradio as gr
|
| 4 |
import torch
|
| 5 |
from PIL import Image
|
| 6 |
-
from diffusers import DiffusionPipeline
|
| 7 |
import random
|
| 8 |
import uuid
|
| 9 |
-
from typing import Tuple
|
| 10 |
import numpy as np
|
| 11 |
import time
|
| 12 |
import zipfile
|
| 13 |
-
from transformers import CLIPTextModel, CLIPTokenizer, T5EncoderModel, T5TokenizerFast
|
| 14 |
|
| 15 |
-
# ---
|
| 16 |
-
|
| 17 |
-
print("torch.__version__ =", torch.__version__)
|
| 18 |
-
print("torch.version.cuda =", torch.version.cuda)
|
| 19 |
-
print("cuda available:", torch.cuda.is_available())
|
| 20 |
-
print("cuda device count:", torch.cuda.device_count())
|
| 21 |
-
if torch.cuda.is_available():
|
| 22 |
-
print("current device:", torch.cuda.current_device())
|
| 23 |
-
print("device name:", torch.cuda.get_device_name(torch.cuda.current_device()))
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
|
| 28 |
-
# Helper functions
|
| 29 |
def save_image(img):
|
| 30 |
unique_name = str(uuid.uuid4()) + ".png"
|
| 31 |
img.save(unique_name)
|
|
@@ -37,190 +27,55 @@ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
|
| 37 |
return seed
|
| 38 |
|
| 39 |
MAX_SEED = np.iinfo(np.int32).max
|
| 40 |
-
MAX_IMAGE_SIZE = 2048
|
| 41 |
|
| 42 |
-
#
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
| 46 |
lora_repo = "strangerzonehf/Flux-Super-Realism-LoRA"
|
| 47 |
trigger_word = "Super Realism"
|
| 48 |
-
pipe_dev.load_lora_weights(lora_repo)
|
| 49 |
-
pipe_dev.to("cuda")
|
| 50 |
-
|
| 51 |
-
# Flux.1-krea
|
| 52 |
-
dtype = torch.bfloat16
|
| 53 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 54 |
-
|
| 55 |
-
# --- Model Loading ---
|
| 56 |
-
taef1 = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=dtype).to(device)
|
| 57 |
-
good_vae = AutoencoderKL.from_pretrained("black-forest-labs/FLUX.1-Krea-dev", subfolder="vae", torch_dtype=dtype).to(device)
|
| 58 |
-
pipe_krea = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-Krea-dev", torch_dtype=dtype, vae=taef1).to(device)
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
def flux_pipe_call_that_returns_an_iterable_of_images(
|
| 63 |
-
self,
|
| 64 |
-
prompt: Union[str, List[str]] = None,
|
| 65 |
-
prompt_2: Optional[Union[str, List[str]]] = None,
|
| 66 |
-
height: Optional[int] = None,
|
| 67 |
-
width: Optional[int] = None,
|
| 68 |
-
num_inference_steps: int = 28,
|
| 69 |
-
timesteps: List[int] = None,
|
| 70 |
-
guidance_scale: float = 3.5,
|
| 71 |
-
num_images_per_prompt: Optional[int] = 1,
|
| 72 |
-
generator: Optional[Union[torch.Generator, List[torch.Generator]]] = None,
|
| 73 |
-
latents: Optional[torch.FloatTensor] = None,
|
| 74 |
-
prompt_embeds: Optional[torch.FloatTensor] = None,
|
| 75 |
-
pooled_prompt_embeds: Optional[torch.FloatTensor] = None,
|
| 76 |
-
output_type: Optional[str] = "pil",
|
| 77 |
-
return_dict: bool = True,
|
| 78 |
-
joint_attention_kwargs: Optional[Dict[str, Any]] = None,
|
| 79 |
-
max_sequence_length: int = 512,
|
| 80 |
-
good_vae: Optional[Any] = None,
|
| 81 |
-
):
|
| 82 |
-
height = height or self.default_sample_size * self.vae_scale_factor
|
| 83 |
-
width = width or self.default_sample_size * self.vae_scale_factor
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
|
|
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
|
| 99 |
-
batch_size = 1 if isinstance(prompt, str) else len(prompt)
|
| 100 |
-
device = self._execution_device
|
| 101 |
|
| 102 |
-
lora_scale = joint_attention_kwargs.get("scale", None) if joint_attention_kwargs is not None else None
|
| 103 |
-
prompt_embeds, pooled_prompt_embeds, text_ids = self.encode_prompt(
|
| 104 |
-
prompt=prompt,
|
| 105 |
-
prompt_2=prompt_2,
|
| 106 |
-
prompt_embeds=prompt_embeds,
|
| 107 |
-
pooled_prompt_embeds=pooled_prompt_embeds,
|
| 108 |
-
device=device,
|
| 109 |
-
num_images_per_prompt=num_images_per_prompt,
|
| 110 |
-
max_sequence_length=max_sequence_length,
|
| 111 |
-
lora_scale=lora_scale,
|
| 112 |
-
)
|
| 113 |
-
|
| 114 |
-
num_channels_latents = self.transformer.config.in_channels // 4
|
| 115 |
-
latents, latent_image_ids = self.prepare_latents(
|
| 116 |
-
batch_size * num_images_per_prompt,
|
| 117 |
-
num_channels_latents,
|
| 118 |
-
height,
|
| 119 |
-
width,
|
| 120 |
-
prompt_embeds.dtype,
|
| 121 |
-
device,
|
| 122 |
-
generator,
|
| 123 |
-
latents,
|
| 124 |
-
)
|
| 125 |
-
|
| 126 |
-
sigmas = np.linspace(1.0, 1 / num_inference_steps, num_inference_steps)
|
| 127 |
-
image_seq_len = latents.shape[1]
|
| 128 |
-
mu = calculate_shift(
|
| 129 |
-
image_seq_len,
|
| 130 |
-
self.scheduler.config.base_image_seq_len,
|
| 131 |
-
self.scheduler.config.max_image_seq_len,
|
| 132 |
-
self.scheduler.config.base_shift,
|
| 133 |
-
self.scheduler.config.max_shift,
|
| 134 |
-
)
|
| 135 |
-
timesteps, num_inference_steps = retrieve_timesteps(
|
| 136 |
-
self.scheduler,
|
| 137 |
-
num_inference_steps,
|
| 138 |
-
device,
|
| 139 |
-
timesteps,
|
| 140 |
-
sigmas,
|
| 141 |
-
mu=mu,
|
| 142 |
-
)
|
| 143 |
-
self._num_timesteps = len(timesteps)
|
| 144 |
-
|
| 145 |
-
guidance = torch.full([1], guidance_scale, device=device, dtype=torch.float32).expand(latents.shape[0]) if self.transformer.config.guidance_embeds else None
|
| 146 |
-
|
| 147 |
-
for i, t in enumerate(timesteps):
|
| 148 |
-
if self.interrupt:
|
| 149 |
-
continue
|
| 150 |
-
|
| 151 |
-
timestep = t.expand(latents.shape[0]).to(latents.dtype)
|
| 152 |
-
|
| 153 |
-
noise_pred = self.transformer(
|
| 154 |
-
hidden_states=latents,
|
| 155 |
-
timestep=timestep / 1000,
|
| 156 |
-
guidance=guidance,
|
| 157 |
-
pooled_projections=pooled_prompt_embeds,
|
| 158 |
-
encoder_hidden_states=prompt_embeds,
|
| 159 |
-
txt_ids=text_ids,
|
| 160 |
-
img_ids=latent_image_ids,
|
| 161 |
-
joint_attention_kwargs=self.joint_attention_kwargs,
|
| 162 |
-
return_dict=False,
|
| 163 |
-
)[0]
|
| 164 |
-
|
| 165 |
-
latents_for_image = self._unpack_latents(latents, height, width, self.vae_scale_factor)
|
| 166 |
-
latents_for_image = (latents_for_image / self.vae.config.scaling_factor) + self.vae.config.shift_factor
|
| 167 |
-
image = self.vae.decode(latents_for_image, return_dict=False)[0]
|
| 168 |
-
yield self.image_processor.postprocess(image, output_type=output_type)[0]
|
| 169 |
-
|
| 170 |
-
latents = self.scheduler.step(noise_pred, t, latents, return_dict=False)[0]
|
| 171 |
-
torch.cuda.empty_cache()
|
| 172 |
-
|
| 173 |
-
latents = self._unpack_latents(latents, height, width, self.vae_scale_factor)
|
| 174 |
-
latents = (latents / good_vae.config.scaling_factor) + good_vae.config.shift_factor
|
| 175 |
-
image = good_vae.decode(latents, return_dict=False)[0]
|
| 176 |
-
self.maybe_free_model_hooks()
|
| 177 |
-
torch.cuda.empty_cache()
|
| 178 |
-
yield self.image_processor.postprocess(image, output_type=output_type)[0]
|
| 179 |
-
|
| 180 |
-
pipe_krea.flux_pipe_call_that_returns_an_iterable_of_images = flux_pipe_call_that_returns_an_iterable_of_images.__get__(pipe_krea)
|
| 181 |
-
|
| 182 |
-
# Helper functions for flux.1-krea
|
| 183 |
-
def calculate_shift(
|
| 184 |
-
image_seq_len,
|
| 185 |
-
base_seq_len: int = 256,
|
| 186 |
-
max_seq_len: int = 4096,
|
| 187 |
-
base_shift: float = 0.5,
|
| 188 |
-
max_shift: float = 1.16,
|
| 189 |
-
):
|
| 190 |
-
m = (max_shift - base_shift) / (max_seq_len - base_seq_len)
|
| 191 |
-
b = base_shift - m * base_seq_len
|
| 192 |
-
mu = image_seq_len * m + b
|
| 193 |
-
return mu
|
| 194 |
-
|
| 195 |
-
def retrieve_timesteps(
|
| 196 |
-
scheduler,
|
| 197 |
-
num_inference_steps: Optional[int] = None,
|
| 198 |
-
device: Optional[Union[str, torch.device]] = None,
|
| 199 |
-
timesteps: Optional[List[int]] = None,
|
| 200 |
-
sigmas: Optional[List[float]] = None,
|
| 201 |
-
**kwargs,
|
| 202 |
-
):
|
| 203 |
-
if timesteps is not None and sigmas is not None:
|
| 204 |
-
raise ValueError("Only one of `timesteps` or `sigmas` can be passed.")
|
| 205 |
-
if timesteps is not None:
|
| 206 |
-
scheduler.set_timesteps(timesteps=timesteps, device=device, **kwargs)
|
| 207 |
-
timesteps = scheduler.timesteps
|
| 208 |
-
num_inference_steps = len(timesteps)
|
| 209 |
-
elif sigmas is not None:
|
| 210 |
-
scheduler.set_timesteps(sigmas=sigmas, device=device, **kwargs)
|
| 211 |
-
timesteps = scheduler.timesteps
|
| 212 |
-
num_inference_steps = len(timesteps)
|
| 213 |
-
else:
|
| 214 |
-
scheduler.set_timesteps(num_inference_steps, device=device, **kwargs)
|
| 215 |
-
timesteps = scheduler.timesteps
|
| 216 |
-
return timesteps, num_inference_steps
|
| 217 |
-
|
| 218 |
-
# Styles for flux.1-dev-realism
|
| 219 |
style_list = [
|
| 220 |
-
{
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
]
|
| 225 |
|
| 226 |
styles = {k["name"]: (k["prompt"], k["negative_prompt"]) for k in style_list}
|
|
@@ -231,9 +86,8 @@ def apply_style(style_name: str, positive: str) -> Tuple[str, str]:
|
|
| 231 |
p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
|
| 232 |
return p.replace("{prompt}", positive), n
|
| 233 |
|
| 234 |
-
# Generation function for flux.1-dev-realism
|
| 235 |
@spaces.GPU
|
| 236 |
-
def
|
| 237 |
prompt: str,
|
| 238 |
negative_prompt: str = "",
|
| 239 |
use_negative_prompt: bool = False,
|
|
@@ -243,7 +97,7 @@ def generate_dev(
|
|
| 243 |
guidance_scale: float = 3,
|
| 244 |
randomize_seed: bool = False,
|
| 245 |
style_name: str = DEFAULT_STYLE_NAME,
|
| 246 |
-
num_inference_steps: int =
|
| 247 |
num_images: int = 1,
|
| 248 |
zip_images: bool = False,
|
| 249 |
progress=gr.Progress(track_tqdm=True),
|
|
@@ -265,7 +119,8 @@ def generate_dev(
|
|
| 265 |
|
| 266 |
start_time = time.time()
|
| 267 |
|
| 268 |
-
|
|
|
|
| 269 |
prompt=positive_prompt,
|
| 270 |
negative_prompt=final_negative_prompt if final_negative_prompt else None,
|
| 271 |
width=width,
|
|
@@ -292,111 +147,11 @@ def generate_dev(
|
|
| 292 |
|
| 293 |
return image_paths, seed, f"{duration:.2f}", zip_path
|
| 294 |
|
| 295 |
-
# Generation function for flux.1-krea
|
| 296 |
-
@spaces.GPU
|
| 297 |
-
def generate_krea(
|
| 298 |
-
prompt: str,
|
| 299 |
-
seed: int = 0,
|
| 300 |
-
width: int = 1024,
|
| 301 |
-
height: int = 1024,
|
| 302 |
-
guidance_scale: float = 4.5,
|
| 303 |
-
randomize_seed: bool = False,
|
| 304 |
-
num_inference_steps: int = 28,
|
| 305 |
-
num_images: int = 1,
|
| 306 |
-
zip_images: bool = False,
|
| 307 |
-
progress=gr.Progress(track_tqdm=True),
|
| 308 |
-
):
|
| 309 |
-
if randomize_seed:
|
| 310 |
-
seed = random.randint(0, MAX_SEED)
|
| 311 |
-
generator = torch.Generator().manual_seed(seed)
|
| 312 |
-
|
| 313 |
-
start_time = time.time()
|
| 314 |
-
|
| 315 |
-
images = []
|
| 316 |
-
for _ in range(num_images):
|
| 317 |
-
final_img = list(pipe_krea.flux_pipe_call_that_returns_an_iterable_of_images(
|
| 318 |
-
prompt=prompt,
|
| 319 |
-
guidance_scale=guidance_scale,
|
| 320 |
-
num_inference_steps=num_inference_steps,
|
| 321 |
-
width=width,
|
| 322 |
-
height=height,
|
| 323 |
-
generator=generator,
|
| 324 |
-
output_type="pil",
|
| 325 |
-
good_vae=good_vae,
|
| 326 |
-
))[-1] # Take the final image only
|
| 327 |
-
images.append(final_img)
|
| 328 |
-
|
| 329 |
-
end_time = time.time()
|
| 330 |
-
duration = end_time - start_time
|
| 331 |
-
|
| 332 |
-
image_paths = [save_image(img) for img in images]
|
| 333 |
-
|
| 334 |
-
zip_path = None
|
| 335 |
-
if zip_images:
|
| 336 |
-
zip_name = str(uuid.uuid4()) + ".zip"
|
| 337 |
-
with zipfile.ZipFile(zip_name, 'w') as zipf:
|
| 338 |
-
for i, img_path in enumerate(image_paths):
|
| 339 |
-
zipf.write(img_path, arcname=f"Img_{i}.png")
|
| 340 |
-
zip_path = zip_name
|
| 341 |
-
|
| 342 |
-
return image_paths, seed, f"{duration:.2f}", zip_path
|
| 343 |
-
|
| 344 |
-
# Main generation function to handle model choice
|
| 345 |
-
@spaces.GPU
|
| 346 |
-
def generate(
|
| 347 |
-
model_choice: str,
|
| 348 |
-
prompt: str,
|
| 349 |
-
negative_prompt: str = "",
|
| 350 |
-
use_negative_prompt: bool = False,
|
| 351 |
-
seed: int = 0,
|
| 352 |
-
width: int = 1024,
|
| 353 |
-
height: int = 1024,
|
| 354 |
-
guidance_scale: float = 3,
|
| 355 |
-
randomize_seed: bool = False,
|
| 356 |
-
style_name: str = DEFAULT_STYLE_NAME,
|
| 357 |
-
num_inference_steps: int = 30,
|
| 358 |
-
num_images: int = 1,
|
| 359 |
-
zip_images: bool = False,
|
| 360 |
-
progress=gr.Progress(track_tqdm=True),
|
| 361 |
-
):
|
| 362 |
-
if model_choice == "flux.1-dev-realism":
|
| 363 |
-
return generate_dev(
|
| 364 |
-
prompt=prompt,
|
| 365 |
-
negative_prompt=negative_prompt,
|
| 366 |
-
use_negative_prompt=use_negative_prompt,
|
| 367 |
-
seed=seed,
|
| 368 |
-
width=width,
|
| 369 |
-
height=height,
|
| 370 |
-
guidance_scale=guidance_scale,
|
| 371 |
-
randomize_seed=randomize_seed,
|
| 372 |
-
style_name=style_name,
|
| 373 |
-
num_inference_steps=num_inference_steps,
|
| 374 |
-
num_images=num_images,
|
| 375 |
-
zip_images=zip_images,
|
| 376 |
-
progress=progress,
|
| 377 |
-
)
|
| 378 |
-
elif model_choice == "flux.1-krea-dev":
|
| 379 |
-
return generate_krea(
|
| 380 |
-
prompt=prompt,
|
| 381 |
-
seed=seed,
|
| 382 |
-
width=width,
|
| 383 |
-
height=height,
|
| 384 |
-
guidance_scale=guidance_scale,
|
| 385 |
-
randomize_seed=randomize_seed,
|
| 386 |
-
num_inference_steps=num_inference_steps,
|
| 387 |
-
num_images=num_images,
|
| 388 |
-
zip_images=zip_images,
|
| 389 |
-
progress=progress,
|
| 390 |
-
)
|
| 391 |
-
else:
|
| 392 |
-
raise ValueError("Invalid model choice")
|
| 393 |
-
|
| 394 |
-
# Examples (tailored for flux.1-dev-realism)
|
| 395 |
examples = [
|
| 396 |
-
"
|
| 397 |
-
"
|
| 398 |
-
"
|
| 399 |
-
"
|
| 400 |
]
|
| 401 |
|
| 402 |
css = '''
|
|
@@ -412,7 +167,6 @@ footer {
|
|
| 412 |
}
|
| 413 |
'''
|
| 414 |
|
| 415 |
-
# Gradio interface
|
| 416 |
with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
| 417 |
gr.Markdown(DESCRIPTION)
|
| 418 |
with gr.Row():
|
|
@@ -425,23 +179,15 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
|
| 425 |
)
|
| 426 |
run_button = gr.Button("Run", scale=0, variant="primary")
|
| 427 |
result = gr.Gallery(label="Result", columns=1, show_label=False, preview=True)
|
| 428 |
-
|
| 429 |
-
with gr.Row():
|
| 430 |
-
# Model choice radio button above additional options
|
| 431 |
-
model_choice = gr.Radio(
|
| 432 |
-
choices=["flux.1-krea-dev", "flux.1-dev-realism"],
|
| 433 |
-
label="Select Model",
|
| 434 |
-
value="flux.1-krea-dev"
|
| 435 |
-
)
|
| 436 |
-
|
| 437 |
with gr.Accordion("Additional Options", open=False):
|
| 438 |
style_selection = gr.Dropdown(
|
| 439 |
-
label="Quality Style
|
| 440 |
choices=STYLE_NAMES,
|
| 441 |
value=DEFAULT_STYLE_NAME,
|
| 442 |
interactive=True,
|
| 443 |
)
|
| 444 |
-
use_negative_prompt = gr.Checkbox(label="Use negative prompt
|
| 445 |
negative_prompt = gr.Text(
|
| 446 |
label="Negative prompt",
|
| 447 |
max_lines=1,
|
|
@@ -476,14 +222,14 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
|
| 476 |
minimum=0.1,
|
| 477 |
maximum=20.0,
|
| 478 |
step=0.1,
|
| 479 |
-
value=
|
| 480 |
)
|
| 481 |
num_inference_steps = gr.Slider(
|
| 482 |
label="Number of inference steps",
|
| 483 |
minimum=1,
|
| 484 |
maximum=40,
|
| 485 |
step=1,
|
| 486 |
-
value=
|
| 487 |
)
|
| 488 |
num_images = gr.Slider(
|
| 489 |
label="Number of images",
|
|
@@ -521,7 +267,6 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
|
| 521 |
],
|
| 522 |
fn=generate,
|
| 523 |
inputs=[
|
| 524 |
-
model_choice,
|
| 525 |
prompt,
|
| 526 |
negative_prompt,
|
| 527 |
use_negative_prompt,
|
|
@@ -540,4 +285,4 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
|
| 540 |
)
|
| 541 |
|
| 542 |
if __name__ == "__main__":
|
| 543 |
-
demo.queue(max_size=
|
|
|
|
| 1 |
import spaces
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
from PIL import Image
|
| 5 |
+
from diffusers import FluxPipeline # Changed from DiffusionPipeline for better compatibility
|
| 6 |
import random
|
| 7 |
import uuid
|
| 8 |
+
from typing import Tuple
|
| 9 |
import numpy as np
|
| 10 |
import time
|
| 11 |
import zipfile
|
|
|
|
| 12 |
|
| 13 |
+
# --- Pruna Imports ---
|
| 14 |
+
from pruna import SmashConfig, smash
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
DESCRIPTION = """## flux realism
|
| 17 |
+
"""
|
| 18 |
|
|
|
|
| 19 |
def save_image(img):
|
| 20 |
unique_name = str(uuid.uuid4()) + ".png"
|
| 21 |
img.save(unique_name)
|
|
|
|
| 27 |
return seed
|
| 28 |
|
| 29 |
MAX_SEED = np.iinfo(np.int32).max
|
|
|
|
| 30 |
|
| 31 |
+
# --- Model and Pipeline Setup ---
|
| 32 |
+
base_model = "black-forest-labs/FLUX.1-dev"
|
| 33 |
+
# Use FluxPipeline directly and move to CUDA before applying optimizations
|
| 34 |
+
pipe = FluxPipeline.from_pretrained(base_model, torch_dtype=torch.bfloat16)
|
| 35 |
+
|
| 36 |
lora_repo = "strangerzonehf/Flux-Super-Realism-LoRA"
|
| 37 |
trigger_word = "Super Realism"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
pipe.load_lora_weights(lora_repo)
|
| 40 |
+
pipe.to("cuda")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
# --- Pruna Optimization ---
|
| 43 |
+
print("Applying Pruna optimizations...")
|
| 44 |
+
smash_config = SmashConfig()
|
| 45 |
+
smash_config["cacher"] = "fora"
|
| 46 |
+
smash_config["fora_interval"] = 3 # or 2 for even faster inference
|
| 47 |
+
smash_config["compiler"] = "torch_compile"
|
| 48 |
+
smash_config["torch_compile_mode"] = "max-autotune-no-cudagraphs"
|
| 49 |
+
smash_config["quantizer"] = "torchao"
|
| 50 |
+
smash_config["torchao_quant_type"] = "int8dq" # you can also try fp8dq
|
| 51 |
+
smash_config["torchao_excluded_modules"] = "norm+embedding"
|
| 52 |
|
| 53 |
+
# Apply smash to the pipeline
|
| 54 |
+
smashed_pipe = smash(pipe, smash_config)
|
| 55 |
+
print("Pruna optimizations applied successfully.")
|
| 56 |
|
|
|
|
|
|
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
style_list = [
|
| 59 |
+
{
|
| 60 |
+
"name": "3840 x 2160",
|
| 61 |
+
"prompt": "hyper-realistic 8K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
|
| 62 |
+
"negative_prompt": "",
|
| 63 |
+
},
|
| 64 |
+
{
|
| 65 |
+
"name": "2560 x 1440",
|
| 66 |
+
"prompt": "hyper-realistic 4K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
|
| 67 |
+
"negative_prompt": "",
|
| 68 |
+
},
|
| 69 |
+
{
|
| 70 |
+
"name": "HD+",
|
| 71 |
+
"prompt": "hyper-realistic 2K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
|
| 72 |
+
"negative_prompt": "",
|
| 73 |
+
},
|
| 74 |
+
{
|
| 75 |
+
"name": "Style Zero",
|
| 76 |
+
"prompt": "{prompt}",
|
| 77 |
+
"negative_prompt": "",
|
| 78 |
+
},
|
| 79 |
]
|
| 80 |
|
| 81 |
styles = {k["name"]: (k["prompt"], k["negative_prompt"]) for k in style_list}
|
|
|
|
| 86 |
p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
|
| 87 |
return p.replace("{prompt}", positive), n
|
| 88 |
|
|
|
|
| 89 |
@spaces.GPU
|
| 90 |
+
def generate(
|
| 91 |
prompt: str,
|
| 92 |
negative_prompt: str = "",
|
| 93 |
use_negative_prompt: bool = False,
|
|
|
|
| 97 |
guidance_scale: float = 3,
|
| 98 |
randomize_seed: bool = False,
|
| 99 |
style_name: str = DEFAULT_STYLE_NAME,
|
| 100 |
+
num_inference_steps: int = 20, # Default value updated for faster inference
|
| 101 |
num_images: int = 1,
|
| 102 |
zip_images: bool = False,
|
| 103 |
progress=gr.Progress(track_tqdm=True),
|
|
|
|
| 119 |
|
| 120 |
start_time = time.time()
|
| 121 |
|
| 122 |
+
# --- Use the smashed_pipe for generation ---
|
| 123 |
+
images = smashed_pipe(
|
| 124 |
prompt=positive_prompt,
|
| 125 |
negative_prompt=final_negative_prompt if final_negative_prompt else None,
|
| 126 |
width=width,
|
|
|
|
| 147 |
|
| 148 |
return image_paths, seed, f"{duration:.2f}", zip_path
|
| 149 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
examples = [
|
| 151 |
+
"Super Realism, High-resolution photograph, woman, UHD, photorealistic, shot on a Sony A7III --chaos 20 --ar 1:2 --style raw --stylize 250",
|
| 152 |
+
"Woman in a red jacket, snowy, in the style of hyper-realistic portraiture, caninecore, mountainous vistas, timeless beauty, palewave, iconic, distinctive noses --ar 72:101 --stylize 750 --v 6",
|
| 153 |
+
"Super Realism, Headshot of handsome young man, wearing dark gray sweater with buttons and big shawl collar, brown hair and short beard, serious look on his face, black background, soft studio lighting, portrait photography --ar 85:128 --v 6.0 --style",
|
| 154 |
+
"Super-realism, Purple Dreamy, a medium-angle shot of a young woman with long brown hair, wearing a pair of eye-level glasses, stands in front of a backdrop of purple and white lights. The womans eyes are closed, her lips are slightly parted, as if she is looking up at the sky. Her hair is cascading over her shoulders, framing her face. She is wearing a sleeveless top, adorned with tiny white dots, and a gold chain necklace around her neck. Her left earrings are dangling from her ears, adding a pop of color to the scene."
|
| 155 |
]
|
| 156 |
|
| 157 |
css = '''
|
|
|
|
| 167 |
}
|
| 168 |
'''
|
| 169 |
|
|
|
|
| 170 |
with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
| 171 |
gr.Markdown(DESCRIPTION)
|
| 172 |
with gr.Row():
|
|
|
|
| 179 |
)
|
| 180 |
run_button = gr.Button("Run", scale=0, variant="primary")
|
| 181 |
result = gr.Gallery(label="Result", columns=1, show_label=False, preview=True)
|
| 182 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
with gr.Accordion("Additional Options", open=False):
|
| 184 |
style_selection = gr.Dropdown(
|
| 185 |
+
label="Quality Style",
|
| 186 |
choices=STYLE_NAMES,
|
| 187 |
value=DEFAULT_STYLE_NAME,
|
| 188 |
interactive=True,
|
| 189 |
)
|
| 190 |
+
use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=False)
|
| 191 |
negative_prompt = gr.Text(
|
| 192 |
label="Negative prompt",
|
| 193 |
max_lines=1,
|
|
|
|
| 222 |
minimum=0.1,
|
| 223 |
maximum=20.0,
|
| 224 |
step=0.1,
|
| 225 |
+
value=3.0,
|
| 226 |
)
|
| 227 |
num_inference_steps = gr.Slider(
|
| 228 |
label="Number of inference steps",
|
| 229 |
minimum=1,
|
| 230 |
maximum=40,
|
| 231 |
step=1,
|
| 232 |
+
value=20, # Default value lowered for optimized performance
|
| 233 |
)
|
| 234 |
num_images = gr.Slider(
|
| 235 |
label="Number of images",
|
|
|
|
| 267 |
],
|
| 268 |
fn=generate,
|
| 269 |
inputs=[
|
|
|
|
| 270 |
prompt,
|
| 271 |
negative_prompt,
|
| 272 |
use_negative_prompt,
|
|
|
|
| 285 |
)
|
| 286 |
|
| 287 |
if __name__ == "__main__":
|
| 288 |
+
demo.queue(max_size=120).launch(mcp_server=True, ssr_mode=False, show_error=True)
|