File size: 5,299 Bytes
bcd4798
 
 
b46a44f
d840e13
bcd4798
115b9e5
376d672
bcd4798
d840e13
bcd4798
63349a0
 
40c0e85
bcd4798
d840e13
bcd4798
40c0e85
ab15d7a
40c0e85
 
5f44cc4
 
376d672
62e2d80
1d09186
 
 
 
c5d4737
ab15d7a
 
5f44cc4
40c0e85
 
 
bce5a4e
 
bcd4798
 
 
 
 
 
 
 
 
 
 
40c0e85
 
bcd4798
 
 
 
 
40c0e85
 
 
 
 
 
bcd4798
 
 
 
 
 
 
 
 
 
b46a44f
 
 
bcd4798
 
 
6a1f8fc
d840e13
5e944b9
40c0e85
bcd4798
 
 
 
 
 
 
 
 
 
376d672
 
 
 
 
ad08350
376d672
 
bcd4798
 
b46a44f
bcd4798
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d840e13
bcd4798
 
63349a0
bcd4798
 
 
 
d840e13
bcd4798
 
63349a0
bcd4798
 
 
 
 
 
 
 
40c0e85
bcd4798
 
 
40c0e85
 
 
ab15d7a
40c0e85
 
 
 
 
 
 
 
 
bcd4798
 
 
 
 
 
 
 
 
 
 
 
 
b46a44f
bcd4798
 
 
d840e13
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
import gradio as gr
import numpy as np
import random
import time
from optimum.intel import OVStableDiffusionXLPipeline
import torch
from diffusers import EulerDiscreteScheduler
from diffusers import LCMScheduler

model_id = "None1145/noobai-XL-Vpred-0.65s-openvino"

prev_height = 1216
prev_width = 832

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

def reload_model(new_model_id):
    global pipe, model_id, prev_height, prev_width
    model_id = new_model_id
    try:
        print(f"{model_id}...")
        pipe = OVStableDiffusionXLPipeline.from_pretrained(model_id, compile=False)
        if model_id == "None1145/noobai-XL-Vpred-0.65s-openvino":
            scheduler_args = {"prediction_type": "v_prediction", "rescale_betas_zero_snr": True}
            pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, **scheduler_args)
            # pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config, **scheduler_args)
            # pipe.load_lora_weights("latent-consistency/lcm-lora-sdxl")
            # pipe.fuse_lora()
        # pipe.to("gpu")
        pipe.reshape(batch_size=1, height=prev_height, width=prev_width, num_images_per_prompt=1)
        pipe.compile()
        print(f"{model_id}!!!")
        return f"Model successfully loaded: {model_id}"
    except Exception as e:
        return f"Failed to load model: {str(e)}"
reload_model(model_id)
        
def infer(
    prompt,
    negative_prompt,
    seed,
    randomize_seed,
    width,
    height,
    guidance_scale,
    num_inference_steps,
    progress=gr.Progress(track_tqdm=True),
):
    global prev_width, prev_height, pipe

    if randomize_seed:
        seed = random.randint(0, MAX_SEED)

    generator = torch.Generator().manual_seed(seed)

    if prev_width != width or prev_height != height:
        pipe.reshape(batch_size=1, height=height, width=width, num_images_per_prompt=1)
        pipe.compile()
        prev_width = width
        prev_height = height

    image = pipe(
        prompt=prompt,
        negative_prompt=negative_prompt,
        guidance_scale=guidance_scale,
        num_inference_steps=num_inference_steps,
        width=width,
        height=height,
        generator=generator,
    ).images[0]

    filename = f"./tmp/{time.time()}.jpg"
    image.save(filename)

    return image, seed


examples = ["murasame \(senren\), senren banka",]
with gr.Blocks() as img:
    gr.Markdown("# OpenVINO Text to Image")
    
    with gr.Column(elem_id="col-container"):
        with gr.Row():
            prompt = gr.Text(
                label="Prompt",
                show_label=False,
                max_lines=1,
                placeholder="Enter your prompt",
                container=False,
            )

            num_inference_steps = gr.Slider(
                label="Number of inference steps",
                minimum=1,
                maximum=60,
                step=1,
                value=28,
            )

            run_button = gr.Button("Run", scale=0, variant="primary")

        file_output = gr.File(label="Download your image")
        result = gr.Image(label="Result", show_label=False)

        with gr.Accordion("Advanced Settings", open=False):
            negative_prompt = gr.Text(
                label="Negative prompt",
                max_lines=1,
                placeholder="Enter a negative prompt",
                visible=False,
            )

            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=512,
                    maximum=MAX_IMAGE_SIZE,
                    step=32,
                    value=832,
                )

                height = gr.Slider(
                    label="Height",
                    minimum=512,
                    maximum=MAX_IMAGE_SIZE,
                    step=32,
                    value=1216,
                )

            with gr.Row():
                guidance_scale = gr.Slider(
                    label="Guidance scale",
                    minimum=0.0,
                    maximum=10.0,
                    step=0.1,
                    value=5.0,
                )

        gr.Examples(examples=examples, inputs=[prompt])
    
    gr.Markdown("### Model Reload")
    with gr.Row():
        new_model_id = gr.Text(label="New Model ID", placeholder="Enter model ID", value=model_id)
        reload_button = gr.Button("Reload Model", variant="primary")
        reload_status = gr.Text(label="Status", interactive=False)

    reload_button.click(
        fn=reload_model,
        inputs=new_model_id,
        outputs=reload_status,
    )

    gr.on(
        triggers=[run_button.click, prompt.submit],
        fn=infer,
        inputs=[
            prompt,
            negative_prompt,
            seed,
            randomize_seed,
            width,
            height,
            guidance_scale,
            num_inference_steps,
        ],
        outputs=[result, seed, file_output],
    )

if __name__ == "__main__":
    img.launch()