|
| 1 | +# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +# Copyright 2023 The HuggingFace Team. All rights reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +import random |
| 17 | + |
| 18 | +import gradio as gr |
| 19 | +import paddle |
| 20 | +from annotator.mlsd import MLSDdetector |
| 21 | +from annotator.util import HWC3, resize_image |
| 22 | + |
| 23 | +from paddlenlp.trainer import set_seed as seed_everything |
| 24 | +from ppdiffusers import ControlNetModel, StableDiffusionControlNetPipeline |
| 25 | + |
| 26 | +apply_mlsd = MLSDdetector() |
| 27 | + |
| 28 | +controlnet = ControlNetModel.from_pretrained("F:/Paddle/model/control_v11p_sd15_mlsd") |
| 29 | +pipe = StableDiffusionControlNetPipeline.from_pretrained( |
| 30 | + "runwayml/stable-diffusion-v1-5", controlnet=controlnet, safety_checker=None |
| 31 | +) |
| 32 | + |
| 33 | + |
| 34 | +def process( |
| 35 | + input_image, |
| 36 | + prompt, |
| 37 | + a_prompt, |
| 38 | + n_prompt, |
| 39 | + num_samples, |
| 40 | + image_resolution, |
| 41 | + ddim_steps, |
| 42 | + guess_mode, |
| 43 | + strength, |
| 44 | + scale, |
| 45 | + seed, |
| 46 | + eta, |
| 47 | + value_threshold, |
| 48 | + distance_threshold, |
| 49 | +): |
| 50 | + with paddle.no_grad(): |
| 51 | + img = resize_image(HWC3(input_image), image_resolution) |
| 52 | + H, W, C = img.shape |
| 53 | + detected_map = apply_mlsd(img, value_threshold, distance_threshold) |
| 54 | + detected_map = HWC3(detected_map) |
| 55 | + |
| 56 | + control = paddle.to_tensor(detected_map.copy(), dtype=paddle.float32) / 255.0 |
| 57 | + control = control.unsqueeze(0).transpose([0, 3, 1, 2]) |
| 58 | + |
| 59 | + control_scales = ( |
| 60 | + [strength * (0.825 ** float(12 - i)) for i in range(13)] if guess_mode else ([strength] * 13) |
| 61 | + ) # Magic number. IDK why. Perhaps because 0.825**12<0.01 but 0.826**12>0.01 |
| 62 | + if seed == -1: |
| 63 | + seed = random.randint(0, 65535) |
| 64 | + seed_everything(seed) |
| 65 | + results = [] |
| 66 | + for _ in range(num_samples): |
| 67 | + img = pipe( |
| 68 | + prompt + ", " + a_prompt, |
| 69 | + negative_prompt=n_prompt, |
| 70 | + image=control, |
| 71 | + num_inference_steps=ddim_steps, |
| 72 | + height=H, |
| 73 | + width=W, |
| 74 | + eta=eta, |
| 75 | + controlnet_conditioning_scale=control_scales, |
| 76 | + guidance_scale=scale, |
| 77 | + ).images[0] |
| 78 | + results.append(img) |
| 79 | + |
| 80 | + return [detected_map] + results |
| 81 | + |
| 82 | + |
| 83 | +block = gr.Blocks().queue() |
| 84 | +with block: |
| 85 | + with gr.Row(): |
| 86 | + gr.Markdown("## Control Stable Diffusion with MLSD Lines") |
| 87 | + with gr.Row(): |
| 88 | + with gr.Column(): |
| 89 | + input_image = gr.Image(source="upload", type="numpy") |
| 90 | + prompt = gr.Textbox(label="Prompt") |
| 91 | + run_button = gr.Button(label="Run") |
| 92 | + with gr.Accordion("Advanced options", open=False): |
| 93 | + num_samples = gr.Slider(label="Images", minimum=1, maximum=12, value=1, step=1) |
| 94 | + image_resolution = gr.Slider(label="Image Resolution", minimum=256, maximum=768, value=512, step=64) |
| 95 | + strength = gr.Slider(label="Control Strength", minimum=0.0, maximum=2.0, value=1.0, step=0.01) |
| 96 | + guess_mode = gr.Checkbox(label="Guess Mode", value=False) |
| 97 | + value_threshold = gr.Slider( |
| 98 | + label="Hough value threshold (MLSD)", minimum=0.01, maximum=2.0, value=0.1, step=0.01 |
| 99 | + ) |
| 100 | + distance_threshold = gr.Slider( |
| 101 | + label="Hough ditance threshold (MLSD)", minimum=0.01, maximum=20.0, value=0.1, step=0.01 |
| 102 | + ) |
| 103 | + ddim_steps = gr.Slider(label="Steps", minimum=1, maximum=100, value=20, step=1) |
| 104 | + scale = gr.Slider(label="Guidance Scale", minimum=0.1, maximum=30.0, value=9.0, step=0.1) |
| 105 | + seed = gr.Slider(label="Seed", minimum=-1, maximum=2147483647, step=1, randomize=True) |
| 106 | + eta = gr.Number(label="eta (DDIM)", value=0.0) |
| 107 | + a_prompt = gr.Textbox(label="Added Prompt", value="best quality, extremely detailed") |
| 108 | + n_prompt = gr.Textbox( |
| 109 | + label="Negative Prompt", |
| 110 | + value="longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality", |
| 111 | + ) |
| 112 | + with gr.Column(): |
| 113 | + result_gallery = gr.Gallery(label="Output", show_label=False, elem_id="gallery").style( |
| 114 | + grid=2, height="auto" |
| 115 | + ) |
| 116 | + ips = [ |
| 117 | + input_image, |
| 118 | + prompt, |
| 119 | + a_prompt, |
| 120 | + n_prompt, |
| 121 | + num_samples, |
| 122 | + image_resolution, |
| 123 | + ddim_steps, |
| 124 | + guess_mode, |
| 125 | + strength, |
| 126 | + scale, |
| 127 | + seed, |
| 128 | + eta, |
| 129 | + value_threshold, |
| 130 | + distance_threshold, |
| 131 | + ] |
| 132 | + run_button.click(fn=process, inputs=ips, outputs=[result_gallery]) |
| 133 | + |
| 134 | +block.launch(server_name="0.0.0.0", server_port=8513) |
0 commit comments