File size: 5,721 Bytes
fa2d678
 
 
5a8c0f6
 
fa2d678
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a8c0f6
fa2d678
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a8c0f6
fa2d678
5a8c0f6
fa2d678
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a8c0f6
fa2d678
 
 
 
5a8c0f6
 
 
 
 
fa2d678
 
 
5a8c0f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa2d678
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a8c0f6
fa2d678
 
5a8c0f6
fa2d678
 
 
5a8c0f6
fa2d678
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200

import gradio as gr
from PIL import Image
from gradio_imageslider import ImageSlider

import requests
import base64
import numpy as np
import random
import io

URL = "http://localhost:5000/predictions"
HEADERS = {
    "Content-Type": "application/json",
}

MAX_SEED = np.iinfo(np.int32).max


def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
    if randomize_seed:
        seed = random.randint(0, MAX_SEED)
    return seed


def generate(
    input_image: Image,
    prompt: str,
    negative_prompt: str = "",
    seed: int = 0,
    width: int = 1024,
    height: int = 1024,
    prior_num_inference_steps: int = 30,
    # prior_timesteps: List[float] = None,
    prior_guidance_scale: float = 4.0,
    decoder_num_inference_steps: int = 12,
    # decoder_timesteps: List[float] = None,
    decoder_guidance_scale: float = 0.0,
    num_images_per_prompt: int = 2,

) -> Image:
    payload = {
        "input": {
            "hdr": 0,
            "image": "http://localhost:7860/file=" + input_image,
            "steps": 20,
            "prompt": prompt,
            "scheduler": "DDIM",
            "creativity": 0.25,
            "guess_mode": False,
            "resolution": "original",
            "resemblance": 0.75,
            "guidance_scale": 7,
            "negative_prompt": "teeth, tooth, open mouth, longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, mutant"
        }
    }
    response = requests.post(URL, headers=HEADERS, json=payload)
    json_response = response.json()
    if 'output' in json_response:
        base64_image = json_response["output"][0]
        image_data = base64.b64decode(
            base64_image.replace("data:image/png;base64,", ""))
        image_stream = io.BytesIO(image_data)
        return [Image.open(input_image), Image.open(image_stream)]
    raise gr.Error(json_response["status"])


examples = [
    ["An astronaut riding a green horse", "examples/image2.png"],
    ["A mecha robot in a favela by Tarsila do Amaral", "examples/image2.png"],
    ["The sprirt of a Tamagotchi wandering in the city of Los Angeles",
        "examples/image1.png"],
    ["A delicious feijoada ramen dish", "examples/image0.png"],
]

with gr.Blocks() as demo:
    with gr.Row():
        with gr.Column():
            input_image = gr.Image(type="filepath")
            with gr.Group():
                with gr.Row():
                    prompt = gr.Text(
                        label="Prompt",
                        show_label=False,
                        max_lines=1,
                        placeholder="Enter your prompt",
                        container=False,
                    )
                    run_button = gr.Button("Run", scale=0)
        with gr.Column():
            result = ImageSlider(label="Result", type="pil")
    with gr.Accordion("Advanced options", open=False):
        negative_prompt = gr.Text(
            label="Negative prompt",
            max_lines=1,
            placeholder="Enter a Negative Prompt",
        )

        seed = gr.Slider(
            label="Seed",
            minimum=0,
            maximum=MAX_SEED,
            step=1,
            value=0,
        )
        randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
        with gr.Row():
            width = gr.Slider(
                label="Width",
                minimum=1024,
                maximum=1024,
                step=512,
                value=1024,
            )
            height = gr.Slider(
                label="Height",
                minimum=1024,
                maximum=1024,
                step=512,
                value=1024,
            )
            num_images_per_prompt = gr.Slider(
                label="Number of Images",
                minimum=1,
                maximum=2,
                step=1,
                value=1,
            )
        with gr.Row():
            prior_guidance_scale = gr.Slider(
                label="Prior Guidance Scale",
                minimum=0,
                maximum=20,
                step=0.1,
                value=4.0,
            )
            prior_num_inference_steps = gr.Slider(
                label="Prior Inference Steps",
                minimum=10,
                maximum=30,
                step=1,
                value=20,
            )

            decoder_guidance_scale = gr.Slider(
                label="Decoder Guidance Scale",
                minimum=0,
                maximum=0,
                step=0.1,
                value=0.0,
            )
            decoder_num_inference_steps = gr.Slider(
                label="Decoder Inference Steps",
                minimum=4,
                maximum=12,
                step=1,
                value=10,
            )

    gr.Examples(
        examples=examples,
        inputs=[prompt, input_image],
        outputs=result,
        fn=generate,
        cache_examples=True,
    )

    inputs = [
        input_image,
        prompt,
        negative_prompt,
        seed,
        width,
        height,
        prior_num_inference_steps,
        # prior_timesteps,
        prior_guidance_scale,
        decoder_num_inference_steps,
        # decoder_timesteps,
        decoder_guidance_scale,
        num_images_per_prompt,
    ]
    gr.on(
        triggers=[prompt.submit, negative_prompt.submit, run_button.click],
        fn=randomize_seed_fn,
        inputs=[seed, randomize_seed],
        outputs=seed,
        queue=False,
        api_name=False,
    ).then(
        fn=generate,
        inputs=inputs,
        outputs=result,
        api_name="run",
    )


if __name__ == "__main__":
    demo.queue(max_size=20).launch()