Update app.py
Browse files
app.py
CHANGED
|
@@ -10,7 +10,8 @@ from PIL import Image
|
|
| 10 |
import spaces
|
| 11 |
import torch
|
| 12 |
from diffusers import (
|
| 13 |
-
StableDiffusionXLPipeline,
|
|
|
|
| 14 |
DPMSolverMultistepScheduler
|
| 15 |
)
|
| 16 |
DESCRIPTION = """
|
|
@@ -34,6 +35,20 @@ pipe = StableDiffusionXLPipeline.from_pretrained(
|
|
| 34 |
)
|
| 35 |
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
|
| 36 |
pipe.to('cuda')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
def save_image(img):
|
| 39 |
unique_name = str(uuid.uuid4()) + ".png"
|
|
@@ -49,6 +64,11 @@ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
|
| 49 |
|
| 50 |
@spaces.GPU(durarion=50, enable_queue=True)
|
| 51 |
def generate(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
prompt: str,
|
| 53 |
negative_prompt: str = "",
|
| 54 |
use_negative_prompt: bool = False,
|
|
@@ -65,16 +85,32 @@ def generate(
|
|
| 65 |
|
| 66 |
if not use_negative_prompt:
|
| 67 |
negative_prompt = "" # type: ignore
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
image_paths = [save_image(img) for img in images]
|
| 80 |
print(image_paths)
|
|
@@ -106,6 +142,25 @@ with gr.Blocks(title="Visionix Playground", css=css) as demo:
|
|
| 106 |
elem_id="duplicate-button",
|
| 107 |
visible=False,
|
| 108 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
with gr.Group():
|
| 110 |
with gr.Row():
|
| 111 |
prompt = gr.Text(
|
|
@@ -175,7 +230,13 @@ with gr.Blocks(title="Visionix Playground", css=css) as demo:
|
|
| 175 |
outputs=negative_prompt,
|
| 176 |
api_name=False,
|
| 177 |
)
|
| 178 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
|
| 180 |
gr.on(
|
| 181 |
triggers=[
|
|
|
|
| 10 |
import spaces
|
| 11 |
import torch
|
| 12 |
from diffusers import (
|
| 13 |
+
StableDiffusionXLPipeline,
|
| 14 |
+
StableDiffusionXLInpaintPipeline,
|
| 15 |
DPMSolverMultistepScheduler
|
| 16 |
)
|
| 17 |
DESCRIPTION = """
|
|
|
|
| 35 |
)
|
| 36 |
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
|
| 37 |
pipe.to('cuda')
|
| 38 |
+
pipe_inpaint = StableDiffusionXLInpaintPipeline.from_pretrained(
|
| 39 |
+
"ehristoforu/Visionix-alpha-inpainting",
|
| 40 |
+
torch_dtype=torch.float16,
|
| 41 |
+
use_safetensors=True,
|
| 42 |
+
)
|
| 43 |
+
pipe_inpaint.scheduler = DPMSolverMultistepScheduler.from_config(pipe_inpaint.scheduler.config)
|
| 44 |
+
pipe_inpaint.to('cuda')
|
| 45 |
+
|
| 46 |
+
def get_model(model):
|
| 47 |
+
if model == "Alpha inpaint":
|
| 48 |
+
return gr.update(visible=True), gr.update(visible=True), gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
|
| 49 |
+
else:
|
| 50 |
+
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
| 51 |
+
|
| 52 |
|
| 53 |
def save_image(img):
|
| 54 |
unique_name = str(uuid.uuid4()) + ".png"
|
|
|
|
| 64 |
|
| 65 |
@spaces.GPU(durarion=50, enable_queue=True)
|
| 66 |
def generate(
|
| 67 |
+
model,
|
| 68 |
+
inpaint_image,
|
| 69 |
+
mask_image,
|
| 70 |
+
blur_factor,
|
| 71 |
+
strength,
|
| 72 |
prompt: str,
|
| 73 |
negative_prompt: str = "",
|
| 74 |
use_negative_prompt: bool = False,
|
|
|
|
| 85 |
|
| 86 |
if not use_negative_prompt:
|
| 87 |
negative_prompt = "" # type: ignore
|
| 88 |
+
if model == "Alpha":
|
| 89 |
+
images = pipe(
|
| 90 |
+
prompt=prompt,
|
| 91 |
+
negative_prompt=f"{negative_prompt}",
|
| 92 |
+
width=width,
|
| 93 |
+
height=height,
|
| 94 |
+
guidance_scale=guidance_scale,
|
| 95 |
+
num_inference_steps=25,
|
| 96 |
+
num_images_per_prompt=1,
|
| 97 |
+
output_type="pil",
|
| 98 |
+
).images
|
| 99 |
+
elif model == "Alpha inpaint":
|
| 100 |
+
blurred_mask = pipe_inpaint.mask_processor.blur(mask_image, blur_factor=blur_factor)
|
| 101 |
+
images = pipe_inpaint(
|
| 102 |
+
prompt=prompt,
|
| 103 |
+
image=inpaint_image,
|
| 104 |
+
mask_image=blurred_mask,
|
| 105 |
+
negative_prompt=negative_prompt,
|
| 106 |
+
width=width,
|
| 107 |
+
height=height,
|
| 108 |
+
guidance_scale=guidance_scale,
|
| 109 |
+
num_inference_steps=25,
|
| 110 |
+
strength=strength,
|
| 111 |
+
num_images_per_prompt=1,
|
| 112 |
+
output_type="pil",
|
| 113 |
+
).images
|
| 114 |
|
| 115 |
image_paths = [save_image(img) for img in images]
|
| 116 |
print(image_paths)
|
|
|
|
| 142 |
elem_id="duplicate-button",
|
| 143 |
visible=False,
|
| 144 |
)
|
| 145 |
+
|
| 146 |
+
with gr.Row():
|
| 147 |
+
model = gr.Radio(
|
| 148 |
+
label="Model",
|
| 149 |
+
choices=["Alpha", "Alpha inpaint"],
|
| 150 |
+
value="Alpha",
|
| 151 |
+
interactive=True,
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
md_mask = gr.Markdown("""
|
| 155 |
+
⚠️ To generate an inpaint mask, go [here](https://huggingface.co/spaces/stevhliu/inpaint-mask-maker).
|
| 156 |
+
""", visible=False)
|
| 157 |
+
inpaint_image = gr.Image(label="Inpaint Image", interactive=True, scale=5, visible=False, type="pil")
|
| 158 |
+
mask_image = gr.Image(label="Mask Image", interactive=True, scale=5, visible=False, type="pil")
|
| 159 |
+
|
| 160 |
+
blur_factor = gr.Slider(label="Mask Blur Factor", minimum=0, maximum=100, value=4, step=1, interactive=True, visible=False)
|
| 161 |
+
strength = gr.Slider(label="Denoising Strength", minimum=0.00, maximum=1.00, value=0.70, step=0.01, interactive=True, visible=False)
|
| 162 |
+
|
| 163 |
+
|
| 164 |
with gr.Group():
|
| 165 |
with gr.Row():
|
| 166 |
prompt = gr.Text(
|
|
|
|
| 230 |
outputs=negative_prompt,
|
| 231 |
api_name=False,
|
| 232 |
)
|
| 233 |
+
|
| 234 |
+
model.change(
|
| 235 |
+
fn=get_model,
|
| 236 |
+
inputs=model,
|
| 237 |
+
outputs=[md_mask, inpaint_image, mask_image, blur_factor, strength],
|
| 238 |
+
api_name=False,
|
| 239 |
+
)
|
| 240 |
|
| 241 |
gr.on(
|
| 242 |
triggers=[
|