File size: 4,551 Bytes
0acc836
 
 
 
d46e1fe
 
 
 
 
 
0acc836
 
 
 
d46e1fe
0acc836
 
 
2355a89
 
0acc836
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
de87909
 
0acc836
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
abe2443
0acc836
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d46e1fe
0acc836
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d46e1fe
abe2443
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
130
131
132
133
134
135
import random

import gradio as gr
import PIL
import torch
from diffusers import StableDiffusionInstructPix2PixPipeline

cartoonization_id = "instruction-tuning-sd/cartoonizer"
image_proc_id = "instruction-tuning-sd/low-level-img-proc"

title = "Instruction-tuned Stable Diffusion"
description = "This Space demonstrates the instruction-tuning on Stable Diffusion. To know more, please check out the [corresponding blog post](https://hf.co/blog/instruction-tuning-sd). Some experimentation tips are available from [the original InstructPix2Pix Space](https://huggingface.co/spaces/timbrooks/instruct-pix2pix)."


def load_pipeline(id: str):
    pipeline = StableDiffusionInstructPix2PixPipeline.from_pretrained(
        id, torch_dtype=torch.float16
    ).to("cuda")
    pipeline.enable_xformers_memory_efficient_attention()
    pipeline.set_progress_bar_config(disable=True)
    return pipeline


def infer_cartoonization(
    prompt: str,
    negative_prompt: str,
    image: PIL.Image.Image,
    steps: int,
    img_cfg: float,
    text_cfg: float,
    seed: int,
):
    pipeline = load_pipeline(cartoonization_id)
    images = pipeline(
        prompt,
        image,
        negative_prompt=negative_prompt,
        num_inference_steps=int(steps),
        image_guidance_scale=img_cfg,
        guidance_scale=text_cfg,
        generator=torch.manual_seed(int(seed)),
    )
    return images


def infer_img_proc(
    prompt: str,
    negative_prompt: str,
    image: PIL.Image.Image,
    steps: int,
    img_cfg: float,
    text_cfg: float,
    seed: int,
):
    pipeline = load_pipeline(image_proc_id)
    images = pipeline(
        prompt,
        image,
        negative_prompt=negative_prompt,
        num_inference_steps=int(steps),
        image_guidance_scale=img_cfg,
        guidance_scale=text_cfg,
        generator=torch.manual_seed(int(seed)),
    )
    return images


examples = [
    ["cartoonize this image", "low quality", "examples/mountain.png", 20, 1.5, 7.5, 0],
    ["derain this image", "low quality", "examples/duck.png", 20, 1.5, 7.5, 0],
]

with gr.Blocks(theme="gradio/soft") as demo:
    gr.Markdown(f"## {title}")
    gr.Markdown(description)

    with gr.Tab("Cartoonization"):
        prompt = gr.Textbox(label="Prompt")
        neg_prompt = gr.Textbox(label="Negative Prompt")
        input_image = gr.Image(label="Input Image")
        steps = gr.Slider(minimum=5, maximum=100, step=1)
        img_cfg = gr.Number(value=1.5, label=f"Image CFG", interactive=True)
        text_cfg = gr.Number(value=7.5, label=f"Text CFG", interactive=True)
        seed = gr.Slider(minimum=0, maximum=100000, step=1)

        car_output_gallery = gr.Gallery(
            label="Generated images", show_label=False, elem_id="gallery"
        ).style(columns=[2], rows=[2], object_fit="contain", height="auto")
        submit_btn = gr.Button(value="Submit")
        all_car_inputs = [prompt, neg_prompt, input_image, steps, img_cfg, text_cfg, seed]
        submit_btn.click(
            fn=infer_cartoonization,
            inputs=all_car_inputs,
            outputs=[car_output_gallery],
        )

    with gr.Tab("Low-level image processing"):
        rompt = gr.Textbox(label="Prompt")
        neg_prompt = gr.Textbox(label="Negative Prompt")
        input_image = gr.Image(label="Input Image")
        steps = gr.Slider(minimum=5, maximum=100, step=1)
        img_cfg = gr.Number(value=1.5, label=f"Image CFG", interactive=True)
        text_cfg = gr.Number(value=7.5, label=f"Text CFG", interactive=True)
        seed = gr.Slider(minimum=0, maximum=100000, step=1)

        img_proc_output_gallery = gr.Gallery(
            label="Generated images", show_label=False, elem_id="gallery"
        ).style(columns=[2], rows=[2], object_fit="contain", height="auto")
        submit_btn = gr.Button(value="Submit")
        all_img_proc_inputs = [prompt, neg_prompt, input_image, img_cfg, text_cfg, seed]
        submit_btn.click(
            fn=infer_img_proc,
            inputs=all_img_proc_inputs,
            outputs=[img_proc_output_gallery],
        )

    gr.Markdown("### Cartoonization example")
    gr.Examples(
        [examples[0]],
        inputs=all_car_inputs,
        outputs=car_output_gallery,
        fn=infer_cartoonization,
        cache_examples=True,
    )
    gr.Markdown("### Low-level image processing example")
    gr.Examples(
        [examples[0]],
        inputs=all_img_proc_inputs,
        outputs=img_proc_output_gallery,
        fn=infer_img_proc,
        cache_examples=True,
    )

demo.launch()