Spaces:
Runtime error
Runtime error
mikonvergence
commited on
Commit
·
1c74133
1
Parent(s):
669f764
Another layout update
Browse files
app.py
CHANGED
@@ -58,99 +58,83 @@ if torch.cuda.is_available():
|
|
58 |
def get_guide(image):
|
59 |
return hed(image,scribble=True)
|
60 |
|
61 |
-
def generate(image,
|
62 |
-
prompt,
|
63 |
-
num_steps,
|
64 |
-
text_scale,
|
65 |
-
sketch_scale,
|
66 |
-
seed):
|
67 |
-
|
68 |
-
sketch=(255*(image['mask'][...,:3]>0)).astype(CURRENT_IMAGE['image'].dtype)
|
69 |
-
mask=CURRENT_IMAGE['mask']
|
70 |
-
|
71 |
-
CURRENT_IMAGE['guide']=(CURRENT_IMAGE['guide']*(mask==0) + sketch*(mask!=0)).astype(CURRENT_IMAGE['image'].dtype)
|
72 |
-
|
73 |
-
mask_img=255*CURRENT_IMAGE['mask'].astype(CURRENT_IMAGE['image'].dtype)
|
74 |
-
|
75 |
-
new_image = pipe(
|
76 |
-
prompt,
|
77 |
-
num_inference_steps=num_steps,
|
78 |
-
guidance_scale=text_scale,
|
79 |
-
generator=torch.manual_seed(seed),
|
80 |
-
image=Image.fromarray(CURRENT_IMAGE['image']),
|
81 |
-
control_image=Image.fromarray(CURRENT_IMAGE['guide']),
|
82 |
-
controlnet_conditioning_scale=sketch_scale,
|
83 |
-
mask_image=Image.fromarray(mask_img)
|
84 |
-
).images#[0]
|
85 |
-
|
86 |
-
return new_image
|
87 |
-
|
88 |
def create_demo(max_images=12, default_num_images=3):
|
89 |
with gr.Blocks(theme=gr.themes.Default(font=[gr.themes.GoogleFont("IBM Plex Mono"), "ui-monospace","monospace"])) as demo:
|
90 |
-
|
91 |
-
|
92 |
-
with gr.
|
93 |
-
gr.Markdown('
|
94 |
gr.Markdown('1. Upload your image below')
|
95 |
gr.Markdown('2. **Draw the mask** for the region you want changed (Cut ✂️)')
|
96 |
-
input_image = gr.Image(source='upload',
|
97 |
-
shape=[HEIGHT,WIDTH],
|
98 |
-
type='numpy',
|
99 |
-
label='Mask Draw (Cut!)',
|
100 |
-
tool='sketch',
|
101 |
-
brush_radius=80)
|
102 |
gr.Markdown('3. Click `Set Mask` when it is ready!')
|
103 |
-
|
104 |
-
with gr.Column(visible=False) as step_2:
|
105 |
gr.Markdown('4. Now, you can **sketch a replacement** object! (Sketch ✏️)')
|
106 |
-
sketch_image = gr.Image(source='upload',
|
107 |
-
shape=[HEIGHT,WIDTH],
|
108 |
-
type='numpy',
|
109 |
-
label='Fill Draw (Sketch!)',
|
110 |
-
tool='sketch',
|
111 |
-
brush_radius=20)
|
112 |
gr.Markdown('5. (You can also provide a **text prompt** if you want)')
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
with gr.
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
132 |
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
output_image = gr.Gallery(
|
140 |
-
label="Generated image",
|
141 |
-
show_label=False,
|
142 |
-
elem_id="gallery",
|
143 |
-
)#.style(grid=(1, 2))
|
144 |
-
|
145 |
inputs = [
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
]
|
153 |
|
|
|
154 |
def set_mask(image):
|
155 |
img=image['image'][...,:3]
|
156 |
mask=1*(image['mask'][...,:3]>0)
|
@@ -167,11 +151,46 @@ def create_demo(max_images=12, default_num_images=3):
|
|
167 |
|
168 |
vis_image=(preview/2).astype(seg_img.dtype) + seg_img * (seg_img!=255)
|
169 |
|
170 |
-
return {input_image : image['image'],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
171 |
|
172 |
-
mask_button.click(fn=set_mask, inputs=[input_image], outputs=[input_image, sketch_image,step_2])
|
173 |
-
run_button.click(fn=generate, inputs=inputs, outputs=output_image)
|
174 |
-
|
175 |
|
176 |
if __name__ == '__main__':
|
177 |
demo = create_demo()
|
|
|
58 |
def get_guide(image):
|
59 |
return hed(image,scribble=True)
|
60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
def create_demo(max_images=12, default_num_images=3):
|
62 |
with gr.Blocks(theme=gr.themes.Default(font=[gr.themes.GoogleFont("IBM Plex Mono"), "ui-monospace","monospace"])) as demo:
|
63 |
+
|
64 |
+
gr.Markdown('# Cut and Sketch ✂️▶️✏️')
|
65 |
+
with gr.Accordion('Instructions', open=False):
|
66 |
+
gr.Markdown('## Cut ✂️')
|
67 |
gr.Markdown('1. Upload your image below')
|
68 |
gr.Markdown('2. **Draw the mask** for the region you want changed (Cut ✂️)')
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
gr.Markdown('3. Click `Set Mask` when it is ready!')
|
70 |
+
gr.Markdown('## Sketch ✏️')
|
|
|
71 |
gr.Markdown('4. Now, you can **sketch a replacement** object! (Sketch ✏️)')
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
gr.Markdown('5. (You can also provide a **text prompt** if you want)')
|
73 |
+
gr.Markdown('6. 🔮 Click `Generate` when ready! ')
|
74 |
+
|
75 |
+
with gr.Row() as main_blocks:
|
76 |
+
with gr.Column() as step_1:
|
77 |
+
gr.Markdown('### Mask Input')
|
78 |
+
input_image = gr.Image(source='upload',
|
79 |
+
shape=[HEIGHT,WIDTH],
|
80 |
+
type='numpy',
|
81 |
+
elem_id="image-upload",
|
82 |
+
label='Mask Draw (Cut!)',
|
83 |
+
tool='sketch',
|
84 |
+
brush_radius=80)
|
85 |
+
mask_button = gr.Button(label='Set Mask', value='Set Mask',full_width=False)
|
86 |
+
with gr.Column(visible=False) as step_2:
|
87 |
+
gr.Markdown('### Sketch Input')
|
88 |
+
sketch_image = gr.Image(source='upload',
|
89 |
+
shape=[HEIGHT,WIDTH],
|
90 |
+
type='numpy',
|
91 |
+
label='Fill Draw (Sketch!)',
|
92 |
+
tool='sketch',
|
93 |
+
brush_radius=15)
|
94 |
+
run_button = gr.Button(label='Generate', value='Generate')
|
95 |
+
prompt = gr.Textbox(label='Prompt')
|
96 |
+
|
97 |
+
with gr.Column() as output_step:
|
98 |
+
gr.Markdown('### Output')
|
99 |
+
output_image = gr.Gallery(
|
100 |
+
label="Generated images",
|
101 |
+
show_label=False,
|
102 |
+
elem_id="gallery",
|
103 |
+
)
|
104 |
+
|
105 |
+
with gr.Accordion('Advanced options', open=False):
|
106 |
+
num_steps = gr.Slider(label='Steps',
|
107 |
+
minimum=1,
|
108 |
+
maximum=100,
|
109 |
+
value=20,
|
110 |
+
step=1)
|
111 |
+
text_scale = gr.Slider(label='Text Guidance Scale',
|
112 |
+
minimum=0.1,
|
113 |
+
maximum=30.0,
|
114 |
+
value=7.5,
|
115 |
+
step=0.1)
|
116 |
+
seed = gr.Slider(label='Seed',
|
117 |
+
minimum=-1,
|
118 |
+
maximum=2147483647,
|
119 |
+
step=1,
|
120 |
+
randomize=True)
|
121 |
|
122 |
+
sketch_scale = gr.Slider(label='Sketch Guidance Scale',
|
123 |
+
minimum=0.0,
|
124 |
+
maximum=1.0,
|
125 |
+
value=1.0,
|
126 |
+
step=0.05)
|
127 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
128 |
inputs = [
|
129 |
+
sketch_image,
|
130 |
+
prompt,
|
131 |
+
num_steps,
|
132 |
+
text_scale,
|
133 |
+
sketch_scale,
|
134 |
+
seed
|
135 |
]
|
136 |
|
137 |
+
# STEP 1: Set Mask
|
138 |
def set_mask(image):
|
139 |
img=image['image'][...,:3]
|
140 |
mask=1*(image['mask'][...,:3]>0)
|
|
|
151 |
|
152 |
vis_image=(preview/2).astype(seg_img.dtype) + seg_img * (seg_img!=255)
|
153 |
|
154 |
+
return {input_image : image['image'],
|
155 |
+
sketch_image : vis_image,
|
156 |
+
step_1: gr.update(visible=False),
|
157 |
+
step_2: gr.update(visible=True)
|
158 |
+
}
|
159 |
+
|
160 |
+
# STEP 2: Generate
|
161 |
+
def generate(image,
|
162 |
+
prompt,
|
163 |
+
num_steps,
|
164 |
+
text_scale,
|
165 |
+
sketch_scale,
|
166 |
+
seed):
|
167 |
+
|
168 |
+
sketch=(255*(image['mask'][...,:3]>0)).astype(CURRENT_IMAGE['image'].dtype)
|
169 |
+
mask=CURRENT_IMAGE['mask']
|
170 |
+
|
171 |
+
CURRENT_IMAGE['guide']=(CURRENT_IMAGE['guide']*(mask==0) + sketch*(mask!=0)).astype(CURRENT_IMAGE['image'].dtype)
|
172 |
+
|
173 |
+
mask_img=255*CURRENT_IMAGE['mask'].astype(CURRENT_IMAGE['image'].dtype)
|
174 |
+
|
175 |
+
new_image = pipe(
|
176 |
+
prompt,
|
177 |
+
num_inference_steps=num_steps,
|
178 |
+
guidance_scale=text_scale,
|
179 |
+
generator=torch.manual_seed(seed),
|
180 |
+
image=Image.fromarray(CURRENT_IMAGE['image']),
|
181 |
+
control_image=Image.fromarray(CURRENT_IMAGE['guide']),
|
182 |
+
controlnet_conditioning_scale=sketch_scale,
|
183 |
+
mask_image=Image.fromarray(mask_img)
|
184 |
+
).images#[0]
|
185 |
+
|
186 |
+
return {output_image : new_image,
|
187 |
+
step_1: gr.update(visible=True),
|
188 |
+
step_2: gr.update(visible=False)
|
189 |
+
}
|
190 |
|
191 |
+
mask_button.click(fn=set_mask, inputs=[input_image], outputs=[input_image, sketch_image, step_1,step_2])
|
192 |
+
run_button.click(fn=generate, inputs=inputs, outputs=[output_image, step_1,step_2])
|
193 |
+
return demo
|
194 |
|
195 |
if __name__ == '__main__':
|
196 |
demo = create_demo()
|