Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,16 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
# from diffusers import UniPCMultistepScheduler
|
| 4 |
import gradio as gr
|
| 5 |
-
|
|
|
|
| 6 |
import base64
|
|
|
|
| 7 |
from io import BytesIO
|
| 8 |
from PIL import Image, ImageFilter
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
| 11 |
|
| 12 |
canvas_html = '<pose-maker/>'
|
| 13 |
load_js = """
|
|
@@ -32,27 +35,24 @@ async (canvas, prompt) => {
|
|
| 32 |
}
|
| 33 |
"""
|
| 34 |
|
| 35 |
-
#
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
# # This command loads the individual model components on GPU on-demand. So, we don't
|
| 45 |
-
# # need to explicitly call pipe.to("cuda").
|
| 46 |
-
# pipe.enable_model_cpu_offload()
|
| 47 |
|
| 48 |
-
#
|
| 49 |
-
# pipe.
|
|
|
|
| 50 |
|
| 51 |
-
#
|
| 52 |
-
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
|
| 57 |
def get_canny_filter(image):
|
| 58 |
if not isinstance(image, np.ndarray):
|
|
@@ -72,17 +72,17 @@ def generate_images(canvas, prompt):
|
|
| 72 |
'RGB').resize((512, 512))
|
| 73 |
input_img = input_img.filter(ImageFilter.GaussianBlur(radius=2))
|
| 74 |
input_img = get_canny_filter(input_img)
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
return all_outputs
|
| 87 |
except Exception as e:
|
| 88 |
raise gr.Error(str(e))
|
|
|
|
| 1 |
+
from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
|
| 2 |
+
from diffusers import UniPCMultistepScheduler
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
+
import numpy as np
|
| 5 |
+
import torch
|
| 6 |
import base64
|
| 7 |
+
import cv2
|
| 8 |
from io import BytesIO
|
| 9 |
from PIL import Image, ImageFilter
|
| 10 |
+
|
| 11 |
+
# Constants
|
| 12 |
+
low_threshold = 100
|
| 13 |
+
high_threshold = 200
|
| 14 |
|
| 15 |
canvas_html = '<pose-maker/>'
|
| 16 |
load_js = """
|
|
|
|
| 35 |
}
|
| 36 |
"""
|
| 37 |
|
| 38 |
+
# Models
|
| 39 |
+
controlnet = ControlNetModel.from_pretrained(
|
| 40 |
+
"lllyasviel/sd-controlnet-canny", torch_dtype=torch.float16
|
| 41 |
+
)
|
| 42 |
+
pipe = StableDiffusionControlNetPipeline.from_pretrained(
|
| 43 |
+
"runwayml/stable-diffusion-v1-5", controlnet=controlnet, safety_checker=None, torch_dtype=torch.float16
|
| 44 |
+
)
|
| 45 |
+
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
# This command loads the individual model components on GPU on-demand. So, we don't
|
| 48 |
+
# need to explicitly call pipe.to("cuda").
|
| 49 |
+
pipe.enable_model_cpu_offload()
|
| 50 |
|
| 51 |
+
# xformers
|
| 52 |
+
pipe.enable_xformers_memory_efficient_attention()
|
| 53 |
|
| 54 |
+
# Generator seed,
|
| 55 |
+
generator = torch.manual_seed(0)
|
| 56 |
|
| 57 |
def get_canny_filter(image):
|
| 58 |
if not isinstance(image, np.ndarray):
|
|
|
|
| 72 |
'RGB').resize((512, 512))
|
| 73 |
input_img = input_img.filter(ImageFilter.GaussianBlur(radius=2))
|
| 74 |
input_img = get_canny_filter(input_img)
|
| 75 |
+
output = pipe(
|
| 76 |
+
f'{prompt}, best quality, extremely detailed',
|
| 77 |
+
input_img,
|
| 78 |
+
generator=generator,
|
| 79 |
+
num_images_per_prompt=2,
|
| 80 |
+
num_inference_steps=20,
|
| 81 |
+
negative_prompt="longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality",
|
| 82 |
+
)
|
| 83 |
+
all_outputs = [input_img]
|
| 84 |
+
for image in output.images:
|
| 85 |
+
all_outputs.append(image)
|
| 86 |
return all_outputs
|
| 87 |
except Exception as e:
|
| 88 |
raise gr.Error(str(e))
|