Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,16 @@
|
|
1 |
import spaces
|
2 |
from typing import Tuple, Union, List
|
3 |
import os
|
4 |
-
|
5 |
import numpy as np
|
6 |
from PIL import Image
|
7 |
|
8 |
import torch
|
|
|
|
|
|
|
|
|
|
|
9 |
from diffusers.pipelines.controlnet import StableDiffusionControlNetInpaintPipeline
|
10 |
from diffusers import ControlNetModel, UniPCMultistepScheduler, AutoPipelineForText2Image
|
11 |
from transformers import AutoImageProcessor, UperNetForSemanticSegmentation, AutoModelForDepthEstimation
|
@@ -33,6 +38,188 @@ css = """
|
|
33 |
"""
|
34 |
|
35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
def filter_items(
|
37 |
colors_list: Union[List, np.ndarray],
|
38 |
items_list: Union[List, np.ndarray],
|
@@ -267,12 +454,13 @@ class ControlNetDepthDesignModelMulti:
|
|
267 |
|
268 |
|
269 |
def create_demo(model):
|
270 |
-
gr.Markdown("###
|
271 |
with gr.Row():
|
272 |
with gr.Column():
|
273 |
input_image = gr.Image(label="Input Image", type='pil', elem_id='img-display-input')
|
274 |
input_text = gr.Textbox(label='Prompt', placeholder='Please upload your image first', lines=2)
|
275 |
with gr.Accordion('Advanced options', open=False):
|
|
|
276 |
num_steps = gr.Slider(label='Steps',
|
277 |
minimum=1,
|
278 |
maximum=50,
|
@@ -305,6 +493,13 @@ def create_demo(model):
|
|
305 |
n_prompt = gr.Textbox(
|
306 |
label='Negative Prompt',
|
307 |
value="window, door, low resolution, banner, logo, watermark, text, deformed, blurry, out of focus, surreal, ugly, beginner")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
308 |
submit = gr.Button("Submit")
|
309 |
|
310 |
with gr.Column():
|
@@ -326,6 +521,7 @@ def create_demo(model):
|
|
326 |
inputs=[input_image, input_text], cache_examples=False)
|
327 |
|
328 |
|
|
|
329 |
controlnet_depth= ControlNetModel.from_pretrained(
|
330 |
"controlnet_depth", torch_dtype=dtype, use_safetensors=True)
|
331 |
controlnet_seg = ControlNetModel.from_pretrained(
|
@@ -353,18 +549,18 @@ depth_feature_extractor, depth_estimator = get_depth_pipeline()
|
|
353 |
depth_estimator = depth_estimator.to(device)
|
354 |
|
355 |
|
|
|
356 |
def main():
|
357 |
model = ControlNetDepthDesignModelMulti()
|
358 |
print('Models uploaded successfully')
|
359 |
|
360 |
-
title = "#
|
361 |
description = """
|
362 |
-
|
363 |
"""
|
364 |
with gr.Blocks() as demo:
|
365 |
gr.Markdown(title)
|
366 |
gr.Markdown(description)
|
367 |
-
create_demo(model)
|
368 |
|
369 |
|
370 |
demo.queue().launch(share=False)
|
|
|
1 |
import spaces
|
2 |
from typing import Tuple, Union, List
|
3 |
import os
|
4 |
+
import time
|
5 |
import numpy as np
|
6 |
from PIL import Image
|
7 |
|
8 |
import torch
|
9 |
+
|
10 |
+
from diffusers import StableDiffusionControlNetImg2ImgPipeline, ControlNetModel, DDIMScheduler
|
11 |
+
from diffusers.pipelines.stable_diffusion import StableDiffusionSafetyChecker
|
12 |
+
from diffusers.models import AutoencoderKL
|
13 |
+
from diffusers.models.attention_processor import AttnProcessor2_0
|
14 |
from diffusers.pipelines.controlnet import StableDiffusionControlNetInpaintPipeline
|
15 |
from diffusers import ControlNetModel, UniPCMultistepScheduler, AutoPipelineForText2Image
|
16 |
from transformers import AutoImageProcessor, UperNetForSemanticSegmentation, AutoModelForDepthEstimation
|
|
|
38 |
"""
|
39 |
|
40 |
|
41 |
+
def download_file(url, folder_path, filename):
|
42 |
+
if not os.path.exists(folder_path):
|
43 |
+
os.makedirs(folder_path)
|
44 |
+
file_path = os.path.join(folder_path, filename)
|
45 |
+
|
46 |
+
if os.path.isfile(file_path):
|
47 |
+
print(f"File already exists: {file_path}")
|
48 |
+
else:
|
49 |
+
response = requests.get(url, stream=True)
|
50 |
+
if response.status_code == 200:
|
51 |
+
with open(file_path, 'wb') as file:
|
52 |
+
for chunk in response.iter_content(chunk_size=1024):
|
53 |
+
file.write(chunk)
|
54 |
+
print(f"File successfully downloaded and saved: {file_path}")
|
55 |
+
else:
|
56 |
+
print(f"Error downloading the file. Status code: {response.status_code}")
|
57 |
+
|
58 |
+
def download_models():
|
59 |
+
models = {
|
60 |
+
"MODEL": ("https://huggingface.co/dantea1118/juggernaut_reborn/resolve/main/juggernaut_reborn.safetensors?download=true", "models/models/Stable-diffusion", "juggernaut_reborn.safetensors"),
|
61 |
+
"UPSCALER_X2": ("https://huggingface.co/ai-forever/Real-ESRGAN/resolve/main/RealESRGAN_x2.pth?download=true", "models/upscalers/", "RealESRGAN_x2.pth"),
|
62 |
+
"UPSCALER_X4": ("https://huggingface.co/ai-forever/Real-ESRGAN/resolve/main/RealESRGAN_x4.pth?download=true", "models/upscalers/", "RealESRGAN_x4.pth"),
|
63 |
+
"NEGATIVE_1": ("https://huggingface.co/philz1337x/embeddings/resolve/main/verybadimagenegative_v1.3.pt?download=true", "models/embeddings", "verybadimagenegative_v1.3.pt"),
|
64 |
+
"NEGATIVE_2": ("https://huggingface.co/datasets/AddictiveFuture/sd-negative-embeddings/resolve/main/JuggernautNegative-neg.pt?download=true", "models/embeddings", "JuggernautNegative-neg.pt"),
|
65 |
+
"LORA_1": ("https://huggingface.co/philz1337x/loras/resolve/main/SDXLrender_v2.0.safetensors?download=true", "models/Lora", "SDXLrender_v2.0.safetensors"),
|
66 |
+
"LORA_2": ("https://huggingface.co/philz1337x/loras/resolve/main/more_details.safetensors?download=true", "models/Lora", "more_details.safetensors"),
|
67 |
+
"CONTROLNET": ("https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11f1e_sd15_tile.pth?download=true", "models/ControlNet", "control_v11f1e_sd15_tile.pth"),
|
68 |
+
"VAE": ("https://huggingface.co/stabilityai/sd-vae-ft-mse-original/resolve/main/vae-ft-mse-840000-ema-pruned.safetensors?download=true", "models/VAE", "vae-ft-mse-840000-ema-pruned.safetensors"),
|
69 |
+
}
|
70 |
+
|
71 |
+
for model, (url, folder, filename) in models.items():
|
72 |
+
download_file(url, folder, filename)
|
73 |
+
|
74 |
+
download_models()
|
75 |
+
|
76 |
+
def timer_func(func):
|
77 |
+
def wrapper(*args, **kwargs):
|
78 |
+
start_time = time.time()
|
79 |
+
result = func(*args, **kwargs)
|
80 |
+
end_time = time.time()
|
81 |
+
print(f"{func.__name__} took {end_time - start_time:.2f} seconds")
|
82 |
+
return result
|
83 |
+
return wrapper
|
84 |
+
|
85 |
+
class LazyLoadPipeline:
|
86 |
+
def __init__(self):
|
87 |
+
self.pipe = None
|
88 |
+
|
89 |
+
@timer_func
|
90 |
+
def load(self):
|
91 |
+
if self.pipe is None:
|
92 |
+
print("Starting to load the pipeline...")
|
93 |
+
self.pipe = self.setup_pipeline()
|
94 |
+
print(f"Moving pipeline to device: {device}")
|
95 |
+
self.pipe.to(device)
|
96 |
+
if USE_TORCH_COMPILE:
|
97 |
+
print("Compiling the model...")
|
98 |
+
self.pipe.unet = torch.compile(self.pipe.unet, mode="reduce-overhead", fullgraph=True)
|
99 |
+
|
100 |
+
@timer_func
|
101 |
+
def setup_pipeline(self):
|
102 |
+
print("Setting up the pipeline...")
|
103 |
+
controlnet = ControlNetModel.from_single_file(
|
104 |
+
"models/ControlNet/control_v11f1e_sd15_tile.pth", torch_dtype=torch.float16
|
105 |
+
)
|
106 |
+
safety_checker = StableDiffusionSafetyChecker.from_pretrained("CompVis/stable-diffusion-safety-checker")
|
107 |
+
model_path = "models/models/Stable-diffusion/juggernaut_reborn.safetensors"
|
108 |
+
pipe = StableDiffusionControlNetImg2ImgPipeline.from_single_file(
|
109 |
+
model_path,
|
110 |
+
controlnet=controlnet,
|
111 |
+
torch_dtype=torch.float16,
|
112 |
+
use_safetensors=True,
|
113 |
+
safety_checker=safety_checker
|
114 |
+
)
|
115 |
+
vae = AutoencoderKL.from_single_file(
|
116 |
+
"models/VAE/vae-ft-mse-840000-ema-pruned.safetensors",
|
117 |
+
torch_dtype=torch.float16
|
118 |
+
)
|
119 |
+
pipe.vae = vae
|
120 |
+
pipe.load_textual_inversion("models/embeddings/verybadimagenegative_v1.3.pt")
|
121 |
+
pipe.load_textual_inversion("models/embeddings/JuggernautNegative-neg.pt")
|
122 |
+
pipe.load_lora_weights("models/Lora/SDXLrender_v2.0.safetensors")
|
123 |
+
pipe.fuse_lora(lora_scale=0.5)
|
124 |
+
pipe.load_lora_weights("models/Lora/more_details.safetensors")
|
125 |
+
pipe.fuse_lora(lora_scale=1.)
|
126 |
+
pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
|
127 |
+
pipe.enable_freeu(s1=0.9, s2=0.2, b1=1.3, b2=1.4)
|
128 |
+
return pipe
|
129 |
+
|
130 |
+
def __call__(self, *args, **kwargs):
|
131 |
+
return self.pipe(*args, **kwargs)
|
132 |
+
|
133 |
+
class LazyRealESRGAN:
|
134 |
+
def __init__(self, device, scale):
|
135 |
+
self.device = device
|
136 |
+
self.scale = scale
|
137 |
+
self.model = None
|
138 |
+
|
139 |
+
def load_model(self):
|
140 |
+
if self.model is None:
|
141 |
+
self.model = RealESRGAN(self.device, scale=self.scale)
|
142 |
+
self.model.load_weights(f'models/upscalers/RealESRGAN_x{self.scale}.pth', download=False)
|
143 |
+
def predict(self, img):
|
144 |
+
self.load_model()
|
145 |
+
return self.model.predict(img)
|
146 |
+
|
147 |
+
lazy_realesrgan_x2 = LazyRealESRGAN(device, scale=2)
|
148 |
+
lazy_realesrgan_x4 = LazyRealESRGAN(device, scale=4)
|
149 |
+
|
150 |
+
@timer_func
|
151 |
+
def resize_and_upscale(input_image, resolution):
|
152 |
+
scale = 2 if resolution <= 2048 else 4
|
153 |
+
input_image = input_image.convert("RGB")
|
154 |
+
W, H = input_image.size
|
155 |
+
k = float(resolution) / min(H, W)
|
156 |
+
H = int(round(H * k / 64.0)) * 64
|
157 |
+
W = int(round(W * k / 64.0)) * 64
|
158 |
+
img = input_image.resize((W, H), resample=Image.LANCZOS)
|
159 |
+
if scale == 2:
|
160 |
+
img = lazy_realesrgan_x2.predict(img)
|
161 |
+
else:
|
162 |
+
img = lazy_realesrgan_x4.predict(img)
|
163 |
+
return img
|
164 |
+
|
165 |
+
@timer_func
|
166 |
+
def create_hdr_effect(original_image, hdr):
|
167 |
+
if hdr == 0:
|
168 |
+
return original_image
|
169 |
+
cv_original = cv2.cvtColor(np.array(original_image), cv2.COLOR_RGB2BGR)
|
170 |
+
factors = [1.0 - 0.9 * hdr, 1.0 - 0.7 * hdr, 1.0 - 0.45 * hdr,
|
171 |
+
1.0 - 0.25 * hdr, 1.0, 1.0 + 0.2 * hdr,
|
172 |
+
1.0 + 0.4 * hdr, 1.0 + 0.6 * hdr, 1.0 + 0.8 * hdr]
|
173 |
+
images = [cv2.convertScaleAbs(cv_original, alpha=factor) for factor in factors]
|
174 |
+
merge_mertens = cv2.createMergeMertens()
|
175 |
+
hdr_image = merge_mertens.process(images)
|
176 |
+
hdr_image_8bit = np.clip(hdr_image * 255, 0, 255).astype('uint8')
|
177 |
+
return Image.fromarray(cv2.cvtColor(hdr_image_8bit, cv2.COLOR_BGR2RGB))
|
178 |
+
|
179 |
+
lazy_pipe = LazyLoadPipeline()
|
180 |
+
lazy_pipe.load()
|
181 |
+
|
182 |
+
def prepare_image(input_image, resolution, hdr):
|
183 |
+
condition_image = resize_and_upscale(input_image, resolution)
|
184 |
+
condition_image = create_hdr_effect(condition_image, hdr)
|
185 |
+
return condition_image
|
186 |
+
|
187 |
+
@spaces.GPU
|
188 |
+
@timer_func
|
189 |
+
def gradio_process_image(input_image, resolution, num_inference_steps, strength, hdr, guidance_scale):
|
190 |
+
print("Starting image processing...")
|
191 |
+
torch.cuda.empty_cache()
|
192 |
+
|
193 |
+
condition_image = prepare_image(input_image, resolution, hdr)
|
194 |
+
|
195 |
+
prompt = "masterpiece, best quality, highres"
|
196 |
+
negative_prompt = "low quality, normal quality, ugly, blurry, blur, lowres, bad anatomy, bad hands, cropped, worst quality, verybadimagenegative_v1.3, JuggernautNegative-neg"
|
197 |
+
|
198 |
+
options = {
|
199 |
+
"prompt": prompt,
|
200 |
+
"negative_prompt": negative_prompt,
|
201 |
+
"image": condition_image,
|
202 |
+
"control_image": condition_image,
|
203 |
+
"width": condition_image.size[0],
|
204 |
+
"height": condition_image.size[1],
|
205 |
+
"strength": strength,
|
206 |
+
"num_inference_steps": num_inference_steps,
|
207 |
+
"guidance_scale": guidance_scale,
|
208 |
+
"generator": torch.Generator(device=device).manual_seed(0),
|
209 |
+
}
|
210 |
+
|
211 |
+
print("Running inference...")
|
212 |
+
result = lazy_pipe(**options).images[0]
|
213 |
+
print("Image processing completed successfully")
|
214 |
+
|
215 |
+
# Convert input_image and result to numpy arrays
|
216 |
+
input_array = np.array(input_image)
|
217 |
+
result_array = np.array(result)
|
218 |
+
|
219 |
+
return [input_array, result_array]
|
220 |
+
|
221 |
+
|
222 |
+
|
223 |
def filter_items(
|
224 |
colors_list: Union[List, np.ndarray],
|
225 |
items_list: Union[List, np.ndarray],
|
|
|
454 |
|
455 |
|
456 |
def create_demo(model):
|
457 |
+
gr.Markdown("### Just try zeroGPU")
|
458 |
with gr.Row():
|
459 |
with gr.Column():
|
460 |
input_image = gr.Image(label="Input Image", type='pil', elem_id='img-display-input')
|
461 |
input_text = gr.Textbox(label='Prompt', placeholder='Please upload your image first', lines=2)
|
462 |
with gr.Accordion('Advanced options', open=False):
|
463 |
+
|
464 |
num_steps = gr.Slider(label='Steps',
|
465 |
minimum=1,
|
466 |
maximum=50,
|
|
|
493 |
n_prompt = gr.Textbox(
|
494 |
label='Negative Prompt',
|
495 |
value="window, door, low resolution, banner, logo, watermark, text, deformed, blurry, out of focus, surreal, ugly, beginner")
|
496 |
+
|
497 |
+
resolution = gr.Slider(minimum=256, maximum=2048, value=512, step=256, label="Resolution")
|
498 |
+
num_inference_steps = gr.Slider(minimum=1, maximum=50, value=20, step=1, label="Number of Inference Steps")
|
499 |
+
strength = gr.Slider(minimum=0, maximum=1, value=0.4, step=0.01, label="Strength")
|
500 |
+
hdr = gr.Slider(minimum=0, maximum=1, value=0, step=0.1, label="HDR Effect")
|
501 |
+
guidance_scale = gr.Slider(minimum=0, maximum=20, value=3, step=0.5, label="Guidance Scale")
|
502 |
+
|
503 |
submit = gr.Button("Submit")
|
504 |
|
505 |
with gr.Column():
|
|
|
521 |
inputs=[input_image, input_text], cache_examples=False)
|
522 |
|
523 |
|
524 |
+
|
525 |
controlnet_depth= ControlNetModel.from_pretrained(
|
526 |
"controlnet_depth", torch_dtype=dtype, use_safetensors=True)
|
527 |
controlnet_seg = ControlNetModel.from_pretrained(
|
|
|
549 |
depth_estimator = depth_estimator.to(device)
|
550 |
|
551 |
|
552 |
+
|
553 |
def main():
|
554 |
model = ControlNetDepthDesignModelMulti()
|
555 |
print('Models uploaded successfully')
|
556 |
|
557 |
+
title = "# Just try zeroGPU"
|
558 |
description = """
|
559 |
+
For test only
|
560 |
"""
|
561 |
with gr.Blocks() as demo:
|
562 |
gr.Markdown(title)
|
563 |
gr.Markdown(description)
|
|
|
564 |
|
565 |
|
566 |
demo.queue().launch(share=False)
|