Update app.py
Browse files
app.py
CHANGED
@@ -1,56 +1,78 @@
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
import random
|
4 |
-
|
5 |
-
# import spaces #[uncomment to use ZeroGPU]
|
6 |
-
from diffusers import DiffusionPipeline
|
7 |
import torch
|
|
|
|
|
8 |
|
9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
10 |
-
model_repo_id = "stabilityai/sdxl-turbo" # Replace to the model you would like to use
|
11 |
-
|
12 |
-
if torch.cuda.is_available():
|
13 |
-
torch_dtype = torch.float16
|
14 |
-
else:
|
15 |
-
torch_dtype = torch.float32
|
16 |
|
17 |
-
|
|
|
|
|
|
|
|
|
18 |
pipe = pipe.to(device)
|
|
|
19 |
|
20 |
MAX_SEED = np.iinfo(np.int32).max
|
21 |
-
MAX_IMAGE_SIZE = 1024
|
22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
|
|
|
|
|
25 |
def infer(
|
26 |
-
prompt,
|
27 |
-
negative_prompt,
|
28 |
-
seed,
|
29 |
-
randomize_seed,
|
30 |
-
width,
|
31 |
-
height,
|
32 |
-
guidance_scale,
|
33 |
-
num_inference_steps,
|
34 |
-
progress=gr.Progress(track_tqdm=True),
|
35 |
):
|
36 |
-
|
|
|
|
|
37 |
seed = random.randint(0, MAX_SEED)
|
38 |
|
39 |
-
generator = torch.
|
40 |
|
|
|
41 |
image = pipe(
|
42 |
prompt=prompt,
|
43 |
-
|
|
|
44 |
guidance_scale=guidance_scale,
|
45 |
num_inference_steps=num_inference_steps,
|
46 |
-
|
47 |
-
|
48 |
-
|
|
|
49 |
).images[0]
|
50 |
|
51 |
return image, seed
|
52 |
|
53 |
-
|
54 |
examples = [
|
55 |
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
56 |
"An astronaut riding a green horse",
|
@@ -64,6 +86,7 @@ css = """
|
|
64 |
}
|
65 |
"""
|
66 |
|
|
|
67 |
with gr.Blocks(css=css) as demo:
|
68 |
with gr.Column(elem_id="col-container"):
|
69 |
gr.Markdown(" # Text-to-Image Gradio Template")
|
@@ -76,19 +99,11 @@ with gr.Blocks(css=css) as demo:
|
|
76 |
placeholder="Enter your prompt",
|
77 |
container=False,
|
78 |
)
|
79 |
-
|
80 |
run_button = gr.Button("Run", scale=0, variant="primary")
|
81 |
|
82 |
result = gr.Image(label="Result", show_label=False)
|
83 |
|
84 |
with gr.Accordion("Advanced Settings", open=False):
|
85 |
-
negative_prompt = gr.Text(
|
86 |
-
label="Negative prompt",
|
87 |
-
max_lines=1,
|
88 |
-
placeholder="Enter a negative prompt",
|
89 |
-
visible=False,
|
90 |
-
)
|
91 |
-
|
92 |
seed = gr.Slider(
|
93 |
label="Seed",
|
94 |
minimum=0,
|
@@ -99,30 +114,13 @@ with gr.Blocks(css=css) as demo:
|
|
99 |
|
100 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
101 |
|
102 |
-
with gr.Row():
|
103 |
-
width = gr.Slider(
|
104 |
-
label="Width",
|
105 |
-
minimum=256,
|
106 |
-
maximum=MAX_IMAGE_SIZE,
|
107 |
-
step=32,
|
108 |
-
value=1024, # Replace with defaults that work for your model
|
109 |
-
)
|
110 |
-
|
111 |
-
height = gr.Slider(
|
112 |
-
label="Height",
|
113 |
-
minimum=256,
|
114 |
-
maximum=MAX_IMAGE_SIZE,
|
115 |
-
step=32,
|
116 |
-
value=1024, # Replace with defaults that work for your model
|
117 |
-
)
|
118 |
-
|
119 |
with gr.Row():
|
120 |
guidance_scale = gr.Slider(
|
121 |
label="Guidance scale",
|
122 |
minimum=0.0,
|
123 |
maximum=10.0,
|
124 |
step=0.1,
|
125 |
-
value=
|
126 |
)
|
127 |
|
128 |
num_inference_steps = gr.Slider(
|
@@ -130,22 +128,19 @@ with gr.Blocks(css=css) as demo:
|
|
130 |
minimum=1,
|
131 |
maximum=50,
|
132 |
step=1,
|
133 |
-
value=
|
134 |
)
|
135 |
|
136 |
gr.Examples(examples=examples, inputs=[prompt])
|
137 |
-
|
138 |
-
|
139 |
fn=infer,
|
140 |
inputs=[
|
141 |
prompt,
|
142 |
-
negative_prompt,
|
143 |
-
seed,
|
144 |
-
randomize_seed,
|
145 |
-
width,
|
146 |
-
height,
|
147 |
guidance_scale,
|
148 |
num_inference_steps,
|
|
|
|
|
149 |
],
|
150 |
outputs=[result, seed],
|
151 |
)
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
import random
|
4 |
+
from PIL import Image
|
|
|
|
|
5 |
import torch
|
6 |
+
from diffusers import ControlNetModel, UniPCMultistepScheduler
|
7 |
+
from hico_pipeline import StableDiffusionControlNetMultiLayoutPipeline
|
8 |
|
9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
+
# Initialize model
|
12 |
+
controlnet = ControlNetModel.from_pretrained("qihoo360/HiCo_T2I", torch_dtype=torch.float16)
|
13 |
+
pipe = StableDiffusionControlNetMultiLayoutPipeline.from_pretrained(
|
14 |
+
"krnl/realisticVisionV51_v51VAE", controlnet=[controlnet], torch_dtype=torch.float16
|
15 |
+
)
|
16 |
pipe = pipe.to(device)
|
17 |
+
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
|
18 |
|
19 |
MAX_SEED = np.iinfo(np.int32).max
|
|
|
20 |
|
21 |
+
# Function for generating dummy bounding box and label data
|
22 |
+
def generate_dummy_data():
|
23 |
+
# Generate random image size
|
24 |
+
img_width, img_height = 512, 512
|
25 |
+
r_image = np.zeros((img_height, img_width, 3), dtype=np.uint8)
|
26 |
+
|
27 |
+
# Generate random bounding boxes and labels
|
28 |
+
num_objects = random.randint(1, 5)
|
29 |
+
r_obj_bbox = []
|
30 |
+
r_obj_class = ["Object"]
|
31 |
+
list_cond_image = []
|
32 |
+
|
33 |
+
for _ in range(num_objects):
|
34 |
+
x1, y1 = random.randint(0, img_width // 2), random.randint(0, img_height // 2)
|
35 |
+
x2, y2 = random.randint(x1, img_width), random.randint(y1, img_height)
|
36 |
+
r_obj_bbox.append([x1, y1, x2, y2])
|
37 |
+
cond_image = np.zeros_like(r_image, dtype=np.uint8)
|
38 |
+
cond_image[y1:y2, x1:x2] = 255
|
39 |
+
list_cond_image.append(cond_image)
|
40 |
+
|
41 |
+
r_obj_bbox.insert(0, [0, 0, img_width, img_height]) # Add background
|
42 |
+
r_obj_class.insert(0, "Background")
|
43 |
+
list_cond_image.insert(0, np.zeros_like(r_image, dtype=np.uint8)) # Add full background
|
44 |
+
|
45 |
+
obj_cond_image = np.stack(list_cond_image, axis=0)
|
46 |
+
list_cond_image_pil = [Image.fromarray(img).convert('RGB') for img in list_cond_image]
|
47 |
|
48 |
+
return r_obj_class, r_obj_bbox, list_cond_image_pil, obj_cond_image
|
49 |
+
|
50 |
+
# Inference function
|
51 |
def infer(
|
52 |
+
prompt, guidance_scale, num_inference_steps, randomize_seed, seed=None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
):
|
54 |
+
# Generate dummy data for demonstration
|
55 |
+
r_obj_class, r_obj_bbox, list_cond_image_pil, _ = generate_dummy_data()
|
56 |
+
if randomize_seed or seed is None:
|
57 |
seed = random.randint(0, MAX_SEED)
|
58 |
|
59 |
+
generator = torch.manual_seed(seed)
|
60 |
|
61 |
+
# Run inference
|
62 |
image = pipe(
|
63 |
prompt=prompt,
|
64 |
+
layo_prompt=r_obj_class,
|
65 |
+
guess_mode=False,
|
66 |
guidance_scale=guidance_scale,
|
67 |
num_inference_steps=num_inference_steps,
|
68 |
+
image=list_cond_image_pil,
|
69 |
+
fuse_type="avg",
|
70 |
+
width=512,
|
71 |
+
height=512
|
72 |
).images[0]
|
73 |
|
74 |
return image, seed
|
75 |
|
|
|
76 |
examples = [
|
77 |
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
78 |
"An astronaut riding a green horse",
|
|
|
86 |
}
|
87 |
"""
|
88 |
|
89 |
+
# Gradio UI
|
90 |
with gr.Blocks(css=css) as demo:
|
91 |
with gr.Column(elem_id="col-container"):
|
92 |
gr.Markdown(" # Text-to-Image Gradio Template")
|
|
|
99 |
placeholder="Enter your prompt",
|
100 |
container=False,
|
101 |
)
|
|
|
102 |
run_button = gr.Button("Run", scale=0, variant="primary")
|
103 |
|
104 |
result = gr.Image(label="Result", show_label=False)
|
105 |
|
106 |
with gr.Accordion("Advanced Settings", open=False):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
seed = gr.Slider(
|
108 |
label="Seed",
|
109 |
minimum=0,
|
|
|
114 |
|
115 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
116 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
with gr.Row():
|
118 |
guidance_scale = gr.Slider(
|
119 |
label="Guidance scale",
|
120 |
minimum=0.0,
|
121 |
maximum=10.0,
|
122 |
step=0.1,
|
123 |
+
value=7.5,
|
124 |
)
|
125 |
|
126 |
num_inference_steps = gr.Slider(
|
|
|
128 |
minimum=1,
|
129 |
maximum=50,
|
130 |
step=1,
|
131 |
+
value=50,
|
132 |
)
|
133 |
|
134 |
gr.Examples(examples=examples, inputs=[prompt])
|
135 |
+
|
136 |
+
run_button.click(
|
137 |
fn=infer,
|
138 |
inputs=[
|
139 |
prompt,
|
|
|
|
|
|
|
|
|
|
|
140 |
guidance_scale,
|
141 |
num_inference_steps,
|
142 |
+
randomize_seed,
|
143 |
+
seed,
|
144 |
],
|
145 |
outputs=[result, seed],
|
146 |
)
|