File size: 5,100 Bytes
ff0aba3
 
 
 
 
 
 
 
 
 
 
 
7688f40
ff0aba3
 
7688f40
 
23086f0
ff0aba3
 
 
 
 
 
 
 
 
 
7688f40
ff0aba3
5c43d2c
ff0aba3
c22253e
ff0aba3
 
 
 
ff1a4e2
ff0aba3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23086f0
ff0aba3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import spaces
import gradio as gr
import time
import torch

from PIL import Image
from segment_utils import(
    segment_image_withmask,
    restore_result,
)
from diffusers import (
    DiffusionPipeline,
    AutoencoderKL,
)

vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)

BASE_MODEL = "RunDiffusion/Juggernaut-XL-v9"

DEVICE = "cuda" if torch.cuda.is_available() else "cpu"

DEFAULT_EDIT_PROMPT = "a woman with linen-blonde-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"

basepipeline = DiffusionPipeline.from_pretrained(
    BASE_MODEL,
    vae=vae,
    torch_dtype=torch.float16,
    # use_safetensors=True,
    custom_pipeline="./pipelines/masked_stable_diffusion_xl_img2img.py",
    variant="fp16",
)

basepipeline = basepipeline.to(DEVICE)

basepipeline.enable_model_cpu_offload()

@spaces.GPU(duration=30)
def image_to_image(
    input_image: Image,
    mask_image: Image,
    edit_prompt: str,
    seed: int,
    num_steps: int,
    guidance_scale: float,
    generate_size: int,
    blur: int,
    strength: float,
):
    run_task_time = 0
    time_cost_str = ''
    run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str)

    generator = torch.Generator(device=DEVICE).manual_seed(seed)
    generated_image = basepipeline(
        generator=generator,
        prompt=edit_prompt,
        negative_prompt=DEFAULT_NEGATIVE_PROMPT,
        original_image=input_image,
        mask=mask_image,
        guidance_scale=guidance_scale,
        num_inference_steps=num_steps,
        blur=blur,
        strength=strength,
    ).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=512)
            with gr.Column():
                num_steps = gr.Slider(minimum=1, maximum=100, value=20, step=1, label="Num Steps")
                guidance_scale = gr.Slider(minimum=0, maximum=30, value=5, step=0.5, label="Guidance Scale")
            with gr.Column():
                with gr.Accordion("Advanced Options", open=False):
                    blur = gr.Slider(minimum=0, maximum=100, value=48, step=1, label="Blur")
                    strength = gr.Slider(minimum=0, maximum=1, value=0.9, step=0.1, label="Strength")
                    mask_dilation = gr.Slider(minimum=0, maximum=10, value=2, step=1, label="Mask Dilation")
                    mask_expansion = gr.Number(label="Mask Expansion", value=50, visible=True)
                    seed = gr.Number(label="Seed", value=8)
                    category = gr.Textbox(label="Category", value=DEFAULT_CATEGORY, visible=False)
                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)
                mask_image = gr.Image(label="Mask Image", type="pil", interactive=False)
        
        g_btn.click(
            fn=segment_image_withmask,
            inputs=[input_image, category, generate_size, mask_expansion, mask_dilation],
            outputs=[origin_area_image, mask_image, croper],
        ).success(
            fn=image_to_image,
            inputs=[origin_area_image, mask_image, edit_prompt,seed, num_steps, guidance_scale, generate_size, blur, strength],
            outputs=[generated_image, generated_cost],
        ).success(
            fn=restore_result,
            inputs=[croper, category, generated_image],
            outputs=[restored_image],
        )

    return demo