Spaces:
Running
on
Zero
Running
on
Zero
update gradio
Browse files
app.py
CHANGED
@@ -1,146 +1,81 @@
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import numpy as np
|
3 |
-
import
|
4 |
-
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
|
|
|
8 |
|
9 |
-
|
10 |
-
|
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 |
-
|
19 |
-
|
20 |
|
21 |
-
|
|
|
|
|
|
|
22 |
|
23 |
-
if randomize_seed:
|
24 |
-
seed = random.randint(0, MAX_SEED)
|
25 |
-
|
26 |
-
generator = torch.Generator().manual_seed(seed)
|
27 |
-
|
28 |
image = pipe(
|
29 |
-
prompt
|
30 |
-
|
31 |
-
|
32 |
-
num_inference_steps
|
33 |
-
|
34 |
-
|
35 |
-
generator
|
36 |
-
).images[0]
|
37 |
-
|
38 |
-
return image
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
"""
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
# Text-to-Image Gradio Template
|
63 |
-
Currently running on {power_device}.
|
64 |
-
""")
|
65 |
-
|
66 |
-
with gr.Row():
|
67 |
-
|
68 |
-
prompt = gr.Text(
|
69 |
-
label="Prompt",
|
70 |
-
show_label=False,
|
71 |
-
max_lines=1,
|
72 |
-
placeholder="Enter your prompt",
|
73 |
-
container=False,
|
74 |
-
)
|
75 |
-
|
76 |
-
run_button = gr.Button("Run", scale=0)
|
77 |
-
|
78 |
-
result = gr.Image(label="Result", show_label=False)
|
79 |
-
|
80 |
-
with gr.Accordion("Advanced Settings", open=False):
|
81 |
-
|
82 |
-
negative_prompt = gr.Text(
|
83 |
-
label="Negative prompt",
|
84 |
-
max_lines=1,
|
85 |
-
placeholder="Enter a negative prompt",
|
86 |
-
visible=False,
|
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.Row():
|
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 |
-
height = gr.Slider(
|
110 |
-
label="Height",
|
111 |
-
minimum=256,
|
112 |
-
maximum=MAX_IMAGE_SIZE,
|
113 |
-
step=32,
|
114 |
-
value=512,
|
115 |
-
)
|
116 |
-
|
117 |
-
with gr.Row():
|
118 |
-
|
119 |
-
guidance_scale = gr.Slider(
|
120 |
-
label="Guidance scale",
|
121 |
-
minimum=0.0,
|
122 |
-
maximum=10.0,
|
123 |
-
step=0.1,
|
124 |
-
value=0.0,
|
125 |
-
)
|
126 |
-
|
127 |
-
num_inference_steps = gr.Slider(
|
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 |
-
fn = infer,
|
142 |
-
inputs = [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
143 |
-
outputs = [result]
|
144 |
-
)
|
145 |
-
|
146 |
-
demo.queue().launch()
|
|
|
1 |
+
import spaces
|
2 |
+
import torch
|
3 |
+
from diffusers import StableDiffusion3InstructPix2PixPipeline, SD3Transformer2DModel
|
4 |
import gradio as gr
|
5 |
+
import PIL.Image
|
6 |
import numpy as np
|
7 |
+
from PIL import Image, ImageOps
|
8 |
+
|
9 |
+
|
10 |
+
pipe = StableDiffusion3InstructPix2PixPipeline.from_pretrained("BleachNick/SD3_UltraEdit_w_mask", torch_dtype=torch.float16)
|
11 |
+
|
12 |
+
pipe = pipe.to("cuda")
|
13 |
+
|
14 |
+
|
15 |
+
|
16 |
+
@spaces.GPU(duration=20)
|
17 |
+
def generate(image_mask, prompt, num_inference_steps=50, image_guidance_scale=1.6, guidance_scale=7.5, seed=255):
|
18 |
+
def is_blank_mask(mask_img):
|
19 |
+
# Convert the mask to a numpy array and check if all values are 0 (black/transparent)
|
20 |
+
mask_array = np.array(mask_img.convert('L')) # Convert to luminance to simplify the check
|
21 |
+
return np.all(mask_array == 0)
|
22 |
+
# Set the seed for reproducibility
|
23 |
+
seed = int(seed)
|
24 |
+
generator = torch.manual_seed(seed)
|
25 |
|
26 |
+
img = image_mask["background"].convert("RGB")
|
27 |
+
mask_img = image_mask["layers"][0].getchannel('A').convert("RGB")
|
28 |
|
29 |
+
# Central crop to desired size
|
30 |
+
desired_size = (512, 512)
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
+
img = ImageOps.fit(img, desired_size, method=Image.LANCZOS, centering=(0.5, 0.5))
|
33 |
+
mask_img = ImageOps.fit(mask_img, desired_size, method=Image.LANCZOS, centering=(0.5, 0.5))
|
34 |
|
35 |
+
if is_blank_mask(mask_img):
|
36 |
+
# Create a mask of the same size with all values set to 255 (white)
|
37 |
+
mask_img = PIL.Image.new('RGB', img.size, color=(255, 255, 255))
|
38 |
+
mask_img = mask_img.convert('RGB')
|
39 |
|
|
|
|
|
|
|
|
|
|
|
40 |
image = pipe(
|
41 |
+
prompt,
|
42 |
+
image=img,
|
43 |
+
mask_img=mask_img,
|
44 |
+
num_inference_steps=num_inference_steps,
|
45 |
+
image_guidance_scale=image_guidance_scale,
|
46 |
+
guidance_scale=guidance_scale,
|
47 |
+
generator=generator
|
48 |
+
).images[0]
|
49 |
+
|
50 |
+
return image,mask_img
|
51 |
+
|
52 |
+
|
53 |
+
# image_mask_input = gr.ImageMask(label="Input Image", type="pil", brush_color="#000000", elem_id="inputmask",
|
54 |
+
# shape=(512, 512))
|
55 |
+
image_mask_input = gr.ImageMask(sources='upload',type="pil",label="Input Image: Mask with pen or leave unmasked",transforms=(),layers=False)
|
56 |
+
prompt_input = gr.Textbox(label="Prompt")
|
57 |
+
num_inference_steps_input = gr.Slider(minimum=0, maximum=100, value=50, label="Number of Inference Steps")
|
58 |
+
image_guidance_scale_input = gr.Slider(minimum=0.0, maximum=2.5, value=1.5, label="Image Guidance Scale")
|
59 |
+
guidance_scale_input = gr.Slider(minimum=0.0, maximum=17.5, value=12.5, label="Guidance Scale")
|
60 |
+
seed_input = gr.Textbox(value="255", label="Random Seed")
|
61 |
+
|
62 |
+
inputs = [image_mask_input, prompt_input, num_inference_steps_input, image_guidance_scale_input, guidance_scale_input,
|
63 |
+
seed_input]
|
64 |
+
outputs = gr.Image(label="Generated Image")
|
65 |
+
|
66 |
+
|
67 |
+
# Custom HTML content
|
68 |
+
article_html = """
|
69 |
+
<h2>Welcome to the Image Generation Interface</h2>
|
70 |
+
<p>This interface allows you to generate images based on a given mask and prompt. Use the sliders to adjust the inference steps and guidance scales, and provide a seed for reproducibility.</p>
|
71 |
"""
|
72 |
|
73 |
+
demo = gr.Interface(
|
74 |
+
fn=generate,
|
75 |
+
inputs=inputs,
|
76 |
+
outputs=outputs,
|
77 |
+
article=article_html # Add article parameter
|
78 |
+
)
|
79 |
+
|
80 |
+
demo.queue().launch()
|
81 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|