Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,77 +18,7 @@ import cv2
|
|
| 18 |
import spaces
|
| 19 |
from gradio_imageslider import ImageSlider
|
| 20 |
|
| 21 |
-
|
| 22 |
-
function refresh() {
|
| 23 |
-
const url = new URL(window.location);
|
| 24 |
-
}
|
| 25 |
-
"""
|
| 26 |
-
|
| 27 |
-
def nms(x, t, s):
|
| 28 |
-
x = cv2.GaussianBlur(x.astype(np.float32), (0, 0), s)
|
| 29 |
-
|
| 30 |
-
f1 = np.array([[0, 0, 0], [1, 1, 1], [0, 0, 0]], dtype=np.uint8)
|
| 31 |
-
f2 = np.array([[0, 1, 0], [0, 1, 0], [0, 1, 0]], dtype=np.uint8)
|
| 32 |
-
f3 = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]], dtype=np.uint8)
|
| 33 |
-
f4 = np.array([[0, 0, 1], [0, 1, 0], [1, 0, 0]], dtype=np.uint8)
|
| 34 |
-
|
| 35 |
-
y = np.zeros_like(x)
|
| 36 |
-
|
| 37 |
-
for f in [f1, f2, f3, f4]:
|
| 38 |
-
np.putmask(y, cv2.dilate(x, kernel=f) == x, x)
|
| 39 |
-
|
| 40 |
-
z = np.zeros_like(y, dtype=np.uint8)
|
| 41 |
-
z[y > t] = 255
|
| 42 |
-
return z
|
| 43 |
-
|
| 44 |
-
def HWC3(x):
|
| 45 |
-
assert x.dtype == np.uint8
|
| 46 |
-
if x.ndim == 2:
|
| 47 |
-
x = x[:, :, None]
|
| 48 |
-
assert x.ndim == 3
|
| 49 |
-
H, W, C = x.shape
|
| 50 |
-
assert C == 1 or C == 3 or C == 4
|
| 51 |
-
if C == 3:
|
| 52 |
-
return x
|
| 53 |
-
if C == 1:
|
| 54 |
-
return np.concatenate([x, x, x], axis=2)
|
| 55 |
-
if C == 4:
|
| 56 |
-
color = x[:, :, 0:3].astype(np.float32)
|
| 57 |
-
alpha = x[:, :, 3:4].astype(np.float32) / 255.0
|
| 58 |
-
y = color * alpha + 255.0 * (1.0 - alpha)
|
| 59 |
-
y = y.clip(0, 255).astype(np.uint8)
|
| 60 |
-
return y
|
| 61 |
-
|
| 62 |
-
DESCRIPTION = ''''''
|
| 63 |
-
|
| 64 |
-
if not torch.cuda.is_available():
|
| 65 |
-
DESCRIPTION += ""
|
| 66 |
-
|
| 67 |
-
style_list = [
|
| 68 |
-
{
|
| 69 |
-
"name": "(No style)",
|
| 70 |
-
"prompt": "{prompt}",
|
| 71 |
-
"negative_prompt": "longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality",
|
| 72 |
-
},
|
| 73 |
-
{
|
| 74 |
-
"name": "Cinematic",
|
| 75 |
-
"prompt": "cinematic still {prompt} . emotional, harmonious, vignette, highly detailed, high budget, bokeh, cinemascope, moody, epic, gorgeous, film grain, grainy",
|
| 76 |
-
"negative_prompt": "anime, cartoon, graphic, text, painting, crayon, graphite, abstract, glitch, deformed, mutated, ugly, disfigured",
|
| 77 |
-
},
|
| 78 |
-
# ... (other styles)
|
| 79 |
-
]
|
| 80 |
-
|
| 81 |
-
styles = {k["name"]: (k["prompt"], k["negative_prompt"]) for k in style_list}
|
| 82 |
-
STYLE_NAMES = list(styles.keys())
|
| 83 |
-
DEFAULT_STYLE_NAME = "(No style)"
|
| 84 |
-
|
| 85 |
-
def apply_style(style_name: str, positive: str, negative: str = "") -> tuple[str, str]:
|
| 86 |
-
p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
|
| 87 |
-
return p.replace("{prompt}", positive), n + negative
|
| 88 |
-
|
| 89 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 90 |
-
|
| 91 |
-
eulera_scheduler = EulerAncestralDiscreteScheduler.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", subfolder="scheduler")
|
| 92 |
|
| 93 |
# Initialize all detectors
|
| 94 |
openpose_detector = OpenposeDetector.from_pretrained('lllyasviel/ControlNet')
|
|
@@ -102,13 +32,13 @@ pidi_detector = PidiNetDetector.from_pretrained('lllyasviel/Annotators')
|
|
| 102 |
normal_detector = NormalBaeDetector.from_pretrained('lllyasviel/Annotators')
|
| 103 |
sam_detector = SamDetector.from_pretrained('ybelkada/segment-anything', subfolder='checkpoints')
|
| 104 |
|
|
|
|
|
|
|
| 105 |
controlnet = ControlNetModel.from_pretrained(
|
| 106 |
"xinsir/controlnet-union-sdxl-1.0",
|
| 107 |
torch_dtype=torch.float16
|
| 108 |
)
|
| 109 |
|
| 110 |
-
vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
|
| 111 |
-
|
| 112 |
pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
|
| 113 |
"stabilityai/stable-diffusion-xl-base-1.0",
|
| 114 |
controlnet=controlnet,
|
|
@@ -118,13 +48,6 @@ pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
|
|
| 118 |
)
|
| 119 |
pipe.to(device)
|
| 120 |
|
| 121 |
-
MAX_SEED = np.iinfo(np.int32).max
|
| 122 |
-
|
| 123 |
-
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
| 124 |
-
if randomize_seed:
|
| 125 |
-
seed = random.randint(0, MAX_SEED)
|
| 126 |
-
return seed
|
| 127 |
-
|
| 128 |
@spaces.GPU
|
| 129 |
def run(
|
| 130 |
image: dict,
|
|
@@ -355,4 +278,4 @@ with gr.Blocks(css="style.css", js=js_func) as demo:
|
|
| 355 |
fn=run, inputs=inputs, outputs=outputs
|
| 356 |
)
|
| 357 |
|
| 358 |
-
demo.queue().launch
|
|
|
|
| 18 |
import spaces
|
| 19 |
from gradio_imageslider import ImageSlider
|
| 20 |
|
| 21 |
+
# ... (keep the existing helper functions like nms, HWC3, etc.)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
# Initialize all detectors
|
| 24 |
openpose_detector = OpenposeDetector.from_pretrained('lllyasviel/ControlNet')
|
|
|
|
| 32 |
normal_detector = NormalBaeDetector.from_pretrained('lllyasviel/Annotators')
|
| 33 |
sam_detector = SamDetector.from_pretrained('ybelkada/segment-anything', subfolder='checkpoints')
|
| 34 |
|
| 35 |
+
# ... (keep the existing style_list and other configurations)
|
| 36 |
+
|
| 37 |
controlnet = ControlNetModel.from_pretrained(
|
| 38 |
"xinsir/controlnet-union-sdxl-1.0",
|
| 39 |
torch_dtype=torch.float16
|
| 40 |
)
|
| 41 |
|
|
|
|
|
|
|
| 42 |
pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
|
| 43 |
"stabilityai/stable-diffusion-xl-base-1.0",
|
| 44 |
controlnet=controlnet,
|
|
|
|
| 48 |
)
|
| 49 |
pipe.to(device)
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
@spaces.GPU
|
| 52 |
def run(
|
| 53 |
image: dict,
|
|
|
|
| 278 |
fn=run, inputs=inputs, outputs=outputs
|
| 279 |
)
|
| 280 |
|
| 281 |
+
demo.queue().launch(show_error=True, ssl_verify=False)
|