File size: 6,068 Bytes
6bcacf9
 
 
 
 
 
 
 
 
 
 
 
5ebc76c
 
 
7f3be1a
f50c4e6
6bcacf9
 
 
 
 
 
 
 
7f3be1a
6bcacf9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
da7edff
 
6bcacf9
 
da7edff
6bcacf9
 
 
 
 
 
 
 
 
 
 
 
da7edff
6bcacf9
da7edff
6bcacf9
 
 
da7edff
6bcacf9
 
 
 
 
 
da7edff
 
6bcacf9
da7edff
5ebc76c
 
7f3be1a
 
f50c4e6
 
 
 
 
7f3be1a
5ebc76c
6bcacf9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d5b85e9
6bcacf9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e34a7f6
 
6bcacf9
 
 
 
 
 
 
d5b85e9
 
 
da7edff
 
 
 
 
 
 
 
 
 
 
 
 
6bcacf9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from typing import Any
import gradio as gr

from backend.models.lcmdiffusion_setting import LCMDiffusionSetting
from context import Context
from models.interface_types import InterfaceType
from app_settings import Settings
from constants import LCM_DEFAULT_MODEL, LCM_DEFAULT_MODEL_OPENVINO
from frontend.utils import is_reshape_required
from app_settings import AppSettings
from constants import DEVICE
from frontend.utils import enable_openvino_controls
from scipy.ndimage import zoom
import numpy as np
from PIL import Image
from super_image import CarnModel,ImageLoader
import cv2

random_enabled = True

context = Context(InterfaceType.WEBUI)
previous_width = 0
previous_height = 0
previous_model_id = ""
previous_num_of_images = 0
upscaler = CarnModel.from_pretrained('eugenesiow/carn-bam', scale=2) 

def generate_text_to_image(
    prompt,
    inference_steps,
    guidance_scale,
    seed,
    use_openvino,
    use_safety_checker,
) -> Any:
    global previous_height, previous_width, previous_model_id, previous_num_of_images
    model_id = LCM_DEFAULT_MODEL
    if use_openvino:
        model_id = LCM_DEFAULT_MODEL_OPENVINO

    use_seed = True if seed != -1 else False

    lcm_diffusion_settings = LCMDiffusionSetting(
        lcm_model_id=model_id,
        prompt=prompt,
        image_height=384,
        image_width=384,
        inference_steps=inference_steps,
        guidance_scale=guidance_scale,
        number_of_images=1,
        seed=seed,
        use_openvino=use_openvino,
        use_safety_checker=use_safety_checker,
        use_seed=use_seed,
    )
    settings = Settings(
        lcm_diffusion_setting=lcm_diffusion_settings,
    )
    reshape = False
    if use_openvino:
        reshape = is_reshape_required(
            previous_width,
            384,
            previous_height,
            384,
            previous_model_id,
            model_id,
            previous_num_of_images,
            1,
        )
    images = context.generate_text_to_image(
        settings,
        reshape,
        DEVICE,
    )
    previous_width = 384
    previous_height = 384
    previous_model_id = model_id
    previous_num_of_images = 1
    out_images = []
    for image in images:
        #out_images.append(image.resize((768, 768),resample=Image.LANCZOS))
        in_image = ImageLoader.load_image(image)
        up_image = upscaler(in_image)
        up_image = up_image.data.cpu().numpy()
        up_image = up_image[0].transpose((1, 2, 0)) * 255.0
        up_image = cv2.cvtColor(up_image, cv2.COLOR_BGR2RGB)
        out_images.append(Image.fromarray(up_image))
       
    return out_images


def get_text_to_image_ui(app_settings: AppSettings) -> None:
    with gr.Blocks():
        with gr.Row():
            with gr.Column():

                def random_seed():
                    global random_enabled
                    random_enabled = not random_enabled
                    seed_val = -1
                    if not random_enabled:
                        seed_val = 42
                    return gr.Number.update(
                        interactive=not random_enabled, value=seed_val
                    )

                with gr.Row():
                    prompt = gr.Textbox(
                        label="Describe the image you'd like to see",
                        lines=3,
                        placeholder="A fantasy landscape",
                    )

                    generate_btn = gr.Button(
                        "Generate",
                        elem_id="generate_button",
                        scale=0,
                    )

                with gr.Accordion("Advanced options", open=False):
                    guidance_scale = gr.Slider(
                        1.0, 30.0, value=8, step=0.5, label="Guidance Scale"
                    )

                    seed = gr.Number(
                        label="Seed",
                        value=-1,
                        precision=0,
                        interactive=False,
                    )
                    seed_checkbox = gr.Checkbox(
                        label="Use random seed",
                        value=True,
                        interactive=True,
                    )

                    openvino_checkbox = gr.Checkbox(
                        label="Use OpenVINO",
                        value=True,
                        interactive=False,
                    )

                    safety_checker_checkbox = gr.Checkbox(
                        label="Use Safety Checker",
                        value=True,
                        interactive=True,
                    )
                    num_inference_steps = gr.Slider(
                        1, 8, value=4, step=1, label="Inference Steps"
                    )
                    # image_height = gr.Slider(
                    #     256, 768, value=384, step=64, label="Image Height",interactive=Fa
                    # )
                    # image_width = gr.Slider(
                    #     256, 768, value=384, step=64, label="Image Width"
                    # )
                    # num_images = gr.Slider(
                    #     1,
                    #     50,
                    #     value=1,
                    #     step=1,
                    #     label="Number of images to generate",
                    # )

                    input_params = [
                        prompt,
                        num_inference_steps,
                        guidance_scale,
                        seed,
                        openvino_checkbox,
                        safety_checker_checkbox,
                    ]

            with gr.Column():
                output = gr.Gallery(
                    label="Generated images",
                    show_label=True,
                    elem_id="gallery",
                    columns=2,
                )

    seed_checkbox.change(fn=random_seed, outputs=seed)
    generate_btn.click(
        fn=generate_text_to_image,
        inputs=input_params,
        outputs=output,
    )