Spaces:
Sleeping
Sleeping
Commit
·
c574468
1
Parent(s):
b236df4
css and speed
Browse files- app.py +27 -22
- local_app.py +39 -33
app.py
CHANGED
|
@@ -12,6 +12,7 @@ import gradio as gr
|
|
| 12 |
import numpy as np
|
| 13 |
import spaces
|
| 14 |
# import imageio
|
|
|
|
| 15 |
import torch
|
| 16 |
from PIL import Image
|
| 17 |
from diffusers import (
|
|
@@ -113,16 +114,10 @@ preprocessor = Preprocessor()
|
|
| 113 |
preprocessor.load("NormalBae")
|
| 114 |
|
| 115 |
print("---------------Loaded controlnet pipeline---------------")
|
|
|
|
|
|
|
| 116 |
print(f"CUDA memory allocated: {torch.cuda.max_memory_allocated(device='cuda') / 1e9:.2f} GB")
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
# @spaces.GPU(duration=12)
|
| 120 |
-
# def init(pipe):
|
| 121 |
-
# pipe.enable_xformers_memory_efficient_attention()
|
| 122 |
-
# pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
|
| 123 |
-
# pipe.unet.set_attn_processor(AttnProcessor2_0())
|
| 124 |
-
# print("Model Compiled!")
|
| 125 |
-
# init(pipe)
|
| 126 |
|
| 127 |
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
| 128 |
if randomize_seed:
|
|
@@ -246,20 +241,30 @@ def apply_style(style_name):
|
|
| 246 |
|
| 247 |
|
| 248 |
css = """
|
| 249 |
-
h1 {
|
| 250 |
text-align: center;
|
| 251 |
-
display:block;
|
| 252 |
}
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
display:block;
|
| 256 |
}
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
}
|
| 261 |
-
.gradio-container{max-width: 1200px !important}
|
| 262 |
-
footer {visibility: hidden}
|
| 263 |
"""
|
| 264 |
with gr.Blocks(theme="bethecloud/storj_theme", css=css) as demo:
|
| 265 |
#############################################################################
|
|
@@ -400,7 +405,7 @@ def process_image(
|
|
| 400 |
guidance_scale,
|
| 401 |
seed,
|
| 402 |
):
|
| 403 |
-
torch.cuda.synchronize()
|
| 404 |
preprocess_start = time.time()
|
| 405 |
print("processing image")
|
| 406 |
|
|
@@ -439,8 +444,8 @@ def process_image(
|
|
| 439 |
print(f"\n-------------------------Preprocess done in: {preprocess_time:.2f} seconds-------------------------")
|
| 440 |
print(f"\n-------------------------Inference done in: {time.time() - start:.2f} seconds-------------------------")
|
| 441 |
results.save("temp_image.jpg")
|
| 442 |
-
torch.cuda.synchronize()
|
| 443 |
-
torch.cuda.empty_cache()
|
| 444 |
return results
|
| 445 |
|
| 446 |
if prod:
|
|
|
|
| 12 |
import numpy as np
|
| 13 |
import spaces
|
| 14 |
# import imageio
|
| 15 |
+
import gc
|
| 16 |
import torch
|
| 17 |
from PIL import Image
|
| 18 |
from diffusers import (
|
|
|
|
| 114 |
preprocessor.load("NormalBae")
|
| 115 |
|
| 116 |
print("---------------Loaded controlnet pipeline---------------")
|
| 117 |
+
torch.cuda.empty_cache()
|
| 118 |
+
gc.collect()
|
| 119 |
print(f"CUDA memory allocated: {torch.cuda.max_memory_allocated(device='cuda') / 1e9:.2f} GB")
|
| 120 |
+
print("Model Compiled!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
| 123 |
if randomize_seed:
|
|
|
|
| 241 |
|
| 242 |
|
| 243 |
css = """
|
| 244 |
+
h1, h2, h3 {
|
| 245 |
text-align: center;
|
| 246 |
+
display: block;
|
| 247 |
}
|
| 248 |
+
footer {
|
| 249 |
+
visibility: hidden;
|
|
|
|
| 250 |
}
|
| 251 |
+
.gradio-container {
|
| 252 |
+
max-width: 900px !important;
|
| 253 |
+
}
|
| 254 |
+
.gr-image {
|
| 255 |
+
display: flex;
|
| 256 |
+
justify-content: center;
|
| 257 |
+
align-items: center;
|
| 258 |
+
width: 100%;
|
| 259 |
+
height: 512px;
|
| 260 |
+
overflow: hidden;
|
| 261 |
+
}
|
| 262 |
+
.gr-image img {
|
| 263 |
+
width: 100%;
|
| 264 |
+
height: 100%;
|
| 265 |
+
object-fit: cover;
|
| 266 |
+
object-position: center;
|
| 267 |
}
|
|
|
|
|
|
|
| 268 |
"""
|
| 269 |
with gr.Blocks(theme="bethecloud/storj_theme", css=css) as demo:
|
| 270 |
#############################################################################
|
|
|
|
| 405 |
guidance_scale,
|
| 406 |
seed,
|
| 407 |
):
|
| 408 |
+
# torch.cuda.synchronize()
|
| 409 |
preprocess_start = time.time()
|
| 410 |
print("processing image")
|
| 411 |
|
|
|
|
| 444 |
print(f"\n-------------------------Preprocess done in: {preprocess_time:.2f} seconds-------------------------")
|
| 445 |
print(f"\n-------------------------Inference done in: {time.time() - start:.2f} seconds-------------------------")
|
| 446 |
results.save("temp_image.jpg")
|
| 447 |
+
# torch.cuda.synchronize()
|
| 448 |
+
# torch.cuda.empty_cache()
|
| 449 |
return results
|
| 450 |
|
| 451 |
if prod:
|
local_app.py
CHANGED
|
@@ -10,7 +10,7 @@ import random
|
|
| 10 |
import time
|
| 11 |
import gradio as gr
|
| 12 |
import numpy as np
|
| 13 |
-
|
| 14 |
import torch
|
| 15 |
from PIL import Image
|
| 16 |
from diffusers import (
|
|
@@ -19,7 +19,7 @@ from diffusers import (
|
|
| 19 |
StableDiffusionControlNetPipeline,
|
| 20 |
AutoencoderKL,
|
| 21 |
)
|
| 22 |
-
|
| 23 |
MAX_SEED = np.iinfo(np.int32).max
|
| 24 |
API_KEY = os.environ.get("API_KEY", None)
|
| 25 |
|
|
@@ -58,9 +58,9 @@ if gr.NO_RELOAD:
|
|
| 58 |
base_model_url = "https://huggingface.co/Lykon/AbsoluteReality/blob/main/AbsoluteReality_1.8.1_pruned.safetensors"
|
| 59 |
vae_url = "https://huggingface.co/stabilityai/sd-vae-ft-mse-original/blob/main/vae-ft-mse-840000-ema-pruned.safetensors"
|
| 60 |
|
| 61 |
-
print('loading vae')
|
| 62 |
-
vae = AutoencoderKL.from_single_file(vae_url, torch_dtype=torch.float16).to("cuda")
|
| 63 |
-
vae.to(memory_format=torch.channels_last)
|
| 64 |
|
| 65 |
print('loading pipe')
|
| 66 |
pipe = StableDiffusionControlNetPipeline.from_single_file(
|
|
@@ -69,7 +69,7 @@ if gr.NO_RELOAD:
|
|
| 69 |
# load_safety_checker=True,
|
| 70 |
controlnet=controlnet,
|
| 71 |
scheduler=scheduler,
|
| 72 |
-
vae=vae,
|
| 73 |
torch_dtype=torch.float16,
|
| 74 |
)
|
| 75 |
|
|
@@ -88,10 +88,14 @@ if gr.NO_RELOAD:
|
|
| 88 |
print("loading preprocessor")
|
| 89 |
from preprocess import Preprocessor
|
| 90 |
preprocessor = Preprocessor()
|
| 91 |
-
|
| 92 |
|
| 93 |
print("---------------Loaded controlnet pipeline---------------")
|
|
|
|
|
|
|
|
|
|
| 94 |
print(f"CUDA memory allocated: {torch.cuda.max_memory_allocated(device='cuda') / 1e9:.2f} GB")
|
|
|
|
| 95 |
|
| 96 |
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
| 97 |
if randomize_seed:
|
|
@@ -219,12 +223,12 @@ h1, h2, h3 {
|
|
| 219 |
text-align: center;
|
| 220 |
display: block;
|
| 221 |
}
|
| 222 |
-
.gradio-container {
|
| 223 |
-
max-width: 1200px !important;
|
| 224 |
-
}
|
| 225 |
footer {
|
| 226 |
visibility: hidden;
|
| 227 |
}
|
|
|
|
|
|
|
|
|
|
| 228 |
.gr-image {
|
| 229 |
display: flex;
|
| 230 |
justify-content: center;
|
|
@@ -293,8 +297,8 @@ with gr.Blocks(theme="bethecloud/storj_theme", css=css) as demo:
|
|
| 293 |
label="Design Styles",
|
| 294 |
)
|
| 295 |
# input image
|
| 296 |
-
with gr.Row(
|
| 297 |
-
with gr.Column(
|
| 298 |
image = gr.Image(
|
| 299 |
label="Input",
|
| 300 |
sources=["upload"],
|
|
@@ -306,7 +310,7 @@ with gr.Blocks(theme="bethecloud/storj_theme", css=css) as demo:
|
|
| 306 |
with gr.Column():
|
| 307 |
run_button = gr.Button(value="Use this one", size="lg", visible=False)
|
| 308 |
# output image
|
| 309 |
-
with gr.Column(
|
| 310 |
result = gr.Image(
|
| 311 |
label="Output",
|
| 312 |
interactive=False,
|
|
@@ -338,18 +342,21 @@ with gr.Blocks(theme="bethecloud/storj_theme", css=css) as demo:
|
|
| 338 |
def auto_process_image(image, style_selection, prompt, a_prompt, n_prompt, num_images, image_resolution, preprocess_resolution, num_steps, guidance_scale, seed, progress=gr.Progress(track_tqdm=True)):
|
| 339 |
return process_image(image, style_selection, prompt, a_prompt, n_prompt, num_images, image_resolution, preprocess_resolution, num_steps, guidance_scale, seed)
|
| 340 |
|
| 341 |
-
# AI Image Processing
|
| 342 |
-
@gr.on(triggers=[use_ai_button.click], inputs=config, outputs=
|
| 343 |
def submit(image, style_selection, prompt, a_prompt, n_prompt, num_images, image_resolution, preprocess_resolution, num_steps, guidance_scale, seed, progress=gr.Progress(track_tqdm=True)):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 344 |
try:
|
| 345 |
print("Updating image to AI Temp Image")
|
| 346 |
-
|
| 347 |
-
|
| 348 |
except FileNotFoundError:
|
| 349 |
print("No AI Image Available")
|
| 350 |
-
|
| 351 |
-
result = process_image(image, style_selection, prompt, a_prompt, n_prompt, num_images, image_resolution, preprocess_resolution, num_steps, guidance_scale, seed)
|
| 352 |
-
yield gr.update(), result
|
| 353 |
|
| 354 |
# Turn off buttons when processing
|
| 355 |
@gr.on(triggers=[image.upload, use_ai_button.click, run_button.click], inputs=None, outputs=[run_button, use_ai_button], show_progress="hidden")
|
|
@@ -375,7 +382,7 @@ def process_image(
|
|
| 375 |
guidance_scale,
|
| 376 |
seed,
|
| 377 |
):
|
| 378 |
-
torch.cuda.synchronize()
|
| 379 |
preprocess_start = time.time()
|
| 380 |
print("processing image")
|
| 381 |
preprocessor.load("NormalBae")
|
|
@@ -400,21 +407,20 @@ def process_image(
|
|
| 400 |
negative_prompt=str(n_prompt)
|
| 401 |
print(prompt)
|
| 402 |
start = time.time()
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
).images[0]
|
| 413 |
print(f"\n-------------------------Preprocess done in: {preprocess_time:.2f} seconds-------------------------")
|
| 414 |
print(f"\n-------------------------Inference done in: {time.time() - start:.2f} seconds-------------------------")
|
| 415 |
results.save("temp_image.jpg")
|
| 416 |
-
torch.cuda.synchronize()
|
| 417 |
-
torch.cuda.empty_cache()
|
| 418 |
return results
|
| 419 |
|
| 420 |
if prod:
|
|
|
|
| 10 |
import time
|
| 11 |
import gradio as gr
|
| 12 |
import numpy as np
|
| 13 |
+
import gc
|
| 14 |
import torch
|
| 15 |
from PIL import Image
|
| 16 |
from diffusers import (
|
|
|
|
| 19 |
StableDiffusionControlNetPipeline,
|
| 20 |
AutoencoderKL,
|
| 21 |
)
|
| 22 |
+
from diffusers.models.attention_processor import AttnProcessor2_0
|
| 23 |
MAX_SEED = np.iinfo(np.int32).max
|
| 24 |
API_KEY = os.environ.get("API_KEY", None)
|
| 25 |
|
|
|
|
| 58 |
base_model_url = "https://huggingface.co/Lykon/AbsoluteReality/blob/main/AbsoluteReality_1.8.1_pruned.safetensors"
|
| 59 |
vae_url = "https://huggingface.co/stabilityai/sd-vae-ft-mse-original/blob/main/vae-ft-mse-840000-ema-pruned.safetensors"
|
| 60 |
|
| 61 |
+
# print('loading vae')
|
| 62 |
+
# vae = AutoencoderKL.from_single_file(vae_url, torch_dtype=torch.float16).to("cuda")
|
| 63 |
+
# vae.to(memory_format=torch.channels_last)
|
| 64 |
|
| 65 |
print('loading pipe')
|
| 66 |
pipe = StableDiffusionControlNetPipeline.from_single_file(
|
|
|
|
| 69 |
# load_safety_checker=True,
|
| 70 |
controlnet=controlnet,
|
| 71 |
scheduler=scheduler,
|
| 72 |
+
# vae=vae,
|
| 73 |
torch_dtype=torch.float16,
|
| 74 |
)
|
| 75 |
|
|
|
|
| 88 |
print("loading preprocessor")
|
| 89 |
from preprocess import Preprocessor
|
| 90 |
preprocessor = Preprocessor()
|
| 91 |
+
preprocessor.load("NormalBae")
|
| 92 |
|
| 93 |
print("---------------Loaded controlnet pipeline---------------")
|
| 94 |
+
pipe.unet.set_attn_processor(AttnProcessor2_0())
|
| 95 |
+
torch.cuda.empty_cache()
|
| 96 |
+
gc.collect()
|
| 97 |
print(f"CUDA memory allocated: {torch.cuda.max_memory_allocated(device='cuda') / 1e9:.2f} GB")
|
| 98 |
+
print("Model Compiled!")
|
| 99 |
|
| 100 |
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
| 101 |
if randomize_seed:
|
|
|
|
| 223 |
text-align: center;
|
| 224 |
display: block;
|
| 225 |
}
|
|
|
|
|
|
|
|
|
|
| 226 |
footer {
|
| 227 |
visibility: hidden;
|
| 228 |
}
|
| 229 |
+
.gradio-container {
|
| 230 |
+
max-width: 900px !important;
|
| 231 |
+
}
|
| 232 |
.gr-image {
|
| 233 |
display: flex;
|
| 234 |
justify-content: center;
|
|
|
|
| 297 |
label="Design Styles",
|
| 298 |
)
|
| 299 |
# input image
|
| 300 |
+
with gr.Row():
|
| 301 |
+
with gr.Column():
|
| 302 |
image = gr.Image(
|
| 303 |
label="Input",
|
| 304 |
sources=["upload"],
|
|
|
|
| 310 |
with gr.Column():
|
| 311 |
run_button = gr.Button(value="Use this one", size="lg", visible=False)
|
| 312 |
# output image
|
| 313 |
+
with gr.Column():
|
| 314 |
result = gr.Image(
|
| 315 |
label="Output",
|
| 316 |
interactive=False,
|
|
|
|
| 342 |
def auto_process_image(image, style_selection, prompt, a_prompt, n_prompt, num_images, image_resolution, preprocess_resolution, num_steps, guidance_scale, seed, progress=gr.Progress(track_tqdm=True)):
|
| 343 |
return process_image(image, style_selection, prompt, a_prompt, n_prompt, num_images, image_resolution, preprocess_resolution, num_steps, guidance_scale, seed)
|
| 344 |
|
| 345 |
+
# AI Image Processing
|
| 346 |
+
@gr.on(triggers=[use_ai_button.click], inputs=config, outputs=result, show_progress="minimal")
|
| 347 |
def submit(image, style_selection, prompt, a_prompt, n_prompt, num_images, image_resolution, preprocess_resolution, num_steps, guidance_scale, seed, progress=gr.Progress(track_tqdm=True)):
|
| 348 |
+
return process_image(image, style_selection, prompt, a_prompt, n_prompt, num_images, image_resolution, preprocess_resolution, num_steps, guidance_scale, seed)
|
| 349 |
+
|
| 350 |
+
# Change input to result
|
| 351 |
+
@gr.on(triggers=[use_ai_button.click], inputs=None, outputs=image, show_progress="hidden")
|
| 352 |
+
def update_input():
|
| 353 |
try:
|
| 354 |
print("Updating image to AI Temp Image")
|
| 355 |
+
ai_temp_image = Image.open("temp_image.jpg")
|
| 356 |
+
return ai_temp_image
|
| 357 |
except FileNotFoundError:
|
| 358 |
print("No AI Image Available")
|
| 359 |
+
return None
|
|
|
|
|
|
|
| 360 |
|
| 361 |
# Turn off buttons when processing
|
| 362 |
@gr.on(triggers=[image.upload, use_ai_button.click, run_button.click], inputs=None, outputs=[run_button, use_ai_button], show_progress="hidden")
|
|
|
|
| 382 |
guidance_scale,
|
| 383 |
seed,
|
| 384 |
):
|
| 385 |
+
# torch.cuda.synchronize()
|
| 386 |
preprocess_start = time.time()
|
| 387 |
print("processing image")
|
| 388 |
preprocessor.load("NormalBae")
|
|
|
|
| 407 |
negative_prompt=str(n_prompt)
|
| 408 |
print(prompt)
|
| 409 |
start = time.time()
|
| 410 |
+
results = pipe(
|
| 411 |
+
prompt=prompt,
|
| 412 |
+
negative_prompt=negative_prompt,
|
| 413 |
+
guidance_scale=guidance_scale,
|
| 414 |
+
num_images_per_prompt=num_images,
|
| 415 |
+
num_inference_steps=num_steps,
|
| 416 |
+
generator=generator,
|
| 417 |
+
image=control_image,
|
| 418 |
+
).images[0]
|
|
|
|
| 419 |
print(f"\n-------------------------Preprocess done in: {preprocess_time:.2f} seconds-------------------------")
|
| 420 |
print(f"\n-------------------------Inference done in: {time.time() - start:.2f} seconds-------------------------")
|
| 421 |
results.save("temp_image.jpg")
|
| 422 |
+
# torch.cuda.synchronize()
|
| 423 |
+
# torch.cuda.empty_cache()
|
| 424 |
return results
|
| 425 |
|
| 426 |
if prod:
|