File size: 11,052 Bytes
8eca2ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import os
import random
import torch
import gc
import gradio as gr
import style as sty
from PIL import Image
from scheduler_mapping import schedulers, apply_scheduler
from utils import *

from diffusers.utils import logging
from query_comfyui import *

logging.set_verbosity_info()
logging.get_logger("diffusers").setLevel(logging.ERROR)

SCHEDULERS = list(schedulers.keys())
SCHEDULERS.insert(0, "Default")


def gen_image(prompt, negative_prompt, width, height,
              num_steps, mode, seed, guidance_scale,
              lora_weight_file, lora_scale, fast_infer,
              scheduler, num_images, progress=gr.Progress(track_tqdm=True)):
    """
    Run diffusion model to generate image
    """
    progress(0, "Starting image generation...")
    for i in range(1, num_steps + 1):
        progress(i / num_steps * 100, f"Processing step {i} of {num_steps}...")

    images = [Image.open("stuffs/logo.png")]
    
    if len(prompt) == 0:
        gr.Info("Please input prompt!", duration=5)
        return images
    
    # Query COmfyUI backend
    if "Stable Diffusion 3.5" in mode:
        if "Medium" in mode:
            ckpt_name = "sd3.5_medium.safetensors"
        else:
            ckpt_name = "sd3.5_large.safetensors"
        images = query_sd35(ckpt_name, prompt, negative_prompt,
                            int(width), int(height),
                            int(num_images), int(seed),
                            float(guidance_scale), int(num_steps))
        return images
    
    model = TEXT_TO_IMAGE_DICTIONARY[mode]
    use_lora = False
    _, current_max_memory = get_gpu_info(width, height, num_images)
    Text2Image_class = model["pipeline"]

    diffusion_configs = {
        "use_safetensors": True,
        "max_memory": current_max_memory
    }
    if "device_map" in model:
        diffusion_configs["device_map"] = model["device_map"]
    if fast_infer:
        diffusion_configs["torch_dtype"] = torch.float16
    if "FLUX" in mode:
        diffusion_configs["torch_dtype"] = torch.bfloat16


    if model["path"].endswith('.safetensors'):
        pipeline = Text2Image_class.from_single_file(
            model["path"], **diffusion_configs)
    else:
        pipeline = Text2Image_class.from_pretrained(
            model["path"], **diffusion_configs)
    pipeline.safety_checker = None

    try:
        pipeline = apply_scheduler(scheduler, pipeline)
    except BaseException:
        gr.Warning(f"Cannot apply {scheduler} for {mode}. Use default sampler instead")
        pipeline = apply_scheduler("Default", pipeline)
        
    # Load LoRA adapter
    if lora_weight_file is not None:
        directory, file_name = os.path.split(lora_weight_file.name)
        try:
            pipeline.load_lora_weights(
                directory,
                weight_name=file_name,
                adapter_name=file_name.replace(".safetensors", ''))
            gr.Info("LoRA weight loaded succesfully", duration=5)
            use_lora = True
        except Exception as e:
            print(e)
            gr.Warning("Cannot load LoRA weight, your model won't use adapter", duration=5)

    # Assign GPU for pipeline
    # if "FLUX" not in mode and "Stable Diffusion 3" not in mode:
    device = assign_gpu(required_vram=10000,
                        width=width,
                        height=height,
                        num_images=num_images)
    if device == "cpu":
        gr.Warning("No available GPUs for inference")
        return images

    generator = torch.Generator("cuda").manual_seed(int(seed))
    try:
        pipeline_configs = {
            "prompt": prompt,
            "negative_prompt": negative_prompt,
            "width": nearest_divisible_by_8(int(width)),
            "height": nearest_divisible_by_8(int(height)),
            "num_inference_steps": int(num_steps),
            "generator": generator,
            "guidance_scale": float(guidance_scale),
            "num_images_per_prompt": num_images
        }
        if "FLUX" not in mode:
            pipeline = pipeline.to(device)
        else:
            # Adjust for FLUX Pipeline
            del pipeline_configs["negative_prompt"]
            # Max 256 tokens for prompt
            pipeline_configs["max_sequence_length"] = 256

        if use_lora:
            if "FLUX" in mode or "Stable Diffusion 3" in mode:
                pipeline_configs["joint_attention_kwargs"] = {
                    "scale": lora_scale}
            else:
                pipeline_configs["cross_attention_kwargs"] = {
                    "scale": lora_scale}

        # Generate images
        images = pipeline(**pipeline_configs).images
    except Exception as e:
        raise gr.Error(f"Exception: {e}", duration=5)

    progress(100, "Completed!")
    del pipeline
    pipeline = None
    gc.collect()
    torch.cuda.empty_cache()
    return images


