File size: 5,541 Bytes
6eb9fe9
2685bf1
6eb9fe9
 
 
26f54c4
6eb9fe9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8cbc484
 
 
6eb9fe9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26f54c4
6eb9fe9
 
 
26f54c4
 
6eb9fe9
 
 
 
 
 
26f54c4
6eb9fe9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26f54c4
6eb9fe9
 
 
 
 
 
 
 
 
 
 
 
 
 
452a02d
6eb9fe9
2685bf1
6eb9fe9
 
 
 
 
26f54c4
 
6eb9fe9
 
 
 
26f54c4
 
6eb9fe9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26f54c4
6eb9fe9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26f54c4
6eb9fe9
 
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
import os
import gradio as gr
import json
import logging
import torch
from PIL import Image
import spaces
from diffusers import DiffusionPipeline, FlowMatchEulerDiscreteScheduler
from huggingface_hub import hf_hub_download, HfFileSystem, ModelCard, snapshot_download
import copy
import random
import time
import re
import math
import numpy as np
import traceback


# Initialize the base model
dtype = torch.bfloat16
device = "cuda" if torch.cuda.is_available() else "cpu"
base_model = "Qwen/Qwen-Image-2512"

# Scheduler configuration from the Qwen-Image-Lightning repository
scheduler_config = {
    "base_image_seq_len": 256,
    "base_shift": math.log(3),
    "invert_sigmas": False,
    "max_image_seq_len": 8192,
    "max_shift": math.log(3),
    "num_train_timesteps": 1000,
    "shift": 1.0,
    "shift_terminal": None,
    "stochastic_sampling": False,
    "time_shift_type": "exponential",
    "use_beta_sigmas": False,
    "use_dynamic_shifting": True,
    "use_exponential_sigmas": False,
    "use_karras_sigmas": False,
}

scheduler = FlowMatchEulerDiscreteScheduler.from_config(scheduler_config)
pipe = DiffusionPipeline.from_pretrained(
    base_model, 
    scheduler=scheduler, 
    torch_dtype=dtype
).to(device)

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


class calculateDuration:
    def __init__(self, activity_name=""):
        self.activity_name = activity_name


    def __enter__(self):
        self.start_time = time.time()
        return self
    
    def __exit__(self, exc_type, exc_value, traceback):
        self.end_time = time.time()
        self.elapsed_time = self.end_time - self.start_time
        if self.activity_name:
            print(f"Elapsed time for {self.activity_name}: {self.elapsed_time:.6f} seconds")
        else:
            print(f"Elapsed time: {self.elapsed_time:.6f} seconds")


@spaces.GPU(duration=70)
def generate_image(
    prompt_mash,
    width,
    height,
):
    pipe.to("cuda")
    if negative_prompt == '':
        negative_prompt =  "δ½Žεˆ†θΎ¨ηŽ‡οΌŒδ½Žη”»θ΄¨οΌŒθ‚’δ½“η•Έε½’οΌŒζ‰‹ζŒ‡η•Έε½’οΌŒη”»ι’θΏ‡ι₯±ε’ŒοΌŒθœ‘εƒζ„ŸοΌŒδΊΊθ„Έζ— η»†θŠ‚οΌŒθΏ‡εΊ¦ε…‰ζ»‘οΌŒη”»ι’ε…·ζœ‰AIζ„Ÿγ€‚ζž„ε›Ύζ··δΉ±γ€‚ζ–‡ε­—ζ¨‘η³ŠοΌŒζ‰­ζ›²γ€‚"


    seed = 235234
    num_images = 1
    seeds = [seed + (i * 100) for i in range(num_images)]
    generators = [torch.Generator(device="cuda").manual_seed(s) for s in seeds]
    
    images = []

    with calculateDuration("Generating images (sequential)"):
        for i in range(num_images):
            current_seed = seed + (i * 100)
            generator = torch.Generator(device="cuda").manual_seed(current_seed)
    
            result = pipe(
                prompt=prompt_mash,
                negative_prompt=negative_prompt,
                num_inference_steps=4,
                true_cfg_scale=1,
                width=width,
                height=height,
                num_images_per_prompt=1,
                generator=generator,
            )
    
            images.append((result.images[0], current_seed))
    return images
           


@spaces.GPU(duration=70)
def run_lora(
    prompt, 
    width, 
    height, 
    progress=gr.Progress(track_tqdm=True)
):

    with calculateDuration("Loading Lightning LoRA and style LoRA"):
        pipe.load_lora_weights(
            'Wuli-Art/Qwen-Image-2512-Turbo-LoRA', 
            weight_name='Wuli-Qwen-Image-2512-Turbo-LoRA-4steps-V1.0-bf16.safetensors',
            adapter_name="lightning"
        )
        pipe.set_adapters(["lightning"], adapter_weights=[1.0])

    
    multiplier = float(quality_multiplier.replace('x', ''))
    width = int(width * multiplier)
    height = int(height * multiplier)
    num_images = int(quantity) + 1


    pairs = generate_image(
        prompt,
        width,
        height
    )

    images_for_gallery = [
        (img, str(s))
        for (img, s) in pairs
    ]

    return images_for_gallery



css = '''
#gen_btn{height: 100%}
#gen_column{align-self: stretch}
#title{text-align: center}
#title h1{font-size: 3em; display:inline-flex; align-items:center}
#title img{width: 100px; margin-right: 0.5em}
#gallery .grid-wrap{height: 10vh}
#lora_list{background: var(--block-background-fill);padding: 0 1em .3em; font-size: 90%}
.card_internal{display: flex;height: 100px;margin-top: .5em}
.card_internal img{margin-right: 1em}
.styler{--form-gap-width: 0px !important}
#speed_status{padding: .5em; border-radius: 5px; margin: 1em 0}
'''


with gr.Blocks(theme=gr.themes.Soft(), css=css, delete_cache=(60, 60)) as app:
    title = gr.HTML(
        """<img src=\"https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-Image/qwen_image_logo.png\" alt=\"Qwen-Image\" style=\"width: 280px; margin: 0 auto\">        
        <h3 style=\"margin-top: -10px\">Wuli-art/Qwen-Image-2512-Turbo-LoRA</h3>""",
        elem_id="title",
    )
    
    selected_index = gr.State(None)
    
    with gr.Row():
        with gr.Column(scale=3):
            prompt = gr.Textbox(label="Prompt", lines=1, placeholder="Type a prompt after selecting a LoRA")
            generate_button = gr.Button("Generate", variant="primary", elem_id="gen_btn", interactive=False)
        with gr.Column(scale=1, elem_id="gen_column"):
            result = gr.Gallery(label="Generated Images", show_label=True, elem_id="result_gallery")
    

    generate_event = gr.on(
        triggers=[generate_button.click, prompt.submit],
        fn=run_lora,
        inputs=[
            prompt, 
            width, 
            height
        ],
        outputs=[result]
    )


app.queue()
app.launch()