|
from PIL import Image |
|
import gradio as gr |
|
from diffusers import StableDiffusionControlNetPipeline, ControlNetModel, UniPCMultistepScheduler |
|
import torch |
|
torch.backends.cuda.matmul.allow_tf32 = True |
|
|
|
controlnet = ControlNetModel.from_pretrained("JFoz/dog-cat-pose", torch_dtype=torch.float16) |
|
|
|
pipe = StableDiffusionControlNetPipeline.from_pretrained( |
|
"runwayml/stable-diffusion-v1-5", |
|
controlnet=controlnet, |
|
torch_dtype=torch.float16, |
|
safety_checker=None, |
|
) |
|
|
|
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config) |
|
|
|
pipe.enable_xformers_memory_efficient_attention() |
|
pipe.enable_model_cpu_offload() |
|
pipe.enable_attention_slicing() |
|
|
|
def infer( |
|
prompt, |
|
negative_prompt, |
|
conditioning_image, |
|
num_inference_steps=30, |
|
size=768, |
|
guidance_scale=7.0, |
|
seed=1234, |
|
): |
|
|
|
conditioning_image_raw = Image.fromarray(conditioning_image) |
|
|
|
|
|
g_cpu = torch.Generator() |
|
|
|
if seed == -1: |
|
generator = g_cpu.manual_seed(g_cpu.seed()) |
|
else: |
|
generator = g_cpu.manual_seed(seed) |
|
|
|
output_image = pipe( |
|
prompt, |
|
conditioning_image, |
|
height=size, |
|
width=size, |
|
num_inference_steps=num_inference_steps, |
|
generator=generator, |
|
negative_prompt=negative_prompt, |
|
guidance_scale=guidance_scale, |
|
controlnet_conditioning_scale=1.0, |
|
).images[0] |
|
|
|
|
|
|
|
|
|
return output_image |
|
|
|
with gr.Blocks(theme=gr.themes.Default(font=[gr.themes.GoogleFont("Inconsolata"), "Arial", "sans-serif"])) as demo: |
|
gr.Markdown( |
|
""" |
|
# Animal Pose Control Net |
|
# This is a demo of Animal Pose Control Net, which is a model trained on runwayml/stable-diffusion-v1-5 with new type of conditioning. |
|
""") |
|
|
|
with gr.Row(): |
|
with gr.Column(): |
|
prompt = gr.Textbox( |
|
label="Prompt", |
|
) |
|
negative_prompt = gr.Textbox( |
|
label="Negative Prompt", |
|
) |
|
conditioning_image = gr.Image( |
|
label="Conditioning Image", |
|
) |
|
with gr.Accordion('Advanced options', open=False): |
|
with gr.Row(): |
|
num_inference_steps = gr.Slider( |
|
10, 40, 20, |
|
step=1, |
|
label="Steps", |
|
) |
|
size = gr.Slider( |
|
256, 768, 512, |
|
step=128, |
|
label="Size", |
|
) |
|
with gr.Row(): |
|
guidance_scale = gr.Slider( |
|
label='Guidance Scale', |
|
minimum=0.1, |
|
maximum=30.0, |
|
value=7.0, |
|
step=0.1 |
|
) |
|
seed = gr.Slider( |
|
label='Seed', |
|
value=-1, |
|
minimum=-1, |
|
maximum=2147483647, |
|
step=1, |
|
|
|
) |
|
submit_btn = gr.Button( |
|
value="Submit", |
|
variant="primary" |
|
) |
|
with gr.Column(min_width=300): |
|
output = gr.Image( |
|
label="Result", |
|
) |
|
|
|
submit_btn.click( |
|
fn=infer, |
|
inputs=[ |
|
prompt, negative_prompt, conditioning_image, num_inference_steps, size, guidance_scale, seed |
|
|
|
], |
|
outputs=output |
|
) |
|
gr.Examples( |
|
examples=[ |
|
|
|
|
|
["a tortoiseshell cat is sitting on a cushion", "https://huggingface.co/JFoz/dog-cat-pose/blob/main/images_0.png"], |
|
["a yellow dog standing on a lawn", "https://huggingface.co/JFoz/dog-cat-pose/blob/main/images_1.png"], |
|
], |
|
inputs=[ |
|
|
|
prompt |
|
], |
|
outputs=output, |
|
fn=infer, |
|
cache_examples=True, |
|
) |
|
gr.Markdown( |
|
""" |
|
* [Dataset](https://huggingface.co/datasets/JFoz/dog-poses-controlnet-dataset) |
|
* [Diffusers model](), [Web UI model](https://huggingface.co/JFoz/dog-pose) |
|
* [Training Report](https://wandb.ai/john-fozard/dog-cat-pose/runs/kmwcvae5)) |
|
""") |
|
|
|
|
|
demo.launch() |