Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
|
| 2 |
+
from diffusers import UniPCMultistepScheduler
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import torch
|
| 5 |
+
from gradio.components import Markdown
|
| 6 |
+
|
| 7 |
+
# Models
|
| 8 |
+
controlnet_pose = ControlNetModel.from_pretrained(
|
| 9 |
+
"lllyasviel/sd-controlnet-openpose", torch_dtype=torch.float16
|
| 10 |
+
)
|
| 11 |
+
controlnet_canny = ControlNetModel.from_pretrained(
|
| 12 |
+
"lllyasviel/sd-controlnet-canny", torch_dtype=torch.float16
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
pipe = StableDiffusionControlNetPipeline.from_pretrained(
|
| 16 |
+
"dreamlike-art/dreamlike-anime-1.0",
|
| 17 |
+
controlnet=[controlnet_pose, controlnet_canny],
|
| 18 |
+
safety_checker=None, torch_dtype=torch.float16
|
| 19 |
+
).to('cuda')
|
| 20 |
+
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
|
| 21 |
+
|
| 22 |
+
# This command loads the individual model components on GPU on-demand. So, we don't
|
| 23 |
+
# need to explicitly call pipe.to("cuda").
|
| 24 |
+
#pipe.enable_model_cpu_offload()
|
| 25 |
+
|
| 26 |
+
# xformers
|
| 27 |
+
pipe.enable_xformers_memory_efficient_attention()
|
| 28 |
+
|
| 29 |
+
# Generator seed,
|
| 30 |
+
generator = torch.manual_seed(1)
|
| 31 |
+
|
| 32 |
+
negative_prompt = ("worst quality, low quality, lowres, bad anatomy, bad hands, "
|
| 33 |
+
"missing fingers, extra digit, fewer digits")
|
| 34 |
+
|
| 35 |
+
markdown = """
|
| 36 |
+
## Generate controlled outputs with Mult-ControlNet and Stable Diffusion using 🤗Diffusers
|
| 37 |
+
This Space uses pose lines and canny edged image as the additional conditioning. Please refer to the "Examples" for what kind of images are appropriate.
|
| 38 |
+
The Followings are tools available to create such images. In this example, tool 1 is being used.
|
| 39 |
+
1. [Character bones that look like Openpose for blender Ver4.7 Depth+Canny](https://toyxyz.gumroad.com/l/ciojz)
|
| 40 |
+
2. [open-pose-editor](https://github.com/ZhUyU1997/open-pose-editor)
|
| 41 |
+
"""
|
| 42 |
+
# **This space using these models**:
|
| 43 |
+
# - ControlNet Model (canny): [lllyasviel/sd-controlnet-canny](https://hf.co/lllyasviel/sd-controlnet-canny)
|
| 44 |
+
# - ControlNet Model (openpose): [lllyasviel/sd-controlnet-openpose](https://hf.co/lllyasviel/sd-controlnet-openpose)
|
| 45 |
+
# - SD Base Model: [dreamlike-art/dreamlike-anime-1.0](https://hf.co/dreamlike-art/dreamlike-anime-1.0)
|
| 46 |
+
|
| 47 |
+
def generate_images(pose_image, canny_image, prompt):
|
| 48 |
+
output = pipe(
|
| 49 |
+
prompt=prompt,
|
| 50 |
+
negative_prompt=negative_prompt,
|
| 51 |
+
image=[pose_image, canny_image],
|
| 52 |
+
generator=generator,
|
| 53 |
+
num_images_per_prompt=2,
|
| 54 |
+
num_inference_steps=20,
|
| 55 |
+
)
|
| 56 |
+
all_outputs = []
|
| 57 |
+
all_outputs.append(pose_image)
|
| 58 |
+
all_outputs.append(canny_image)
|
| 59 |
+
for image in output.images:
|
| 60 |
+
all_outputs.append(image)
|
| 61 |
+
return all_outputs
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
gr.Interface(
|
| 65 |
+
generate_images,
|
| 66 |
+
inputs=[
|
| 67 |
+
gr.Image(type="pil"),
|
| 68 |
+
gr.Image(type="pil"),
|
| 69 |
+
gr.Textbox(
|
| 70 |
+
label="Enter your prompt",
|
| 71 |
+
max_lines=1,
|
| 72 |
+
placeholder="Enter your prompt",
|
| 73 |
+
),
|
| 74 |
+
],
|
| 75 |
+
outputs=gr.Gallery().style(grid=[2], height="auto"),
|
| 76 |
+
description=markdown,
|
| 77 |
+
examples=[
|
| 78 |
+
["p11_clip.png",
|
| 79 |
+
"c11_clip.png",
|
| 80 |
+
"masterpiece, a professional portrait of woman wearing white shirts, smile, heart hand sign"
|
| 81 |
+
],
|
| 82 |
+
# ["p13_clip.png",
|
| 83 |
+
# "c13_clip.png",
|
| 84 |
+
# "masterpiece, a professional portrait of woman wearing white shirts, smile"
|
| 85 |
+
# ],
|
| 86 |
+
["p12_clip.png",
|
| 87 |
+
"c12_clip.png",
|
| 88 |
+
"masterpiece, a professional portrait of woman wearing white shirts, smile"
|
| 89 |
+
],
|
| 90 |
+
],
|
| 91 |
+
allow_flagging=False,
|
| 92 |
+
).launch(enable_queue=True)
|