Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,6 +11,7 @@ import numpy as np
|
|
| 11 |
from diffusers.models.attention_processor import AttnProcessor2_0
|
| 12 |
import gradio as gr
|
| 13 |
import spaces
|
|
|
|
| 14 |
|
| 15 |
USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE", "0") == "1"
|
| 16 |
ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"
|
|
@@ -52,8 +53,6 @@ def download_models():
|
|
| 52 |
|
| 53 |
download_models()
|
| 54 |
|
| 55 |
-
import time
|
| 56 |
-
|
| 57 |
def timer_func(func):
|
| 58 |
def wrapper(*args, **kwargs):
|
| 59 |
start_time = time.time()
|
|
@@ -71,35 +70,20 @@ class LazyLoadPipeline:
|
|
| 71 |
def load(self):
|
| 72 |
if self.pipe is None:
|
| 73 |
print("Starting to load the pipeline...")
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
print(f"Moving pipeline to device: {device}")
|
| 81 |
-
self.pipe.to(device)
|
| 82 |
-
if USE_TORCH_COMPILE:
|
| 83 |
-
print("Compiling the model...")
|
| 84 |
-
self.pipe.unet = torch.compile(self.pipe.unet, mode="reduce-overhead", fullgraph=True)
|
| 85 |
-
except Exception as e:
|
| 86 |
-
print(f"Error loading pipeline: {str(e)}")
|
| 87 |
-
raise
|
| 88 |
|
| 89 |
@timer_func
|
| 90 |
def setup_pipeline(self):
|
| 91 |
print("Setting up the pipeline...")
|
| 92 |
-
start_time = time.time()
|
| 93 |
controlnet = ControlNetModel.from_single_file(
|
| 94 |
"models/ControlNet/control_v11f1e_sd15_tile.pth", torch_dtype=torch.float16
|
| 95 |
)
|
| 96 |
-
print(f"ControlNet loaded in {time.time() - start_time:.2f} seconds")
|
| 97 |
-
|
| 98 |
-
start_time = time.time()
|
| 99 |
safety_checker = StableDiffusionSafetyChecker.from_pretrained("CompVis/stable-diffusion-safety-checker")
|
| 100 |
-
print(f"Safety checker loaded in {time.time() - start_time:.2f} seconds")
|
| 101 |
-
|
| 102 |
-
start_time = time.time()
|
| 103 |
model_path = "models/models/Stable-diffusion/juggernaut_reborn.safetensors"
|
| 104 |
pipe = StableDiffusionControlNetImg2ImgPipeline.from_single_file(
|
| 105 |
model_path,
|
|
@@ -108,37 +92,21 @@ class LazyLoadPipeline:
|
|
| 108 |
use_safetensors=True,
|
| 109 |
safety_checker=safety_checker
|
| 110 |
)
|
| 111 |
-
print(f"Main pipeline loaded in {time.time() - start_time:.2f} seconds")
|
| 112 |
-
|
| 113 |
-
start_time = time.time()
|
| 114 |
vae = AutoencoderKL.from_single_file(
|
| 115 |
"models/VAE/vae-ft-mse-840000-ema-pruned.safetensors",
|
| 116 |
torch_dtype=torch.float16
|
| 117 |
)
|
| 118 |
pipe.vae = vae
|
| 119 |
-
print(f"VAE loaded in {time.time() - start_time:.2f} seconds")
|
| 120 |
-
|
| 121 |
-
print("Loading textual inversions and LoRA weights...")
|
| 122 |
-
start_time = time.time()
|
| 123 |
pipe.load_textual_inversion("models/embeddings/verybadimagenegative_v1.3.pt")
|
| 124 |
pipe.load_textual_inversion("models/embeddings/JuggernautNegative-neg.pt")
|
| 125 |
-
print(f"Textual inversions loaded in {time.time() - start_time:.2f} seconds")
|
| 126 |
-
|
| 127 |
-
start_time = time.time()
|
| 128 |
pipe.load_lora_weights("models/Lora/SDXLrender_v2.0.safetensors")
|
| 129 |
pipe.fuse_lora(lora_scale=0.5)
|
| 130 |
pipe.load_lora_weights("models/Lora/more_details.safetensors")
|
| 131 |
-
print(f"LoRA weights loaded in {time.time() - start_time:.2f} seconds")
|
| 132 |
-
|
| 133 |
-
start_time = time.time()
|
| 134 |
pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
|
| 135 |
pipe.enable_freeu(s1=0.9, s2=0.2, b1=1.3, b2=1.4)
|
| 136 |
-
print(f"Scheduler and FreeU set up in {time.time() - start_time:.2f} seconds")
|
| 137 |
-
|
| 138 |
return pipe
|
| 139 |
|
| 140 |
def __call__(self, *args, **kwargs):
|
| 141 |
-
self.load()
|
| 142 |
return self.pipe(*args, **kwargs)
|
| 143 |
|
| 144 |
class LazyRealESRGAN:
|
|
@@ -173,7 +141,7 @@ def resize_and_upscale(input_image, resolution):
|
|
| 173 |
else:
|
| 174 |
img = lazy_realesrgan_x4.predict(img)
|
| 175 |
return img
|
| 176 |
-
|
| 177 |
@timer_func
|
| 178 |
def create_hdr_effect(original_image, hdr):
|
| 179 |
if hdr == 0:
|
|
@@ -189,44 +157,41 @@ def create_hdr_effect(original_image, hdr):
|
|
| 189 |
return Image.fromarray(cv2.cvtColor(hdr_image_8bit, cv2.COLOR_BGR2RGB))
|
| 190 |
|
| 191 |
lazy_pipe = LazyLoadPipeline()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
|
| 193 |
@spaces.GPU
|
| 194 |
@timer_func
|
| 195 |
def gradio_process_image(input_image, resolution, num_inference_steps, strength, hdr, guidance_scale):
|
| 196 |
print("Starting image processing...")
|
| 197 |
torch.cuda.empty_cache()
|
| 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 |
-
print("Running inference...")
|
| 224 |
-
result = lazy_pipe(**options).images[0]
|
| 225 |
-
print("Image processing completed successfully")
|
| 226 |
-
return result
|
| 227 |
-
except Exception as e:
|
| 228 |
-
print(f"Error during image processing: {str(e)}")
|
| 229 |
-
raise gr.Error(f"An error occurred: {str(e)}")
|
| 230 |
|
| 231 |
# Gradio interface
|
| 232 |
with gr.Blocks() as demo:
|
|
|
|
| 11 |
from diffusers.models.attention_processor import AttnProcessor2_0
|
| 12 |
import gradio as gr
|
| 13 |
import spaces
|
| 14 |
+
import time
|
| 15 |
|
| 16 |
USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE", "0") == "1"
|
| 17 |
ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"
|
|
|
|
| 53 |
|
| 54 |
download_models()
|
| 55 |
|
|
|
|
|
|
|
| 56 |
def timer_func(func):
|
| 57 |
def wrapper(*args, **kwargs):
|
| 58 |
start_time = time.time()
|
|
|
|
| 70 |
def load(self):
|
| 71 |
if self.pipe is None:
|
| 72 |
print("Starting to load the pipeline...")
|
| 73 |
+
self.pipe = self.setup_pipeline()
|
| 74 |
+
print(f"Moving pipeline to device: {device}")
|
| 75 |
+
self.pipe.to(device)
|
| 76 |
+
if USE_TORCH_COMPILE:
|
| 77 |
+
print("Compiling the model...")
|
| 78 |
+
self.pipe.unet = torch.compile(self.pipe.unet, mode="reduce-overhead", fullgraph=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
@timer_func
|
| 81 |
def setup_pipeline(self):
|
| 82 |
print("Setting up the pipeline...")
|
|
|
|
| 83 |
controlnet = ControlNetModel.from_single_file(
|
| 84 |
"models/ControlNet/control_v11f1e_sd15_tile.pth", torch_dtype=torch.float16
|
| 85 |
)
|
|
|
|
|
|
|
|
|
|
| 86 |
safety_checker = StableDiffusionSafetyChecker.from_pretrained("CompVis/stable-diffusion-safety-checker")
|
|
|
|
|
|
|
|
|
|
| 87 |
model_path = "models/models/Stable-diffusion/juggernaut_reborn.safetensors"
|
| 88 |
pipe = StableDiffusionControlNetImg2ImgPipeline.from_single_file(
|
| 89 |
model_path,
|
|
|
|
| 92 |
use_safetensors=True,
|
| 93 |
safety_checker=safety_checker
|
| 94 |
)
|
|
|
|
|
|
|
|
|
|
| 95 |
vae = AutoencoderKL.from_single_file(
|
| 96 |
"models/VAE/vae-ft-mse-840000-ema-pruned.safetensors",
|
| 97 |
torch_dtype=torch.float16
|
| 98 |
)
|
| 99 |
pipe.vae = vae
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
pipe.load_textual_inversion("models/embeddings/verybadimagenegative_v1.3.pt")
|
| 101 |
pipe.load_textual_inversion("models/embeddings/JuggernautNegative-neg.pt")
|
|
|
|
|
|
|
|
|
|
| 102 |
pipe.load_lora_weights("models/Lora/SDXLrender_v2.0.safetensors")
|
| 103 |
pipe.fuse_lora(lora_scale=0.5)
|
| 104 |
pipe.load_lora_weights("models/Lora/more_details.safetensors")
|
|
|
|
|
|
|
|
|
|
| 105 |
pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
|
| 106 |
pipe.enable_freeu(s1=0.9, s2=0.2, b1=1.3, b2=1.4)
|
|
|
|
|
|
|
| 107 |
return pipe
|
| 108 |
|
| 109 |
def __call__(self, *args, **kwargs):
|
|
|
|
| 110 |
return self.pipe(*args, **kwargs)
|
| 111 |
|
| 112 |
class LazyRealESRGAN:
|
|
|
|
| 141 |
else:
|
| 142 |
img = lazy_realesrgan_x4.predict(img)
|
| 143 |
return img
|
| 144 |
+
|
| 145 |
@timer_func
|
| 146 |
def create_hdr_effect(original_image, hdr):
|
| 147 |
if hdr == 0:
|
|
|
|
| 157 |
return Image.fromarray(cv2.cvtColor(hdr_image_8bit, cv2.COLOR_BGR2RGB))
|
| 158 |
|
| 159 |
lazy_pipe = LazyLoadPipeline()
|
| 160 |
+
lazy_pipe.load() # Load the pipeline outside of the GPU function
|
| 161 |
+
|
| 162 |
+
def prepare_image(input_image, resolution, hdr):
|
| 163 |
+
condition_image = resize_and_upscale(input_image, resolution)
|
| 164 |
+
condition_image = create_hdr_effect(condition_image, hdr)
|
| 165 |
+
return condition_image
|
| 166 |
|
| 167 |
@spaces.GPU
|
| 168 |
@timer_func
|
| 169 |
def gradio_process_image(input_image, resolution, num_inference_steps, strength, hdr, guidance_scale):
|
| 170 |
print("Starting image processing...")
|
| 171 |
torch.cuda.empty_cache()
|
| 172 |
+
|
| 173 |
+
condition_image = prepare_image(input_image, resolution, hdr)
|
| 174 |
+
|
| 175 |
+
prompt = "masterpiece, best quality, highres"
|
| 176 |
+
negative_prompt = "low quality, normal quality, ugly, blurry, blur, lowres, bad anatomy, bad hands, cropped, worst quality, verybadimagenegative_v1.3, JuggernautNegative-neg"
|
| 177 |
+
|
| 178 |
+
options = {
|
| 179 |
+
"prompt": prompt,
|
| 180 |
+
"negative_prompt": negative_prompt,
|
| 181 |
+
"image": condition_image,
|
| 182 |
+
"control_image": condition_image,
|
| 183 |
+
"width": condition_image.size[0],
|
| 184 |
+
"height": condition_image.size[1],
|
| 185 |
+
"strength": strength,
|
| 186 |
+
"num_inference_steps": num_inference_steps,
|
| 187 |
+
"guidance_scale": guidance_scale,
|
| 188 |
+
"generator": torch.Generator(device=device).manual_seed(0),
|
| 189 |
+
}
|
| 190 |
+
|
| 191 |
+
print("Running inference...")
|
| 192 |
+
result = lazy_pipe(**options).images[0]
|
| 193 |
+
print("Image processing completed successfully")
|
| 194 |
+
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
|
| 196 |
# Gradio interface
|
| 197 |
with gr.Blocks() as demo:
|