Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,6 +12,7 @@ Run:
|
|
| 12 |
python app.py
|
| 13 |
"""
|
| 14 |
|
|
|
|
| 15 |
import os
|
| 16 |
import torch
|
| 17 |
import gradio as gr
|
|
@@ -19,27 +20,21 @@ from diffusers import StableDiffusionXLInpaintPipeline
|
|
| 19 |
from PIL import Image
|
| 20 |
|
| 21 |
MODEL_ID = "ShinoharaHare/Waifu-Inpaint-XL"
|
| 22 |
-
|
| 23 |
-
DTYPE = torch.float16 if DEVICE == "cuda" else torch.float32
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
)
|
| 36 |
-
pipe.to(DEVICE)
|
| 37 |
-
if DEVICE == "cuda":
|
| 38 |
-
pipe.enable_vae_slicing()
|
| 39 |
-
pipe.enable_attention_slicing()
|
| 40 |
-
return pipe
|
| 41 |
|
| 42 |
|
|
|
|
| 43 |
def run_inpaint(
|
| 44 |
editor_value, # gr.ImageEditor output: {"background":..., "layers":[...], "composite":...}
|
| 45 |
prompt,
|
|
@@ -60,13 +55,11 @@ def run_inpaint(
|
|
| 60 |
mask_layer = editor_value["layers"][0]
|
| 61 |
mask = mask_layer.split()[-1].convert("L") # alpha channel -> grayscale mask
|
| 62 |
|
| 63 |
-
p = load_pipeline()
|
| 64 |
-
|
| 65 |
results = []
|
| 66 |
base_seed = int(seed) if seed >= 0 else torch.seed()
|
| 67 |
for i in range(int(num_variations)):
|
| 68 |
-
gen = torch.Generator(device=
|
| 69 |
-
out =
|
| 70 |
prompt=prompt,
|
| 71 |
negative_prompt=negative_prompt or None,
|
| 72 |
image=base_image,
|
|
@@ -112,5 +105,5 @@ with gr.Blocks(title="Waifu-Inpaint-XL") as demo:
|
|
| 112 |
)
|
| 113 |
|
| 114 |
if __name__ == "__main__":
|
| 115 |
-
#
|
| 116 |
-
demo.launch(
|
|
|
|
| 12 |
python app.py
|
| 13 |
"""
|
| 14 |
|
| 15 |
+
import spaces # MUST be imported before torch/anything CUDA-related, ZeroGPU requirement
|
| 16 |
import os
|
| 17 |
import torch
|
| 18 |
import gradio as gr
|
|
|
|
| 20 |
from PIL import Image
|
| 21 |
|
| 22 |
MODEL_ID = "ShinoharaHare/Waifu-Inpaint-XL"
|
| 23 |
+
DTYPE = torch.float16
|
|
|
|
| 24 |
|
| 25 |
+
# Load once at startup. Moving to 'cuda' here is fine under ZeroGPU -- the actual
|
| 26 |
+
# GPU device is only allocated when a @spaces.GPU-decorated function is called.
|
| 27 |
+
pipe = StableDiffusionXLInpaintPipeline.from_pretrained(
|
| 28 |
+
MODEL_ID,
|
| 29 |
+
torch_dtype=DTYPE,
|
| 30 |
+
use_safetensors=True,
|
| 31 |
+
)
|
| 32 |
+
pipe.to("cuda")
|
| 33 |
+
pipe.enable_vae_slicing()
|
| 34 |
+
pipe.enable_attention_slicing()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
|
| 37 |
+
@spaces.GPU(duration=60) # seconds of GPU time requested per call; raise if you increase steps/variations
|
| 38 |
def run_inpaint(
|
| 39 |
editor_value, # gr.ImageEditor output: {"background":..., "layers":[...], "composite":...}
|
| 40 |
prompt,
|
|
|
|
| 55 |
mask_layer = editor_value["layers"][0]
|
| 56 |
mask = mask_layer.split()[-1].convert("L") # alpha channel -> grayscale mask
|
| 57 |
|
|
|
|
|
|
|
| 58 |
results = []
|
| 59 |
base_seed = int(seed) if seed >= 0 else torch.seed()
|
| 60 |
for i in range(int(num_variations)):
|
| 61 |
+
gen = torch.Generator(device="cuda").manual_seed(base_seed + i)
|
| 62 |
+
out = pipe(
|
| 63 |
prompt=prompt,
|
| 64 |
negative_prompt=negative_prompt or None,
|
| 65 |
image=base_image,
|
|
|
|
| 105 |
)
|
| 106 |
|
| 107 |
if __name__ == "__main__":
|
| 108 |
+
# Spaces already serves a public URL -- do NOT pass share=True here (errors on Spaces).
|
| 109 |
+
demo.launch()
|