Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -9,15 +9,13 @@ import torch
|
|
9 |
from PIL import Image
|
10 |
from diffusers import FluxInpaintPipeline
|
11 |
|
12 |
-
torch.cuda.empty_cache()
|
13 |
-
|
14 |
MARKDOWN = """
|
15 |
-
# FLUX Inpainting
|
16 |
-
|
17 |
"""
|
18 |
|
19 |
MAX_SEED = np.iinfo(np.int32).max
|
20 |
-
IMAGE_SIZE =
|
21 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
22 |
|
23 |
|
@@ -36,35 +34,35 @@ def remove_background(image: Image.Image, threshold: int = 50) -> Image.Image:
|
|
36 |
return image
|
37 |
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
|
66 |
pipe = FluxInpaintPipeline.from_pretrained(
|
67 |
-
"black-forest-labs/FLUX.1-
|
68 |
|
69 |
|
70 |
def resize_image_dimensions(
|
@@ -124,18 +122,17 @@ def process(
|
|
124 |
if randomize_seed_checkbox:
|
125 |
seed_slicer = random.randint(0, MAX_SEED)
|
126 |
generator = torch.Generator().manual_seed(seed_slicer)
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
torch.cuda.empty_cache()
|
139 |
return result, resized_mask
|
140 |
|
141 |
|
@@ -177,9 +174,9 @@ with gr.Blocks() as demo:
|
|
177 |
with gr.Row():
|
178 |
strength_slider_component = gr.Slider(
|
179 |
label="Strength",
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
minimum=0,
|
184 |
maximum=1,
|
185 |
step=0.01,
|
@@ -188,10 +185,10 @@ with gr.Blocks() as demo:
|
|
188 |
|
189 |
num_inference_steps_slider_component = gr.Slider(
|
190 |
label="Number of inference steps",
|
191 |
-
|
192 |
-
|
193 |
minimum=1,
|
194 |
-
maximum=
|
195 |
step=1,
|
196 |
value=20,
|
197 |
)
|
@@ -201,25 +198,25 @@ with gr.Blocks() as demo:
|
|
201 |
with gr.Accordion("Debug", open=False):
|
202 |
output_mask_component = gr.Image(
|
203 |
type='pil', image_mode='RGB', label='Input mask', format="png")
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
|
224 |
submit_button_component.click(
|
225 |
fn=process,
|
@@ -237,4 +234,4 @@ with gr.Blocks() as demo:
|
|
237 |
]
|
238 |
)
|
239 |
|
240 |
-
demo.launch(debug=False, show_error=True)
|
|
|
9 |
from PIL import Image
|
10 |
from diffusers import FluxInpaintPipeline
|
11 |
|
|
|
|
|
12 |
MARKDOWN = """
|
13 |
+
# FLUX.1 Inpainting using
|
14 |
+
[Black Forest Labs](https://huggingface.co/black-forest-labs)'s FLUX.1-schnell
|
15 |
"""
|
16 |
|
17 |
MAX_SEED = np.iinfo(np.int32).max
|
18 |
+
IMAGE_SIZE = 1024
|
19 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
20 |
|
21 |
|
|
|
34 |
return image
|
35 |
|
36 |
|
37 |
+
EXAMPLES = [
|
38 |
+
[
|
39 |
+
{
|
40 |
+
"background": Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-image.png", stream=True).raw),
|
41 |
+
"layers": [remove_background(Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-mask-2.png", stream=True).raw))],
|
42 |
+
"composite": Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-composite-2.png", stream=True).raw),
|
43 |
+
},
|
44 |
+
"little lion",
|
45 |
+
42,
|
46 |
+
False,
|
47 |
+
0.85,
|
48 |
+
30
|
49 |
+
],
|
50 |
+
[
|
51 |
+
{
|
52 |
+
"background": Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-image.png", stream=True).raw),
|
53 |
+
"layers": [remove_background(Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-mask-3.png", stream=True).raw))],
|
54 |
+
"composite": Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-composite-3.png", stream=True).raw),
|
55 |
+
},
|
56 |
+
"tribal tattoos",
|
57 |
+
42,
|
58 |
+
False,
|
59 |
+
0.85,
|
60 |
+
30
|
61 |
+
]
|
62 |
+
]
|
63 |
|
64 |
pipe = FluxInpaintPipeline.from_pretrained(
|
65 |
+
"black-forest-labs/FLUX.1-dev", torch_dtype=torch.bfloat16).to(DEVICE)
|
66 |
|
67 |
|
68 |
def resize_image_dimensions(
|
|
|
122 |
if randomize_seed_checkbox:
|
123 |
seed_slicer = random.randint(0, MAX_SEED)
|
124 |
generator = torch.Generator().manual_seed(seed_slicer)
|
125 |
+
result = pipe(
|
126 |
+
prompt=input_text,
|
127 |
+
image=resized_image,
|
128 |
+
mask_image=resized_mask,
|
129 |
+
width=width,
|
130 |
+
height=height,
|
131 |
+
strength=strength_slider,
|
132 |
+
generator=generator,
|
133 |
+
num_inference_steps=num_inference_steps_slider
|
134 |
+
).images[0]
|
135 |
+
print('INFERENCE DONE')
|
|
|
136 |
return result, resized_mask
|
137 |
|
138 |
|
|
|
174 |
with gr.Row():
|
175 |
strength_slider_component = gr.Slider(
|
176 |
label="Strength",
|
177 |
+
info="Indicates extent to transform the reference `image`. "
|
178 |
+
"Must be between 0 and 1. `image` is used as a starting "
|
179 |
+
"point and more noise is added the higher the `strength`.",
|
180 |
minimum=0,
|
181 |
maximum=1,
|
182 |
step=0.01,
|
|
|
185 |
|
186 |
num_inference_steps_slider_component = gr.Slider(
|
187 |
label="Number of inference steps",
|
188 |
+
info="The number of denoising steps. More denoising steps "
|
189 |
+
"usually lead to a higher quality image at the",
|
190 |
minimum=1,
|
191 |
+
maximum=50,
|
192 |
step=1,
|
193 |
value=20,
|
194 |
)
|
|
|
198 |
with gr.Accordion("Debug", open=False):
|
199 |
output_mask_component = gr.Image(
|
200 |
type='pil', image_mode='RGB', label='Input mask', format="png")
|
201 |
+
with gr.Row():
|
202 |
+
gr.Examples(
|
203 |
+
fn=process,
|
204 |
+
examples=EXAMPLES,
|
205 |
+
inputs=[
|
206 |
+
input_image_editor_component,
|
207 |
+
input_text_component,
|
208 |
+
seed_slicer_component,
|
209 |
+
randomize_seed_checkbox_component,
|
210 |
+
strength_slider_component,
|
211 |
+
num_inference_steps_slider_component
|
212 |
+
],
|
213 |
+
outputs=[
|
214 |
+
output_image_component,
|
215 |
+
output_mask_component
|
216 |
+
],
|
217 |
+
run_on_click=True,
|
218 |
+
cache_examples=True
|
219 |
+
)
|
220 |
|
221 |
submit_button_component.click(
|
222 |
fn=process,
|
|
|
234 |
]
|
235 |
)
|
236 |
|
237 |
+
demo.launch(debug=False, show_error=True, share=True)
|