# -------------------------------------------- Gradio App -------------------------------------------- #
with gr.Blocks(title="TonAI Creative",
               theme=sty.app_theme,
               css=sty.custom_css) as interface:
    gr.HTML(sty.tonai_creative_html)
    with gr.Row():
        with gr.Column(scale=2):
            with gr.Accordion("Basic Usage", open=True):
                with gr.Row():
                    prompt = gr.Textbox(
                        label="Prompt",
                        placeholder="Describe the image you want to generate")
                with gr.Row():
                    width = gr.components.Slider(
                        minimum=512, maximum=1920, value=1024, step=8,
                        label="Width",
                        scale=1
                    )
                    height = gr.components.Slider(
                        minimum=512, maximum=1920, value=1024, step=8,
                        label="Height",
                        scale=1
                    )
                    mode = gr.Dropdown(
                        choices=TEXT_TO_IMAGE_DICTIONARY.keys(),
                        label="Mode",
                        filterable=False,
                        value=list(TEXT_TO_IMAGE_DICTIONARY.keys())[
                            0],  # FLUX.1 Merged is default
                        interactive=True,
                        scale=1)
                with gr.Row():
                    generate_btn = gr.Button("Generate", scale=2)
                    stop_btn = gr.Button("Stop", elem_id="stop-button", scale=1)

            with gr.Accordion("Advanced Settings", open=False):
                negative_prompt = gr.Textbox(
                    label="Negative Prompt",
                    value="ugly, disfigured, deformed",
                    placeholder="Instruct the AI model that it should not include")
                with gr.Row():
                    with gr.Column(scale=4):
                        with gr.Row():
                            num_steps = gr.components.Slider(
                                minimum=3, maximum=50, value=20, step=1,
                                label="Inference Steps",
                                scale=2
                            )
                        with gr.Row():
                            guidance_scale = gr.components.Slider(
                                minimum=0, maximum=20, value=3, step=0.1,
                                label="CFG Scale",
                                scale=1
                            )
                        with gr.Row():
                            num_images = gr.components.Slider(
                                minimum=1, maximum=6, value=1, step=1,
                                label="Number of generated images",
                                scale=1)
                            scheduler = gr.Dropdown(
                                choices=SCHEDULERS,
                                label="Sampler",
                                filterable=False,
                                value=SCHEDULERS[0],
                                interactive=True,
                                scale=1)
                    with gr.Column(scale=1):
                        seed = gr.Textbox(label="RNG Seed", value=0)
                        rng_btn = gr.Button("Roll the 🎲", scale=1)
                        rng_btn.click(
                            fn=generate_number, inputs=None, outputs=seed)
                        fast_infer = gr.Checkbox(
                            label="Fast Inference",
                            info="Faster run with FP16",
                            value=True,
                            scale=1)
                with gr.Row():
                    lora_weight_file = gr.File(
                        label="LoRA safetensors file",
                        elem_classes="file-uploader",
                        file_types=["safetensors"],
                        min_width=50, height=30, scale=2)
                    lora_scale = gr.components.Slider(
                        minimum=0, maximum=1, value=0.8, step=0.01,
                        label="LoRA Scale",
                        scale=1
                    )
            with gr.Accordion("Helps", open=False):
                gr.Markdown(sty.tips_content)

        with gr.Column(scale=1):
            gallery = gr.Gallery(
                label="Generated Images",
                format="png",
                elem_id="gallery",
                columns=2, rows=2,
                preview=True,
                object_fit="contain")
            click_button_behavior = {
                "fn": gen_image,
                "outputs": gallery,
                "concurrency_limit": 10
            }
            click_event = generate_btn.click(inputs=[prompt,
                                       negative_prompt,
                                       width,
                                       height,
                                       num_steps,
                                       mode,
                                       seed,
                                       guidance_scale,
                                       lora_weight_file,
                                       lora_scale,
                                       fast_infer,
                                       scheduler,
                                       num_images],
                               **click_button_behavior)
            stop_btn.click(fn=None, inputs=None, outputs=None, cancels=[click_event])

        interface.load(
            lambda: gr.update(
                value=random.randint(
                    0, 999999)), None, seed)

if __name__ == '__main__':
    allowed_paths = ["stuffs/splash.png", "stuffs/favicon.png"]
    interface.queue(default_concurrency_limit=10)
    interface.launch(share=False,
                     root_path="/tonai",
                     server_name="0.0.0.0",
                     show_error=True,
                     favicon_path="stuffs/favicon.png",
                     allowed_paths=allowed_paths,
                     max_threads=10)