Spaces:
Running
on
Zero
Running
on
Zero
init
Browse files- README.md +1 -1
- app.py +129 -121
- requirements.txt +7 -1
README.md
CHANGED
@@ -4,7 +4,7 @@ emoji: 🖼
|
|
4 |
colorFrom: purple
|
5 |
colorTo: red
|
6 |
sdk: gradio
|
7 |
-
sdk_version: 4.
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: apache-2.0
|
|
|
4 |
colorFrom: purple
|
5 |
colorTo: red
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 4.29
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: apache-2.0
|
app.py
CHANGED
@@ -1,146 +1,154 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
import random
|
4 |
from diffusers import DiffusionPipeline
|
5 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
-
if torch.cuda.is_available():
|
10 |
-
torch.cuda.max_memory_allocated(device=device)
|
11 |
-
pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
|
12 |
-
pipe.enable_xformers_memory_efficient_attention()
|
13 |
-
pipe = pipe.to(device)
|
14 |
-
else:
|
15 |
-
pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", use_safetensors=True)
|
16 |
-
pipe = pipe.to(device)
|
17 |
|
18 |
MAX_SEED = np.iinfo(np.int32).max
|
19 |
MAX_IMAGE_SIZE = 1024
|
20 |
|
21 |
-
|
|
|
|
|
22 |
|
|
|
23 |
if randomize_seed:
|
24 |
seed = random.randint(0, MAX_SEED)
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
return image
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
#col-container {
|
48 |
-
margin: 0 auto;
|
49 |
-
max-width: 520px;
|
50 |
-
}
|
51 |
-
"""
|
52 |
-
|
53 |
-
if torch.cuda.is_available():
|
54 |
-
power_device = "GPU"
|
55 |
-
else:
|
56 |
-
power_device = "CPU"
|
57 |
-
|
58 |
-
with gr.Blocks(css=css) as demo:
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
run_button = gr.Button("Run", scale=0)
|
77 |
-
|
78 |
-
result = gr.Image(label="Result", show_label=False)
|
79 |
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
)
|
88 |
-
|
89 |
-
seed = gr.Slider(
|
90 |
-
label="Seed",
|
91 |
-
minimum=0,
|
92 |
-
maximum=MAX_SEED,
|
93 |
-
step=1,
|
94 |
-
value=0,
|
95 |
-
)
|
96 |
-
|
97 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
98 |
|
99 |
-
with gr.
|
100 |
-
|
101 |
-
width = gr.Slider(
|
102 |
-
label="Width",
|
103 |
-
minimum=256,
|
104 |
-
maximum=MAX_IMAGE_SIZE,
|
105 |
-
step=32,
|
106 |
-
value=512,
|
107 |
-
)
|
108 |
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
|
|
|
|
|
|
115 |
)
|
|
|
|
|
|
|
116 |
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
label="Number of inference steps",
|
129 |
-
minimum=1,
|
130 |
-
maximum=12,
|
131 |
-
step=1,
|
132 |
-
value=2,
|
133 |
-
)
|
134 |
-
|
135 |
-
gr.Examples(
|
136 |
-
examples = examples,
|
137 |
-
inputs = [prompt]
|
138 |
-
)
|
139 |
-
|
140 |
run_button.click(
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
145 |
|
146 |
demo.queue().launch()
|
|
|
1 |
+
import spaces
|
2 |
import gradio as gr
|
3 |
import numpy as np
|
4 |
import random
|
5 |
from diffusers import DiffusionPipeline
|
6 |
import torch
|
7 |
+
import random
|
8 |
+
from diffusers import (
|
9 |
+
ControlNetModel,
|
10 |
+
DiffusionPipeline,
|
11 |
+
StableDiffusionControlNetPipeline,
|
12 |
+
StableDiffusionXLControlNetPipeline,
|
13 |
+
UniPCMultistepScheduler,
|
14 |
+
EulerDiscreteScheduler,
|
15 |
+
AutoencoderKL
|
16 |
+
)
|
17 |
+
from transformers import DPTFeatureExtractor, DPTForDepthEstimation, DPTImageProcessor
|
18 |
+
from transformers import CLIPImageProcessor
|
19 |
+
from diffusers.utils import load_image
|
20 |
+
|
21 |
+
device = "cuda"
|
22 |
+
base_model_id = "SG161222/RealVisXL_V4.0"
|
23 |
+
controlnet_model_id = "diffusers/controlnet-depth-sdxl-1.0"
|
24 |
+
vae_model_id = "madebyollin/sdxl-vae-fp16-fix"
|
25 |
+
|
26 |
|
27 |
+
# load pipe
|
28 |
+
controlnet = ControlNetModel.from_pretrained(controlnet_model_id, variant="fp16", use_safetensors=True, torch_dtype=torch.float16)
|
29 |
+
vae = AutoencoderKL.from_pretrained(vae_model_id, torch_dtype=torch.float16)
|
30 |
+
pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
|
31 |
+
base_model_id,
|
32 |
+
controlnet=controlnet,
|
33 |
+
vae=vae,
|
34 |
+
variant="fp16",
|
35 |
+
use_safetensors=True,
|
36 |
+
torch_dtype=torch.float16,
|
37 |
+
)
|
38 |
+
pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config)
|
39 |
+
pipe.enable_model_cpu_offload()
|
40 |
+
pipe.enable_xformers_memory_efficient_attention()
|
41 |
+
pipe.to(device)
|
42 |
+
|
43 |
+
depth_estimator = DPTForDepthEstimation.from_pretrained("Intel/dpt-hybrid-midas").to("cuda")
|
44 |
+
feature_extractor = DPTImageProcessor.from_pretrained("Intel/dpt-hybrid-midas")
|
45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
MAX_SEED = np.iinfo(np.int32).max
|
48 |
MAX_IMAGE_SIZE = 1024
|
49 |
|
50 |
+
USE_TORCH_COMPILE = 0
|
51 |
+
ENABLE_CPU_OFFLOAD = 0
|
52 |
+
|
53 |
|
54 |
+
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
55 |
if randomize_seed:
|
56 |
seed = random.randint(0, MAX_SEED)
|
57 |
+
return seed
|
58 |
+
|
59 |
+
|
60 |
+
def get_depth_map(image):
|
61 |
+
image = feature_extractor(images=image, return_tensors="pt").pixel_values.to("cuda")
|
62 |
+
with torch.no_grad(), torch.autocast("cuda"):
|
63 |
+
depth_map = depth_estimator(image).predicted_depth
|
64 |
+
|
65 |
+
depth_map = torch.nn.functional.interpolate(
|
66 |
+
depth_map.unsqueeze(1),
|
67 |
+
size=(1024, 1024),
|
68 |
+
mode="bicubic",
|
69 |
+
align_corners=False,
|
70 |
+
)
|
71 |
+
depth_min = torch.amin(depth_map, dim=[1, 2, 3], keepdim=True)
|
72 |
+
depth_max = torch.amax(depth_map, dim=[1, 2, 3], keepdim=True)
|
73 |
+
depth_map = (depth_map - depth_min) / (depth_max - depth_min)
|
74 |
+
image = torch.cat([depth_map] * 3, dim=1)
|
75 |
+
image = image.permute(0, 2, 3, 1).cpu().numpy()[0]
|
76 |
+
image = Image.fromarray((image * 255.0).clip(0, 255).astype(np.uint8))
|
77 |
return image
|
78 |
|
79 |
+
|
80 |
+
|
81 |
+
@spaces.GPU(enable_queue=True)
|
82 |
+
def process(orginal_image, image_url, prompt, a_prompt, n_prompt, num_steps, guidance_scale, control_strength, seed):
|
83 |
+
|
84 |
+
if image_url:
|
85 |
+
orginal_image = load_image(image_url)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
|
87 |
+
width = 1024
|
88 |
+
height = 1024
|
89 |
+
depth_image = get_depth_map(orginal_image.resize((1024, 1024)))
|
90 |
+
generator = torch.Generator().manual_seed(seed)
|
91 |
+
generated_image = self.pipe(
|
92 |
+
prompt=prompt,
|
93 |
+
negative_prompt=n_prompt,
|
94 |
+
width=width,
|
95 |
+
height=height,
|
96 |
+
guidance_scale=guidance_scale,
|
97 |
+
num_inference_steps=num_steps,
|
98 |
+
strength=control_strength,
|
99 |
+
generator=generator,
|
100 |
+
image=depth_image,
|
101 |
+
).images[0]
|
102 |
+
return [[depth_image, generated_image], "ok"]
|
|
|
|
|
|
|
103 |
|
104 |
+
with gr.Blocks() as demo:
|
105 |
+
|
106 |
+
with gr.Row():
|
107 |
+
with gr.Column():
|
108 |
+
image = gr.Image()
|
109 |
+
image_url = gr.Textbox(label="Image Url", placeholder="Enter image URL here (optional)")
|
110 |
+
prompt = gr.Textbox(label="Prompt")
|
111 |
+
run_button = gr.Button("Run")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
|
113 |
+
with gr.Accordion("Advanced options", open=True):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
|
115 |
+
num_steps = gr.Slider(label="Number of steps", minimum=1, maximum=100, value=30, step=1)
|
116 |
+
guidance_scale = gr.Slider(label="Guidance scale", minimum=0.1, maximum=30.0, value=7.5, step=0.1)
|
117 |
+
control_strength = gr.Slider(label="Control Strength", minimum=0.1, maximum=4.0, value=0.8, step=0.1)
|
118 |
+
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
|
119 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
120 |
+
a_prompt = gr.Textbox(label="Additional prompt", value="high-quality, extremely detailed, 4K")
|
121 |
+
n_prompt = gr.Textbox(
|
122 |
+
label="Negative prompt",
|
123 |
+
value="longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality",
|
124 |
)
|
125 |
+
with gr.Column():
|
126 |
+
result = ImageSlider(label="Generate image", type="pil", slider_color="pink")
|
127 |
+
logs = gr.Textbox(label="logs")
|
128 |
|
129 |
+
inputs = [
|
130 |
+
image,
|
131 |
+
image_url,
|
132 |
+
prompt,
|
133 |
+
a_prompt,
|
134 |
+
n_prompt,
|
135 |
+
num_steps,
|
136 |
+
guidance_scale,
|
137 |
+
control_strength,
|
138 |
+
seed
|
139 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
run_button.click(
|
141 |
+
fn=randomize_seed_fn,
|
142 |
+
inputs=[seed, randomize_seed],
|
143 |
+
outputs=seed,
|
144 |
+
queue=False,
|
145 |
+
api_name=False,
|
146 |
+
).then(
|
147 |
+
fn=process,
|
148 |
+
inputs=inputs,
|
149 |
+
outputs=[result, logs],
|
150 |
+
api_name=False
|
151 |
+
)
|
152 |
+
return demo
|
153 |
|
154 |
demo.queue().launch()
|
requirements.txt
CHANGED
@@ -3,4 +3,10 @@ diffusers
|
|
3 |
invisible_watermark
|
4 |
torch
|
5 |
transformers
|
6 |
-
xformers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
invisible_watermark
|
4 |
torch
|
5 |
transformers
|
6 |
+
xformers
|
7 |
+
gradio_imageslider
|
8 |
+
requests
|
9 |
+
spaces
|
10 |
+
huggingface_hub
|
11 |
+
controlnet-aux
|
12 |
+
safetensors
|