Spaces:
Running
on
Zero
Running
on
Zero
import spaces | |
import gradio as gr | |
import time | |
import torch | |
from PIL import Image | |
from segment_utils import( | |
segment_image, | |
restore_result, | |
) | |
from diffusers import ( | |
StableDiffusionAdapterPipeline, | |
DiffusionPipeline, | |
T2IAdapter, | |
MultiAdapter, | |
) | |
from controlnet_aux import ( | |
LineartDetector, | |
CannyDetector, | |
PidiNetDetector, | |
MidasDetector, | |
) | |
BASE_MODEL = "stable-diffusion-v1-5/stable-diffusion-v1-5" | |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu" | |
DEFAULT_EDIT_PROMPT = "blue hair" | |
DEFAULT_NEGATIVE_PROMPT = "worst quality, normal quality, low quality, low res, blurry, text, watermark, logo, banner, extra digits, cropped, jpeg artifacts, signature, username, error, sketch ,duplicate, ugly, monochrome, horror, geometry, mutation, disgusting, poorly drawn face, bad face, fused face, ugly face, worst face, asymmetrical, unrealistic skin texture, bad proportions, out of frame, poorly drawn hands, cloned face, double face" | |
DEFAULT_CATEGORY = "hair" | |
lineart_detector = LineartDetector.from_pretrained("lllyasviel/Annotators") | |
lineart_detector = lineart_detector.to(DEVICE) | |
pidinet_detector = PidiNetDetector.from_pretrained("lllyasviel/Annotators") | |
pidinet_detector = pidinet_detector.to(DEVICE) | |
canndy_detector = CannyDetector() | |
midas_detector = MidasDetector.from_pretrained( | |
"valhalla/t2iadapter-aux-models", filename="dpt_large_384.pt", model_type="dpt_large" | |
) | |
midas_detector = midas_detector.to(DEVICE) | |
adapters = MultiAdapter( | |
[ | |
T2IAdapter.from_pretrained( | |
"TencentARC/t2i-adapter-lineart-sdxl-1.0", | |
torch_dtype=torch.float16, | |
varient="fp16", | |
), | |
T2IAdapter.from_pretrained( | |
"TencentARC/t2iadapter_canny_sd15v2", | |
torch_dtype=torch.float16, | |
varient="fp16", | |
) | |
] | |
) | |
adapters = adapters.to(torch.float16) | |
basepipeline = StableDiffusionAdapterPipeline.from_pretrained( | |
BASE_MODEL, | |
torch_dtype=torch.float16, | |
use_safetensors=True, | |
adapter=adapters, | |
) | |
basepipeline = basepipeline.to(DEVICE) | |
basepipeline.enable_model_cpu_offload() | |
def image_to_image( | |
input_image: Image, | |
edit_prompt: str, | |
seed: int, | |
num_steps: int, | |
guidance_scale: float, | |
generate_size: int, | |
lineart_scale: float = 1.0, | |
canny_scale: float = 0.5, | |
): | |
run_task_time = 0 | |
time_cost_str = '' | |
run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str) | |
lineart_image = lineart_detector(input_image, int(generate_size*0.375), generate_size) | |
run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str) | |
canny_image = canndy_detector(input_image, int(generate_size*0.375), generate_size) | |
run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str) | |
cond_image = [lineart_image, canny_image] | |
cond_scale = [lineart_scale, canny_scale] | |
generator = torch.Generator(device=DEVICE).manual_seed(seed) | |
generated_image = basepipeline( | |
generator=generator, | |
prompt=edit_prompt, | |
negative_prompt=DEFAULT_NEGATIVE_PROMPT, | |
image=input_image, | |
height=generate_size, | |
width=generate_size, | |
guidance_scale=guidance_scale, | |
num_inference_steps=num_steps, | |
adapter_image=cond_image, | |
adapter_conditioning_scale=cond_scale, | |
).images[0] | |
run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str) | |
return generated_image, time_cost_str | |
def get_time_cost(run_task_time, time_cost_str): | |
now_time = int(time.time()*1000) | |
if run_task_time == 0: | |
time_cost_str = 'start' | |
else: | |
if time_cost_str != '': | |
time_cost_str += f'-->' | |
time_cost_str += f'{now_time - run_task_time}' | |
run_task_time = now_time | |
return run_task_time, time_cost_str | |
def create_demo() -> gr.Blocks: | |
with gr.Blocks() as demo: | |
croper = gr.State() | |
with gr.Row(): | |
with gr.Column(): | |
edit_prompt = gr.Textbox(lines=1, label="Edit Prompt", value=DEFAULT_EDIT_PROMPT) | |
generate_size = gr.Number(label="Generate Size", value=1024) | |
seed = gr.Number(label="Seed", value=8) | |
category = gr.Textbox(label="Category", value=DEFAULT_CATEGORY, visible=False) | |
with gr.Column(): | |
num_steps = gr.Slider(minimum=1, maximum=100, value=5, step=1, label="Num Steps") | |
guidance_scale = gr.Slider(minimum=0, maximum=30, value=2.5, step=0.5, label="Guidance Scale") | |
mask_expansion = gr.Number(label="Mask Expansion", value=20, visible=True) | |
with gr.Column(): | |
mask_dilation = gr.Slider(minimum=0, maximum=10, value=2, step=1, label="Mask Dilation") | |
lineart_scale = gr.Slider(minimum=0, maximum=5, value=1, step=0.1, label="Lineart Scale") | |
canny_scale = gr.Slider(minimum=0, maximum=5, value=0.7, step=0.1, label="Canny Scale") | |
g_btn = gr.Button("Edit Image") | |
with gr.Row(): | |
with gr.Column(): | |
input_image = gr.Image(label="Input Image", type="pil") | |
with gr.Column(): | |
restored_image = gr.Image(label="Restored Image", type="pil", interactive=False) | |
with gr.Column(): | |
origin_area_image = gr.Image(label="Origin Area Image", type="pil", interactive=False) | |
generated_image = gr.Image(label="Generated Image", type="pil", interactive=False) | |
generated_cost = gr.Textbox(label="Time cost by step (ms):", visible=True, interactive=False) | |
g_btn.click( | |
fn=segment_image, | |
inputs=[input_image, category, generate_size, mask_expansion, mask_dilation], | |
outputs=[origin_area_image, croper], | |
).success( | |
fn=image_to_image, | |
inputs=[origin_area_image, edit_prompt,seed, num_steps, guidance_scale, generate_size, lineart_scale, canny_scale], | |
outputs=[generated_image, generated_cost], | |
).success( | |
fn=restore_result, | |
inputs=[croper, category, generated_image], | |
outputs=[restored_image], | |
) | |
return demo |