File size: 6,539 Bytes
6267052
 
 
524b414
 
a6ad525
524b414
a6ad525
6267052
 
 
a6ad525
 
 
f05f013
 
 
 
 
a6ad525
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
524b414
47ab942
f05f013
 
47ab942
 
a6ad525
 
 
 
 
 
f05f013
a6ad525
f05f013
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6267052
f05f013
 
 
 
 
 
 
 
 
 
 
524b414
 
 
f05f013
 
 
 
 
 
 
 
a6ad525
f05f013
 
 
6267052
 
 
 
f05f013
 
524b414
f05f013
 
 
 
524b414
 
 
 
6267052
524b414
 
 
 
ffe4561
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
524b414
ffe4561
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
524b414
 
903a60a
 
 
 
 
 
 
 
 
 
26eb541
cdc565e
 
 
 
26eb541
f7fa496
cdc565e
f7fa496
 
 
 
 
 
 
 
 
 
 
 
 
 
903a60a
26eb541
 
 
 
 
f7fa496
903a60a
 
cdc565e
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
#import spaces
import random

import gradio as gr
import numpy as np
import PIL.Image
import torch
from compel import Compel, ReturnedEmbeddingsType
from diffusers import (EulerAncestralDiscreteScheduler,
                       StableDiffusionXLPipeline)
from PIL import Image

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

DEFAULT_BATCH_SIZE = 2
MAX_BATCH_SIZE = 12



# Make sure to use torch.float16 consistently throughout the pipeline
pipe = StableDiffusionXLPipeline.from_pretrained(
    "dhead/wai-nsfw-illustrious-sdxl-v140-sdxl",
    torch_dtype=torch.float16,
    variant="fp16",  # Explicitly use fp16 variant
    use_safetensors=True  # Use safetensors if available
)

pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
pipe.to(device)

# Force all components to use the same dtype
pipe.text_encoder.to(torch.float16)
pipe.text_encoder_2.to(torch.float16)
pipe.vae.to(torch.float16)
pipe.unet.to(torch.float16)

# 追加: Initialize Compel for long prompt processing
compel = Compel(
    tokenizer=[pipe.tokenizer, pipe.tokenizer_2],
    text_encoder=[pipe.text_encoder, pipe.text_encoder_2],
    returned_embeddings_type=ReturnedEmbeddingsType.PENULTIMATE_HIDDEN_STATES_NON_NORMALIZED,
    requires_pooled=[False, True],
    truncate_long_prompts=False
)


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


# 追加: Simple long prompt processing function
def process_long_prompt(prompt, negative_prompt=""):
    try:
        conditioning, pooled = compel([prompt, negative_prompt])
        return conditioning, pooled
    except Exception as e:
        print(f"Long prompt processing failed: {e}")
        return None, None
def generate_image(
    prompt,
    negative_prompt,
    generator,
    width,
    height,
    guidance_scale,
    num_inference_steps,
):
    return 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]
#@spaces.GPU
def infer(
    prompt,
    negative_prompt,
    seed,
    randomize_seed,
    batch_size,
    width,
    height,
    guidance_scale,
    num_inference_steps,
):
    if randomize_seed:
        seed = random.randint(0, MAX_SEED)

    prompts = [prompt] * batch_size
    negative_prompts = [negative_prompt] * batch_size

    generators = [
        torch.Generator(device=device).manual_seed(seed - i)
        for i in range(batch_size)
    ]

    try:
        images = pipe(
            prompt=prompts,
            negative_prompt=negative_prompts,
            guidance_scale=guidance_scale,
            num_inference_steps=num_inference_steps,
            width=width,
            height=height,
            generator=generators,
        ).images

        return images
    except RuntimeError as e:
        print(f"Generation error: {e}")
        return [Image.new("RGB", (width, height), (0, 0, 0))] * batch_size

css = """
#col-container {
    margin: 0 auto;
    max-width: 2000px;
}
"""

with gr.Blocks(css=css) as demo:
    with gr.Accordion("Advanced Settings", open=False):

        negative_prompt = gr.Text(
            label="Negative prompt",
            max_lines=5,
            placeholder="Enter a negative prompt",
            value=" ((unimaginative, dry, flat, static, stiff, uninspired), bad quality overexposed, too bright, washed out, high exposure, low resolution, artifact, compression artifacts, low poly, blocky, banding, color bleed, texture seams, oversaturation, fused fingers, malformed eyes, missing iris sclera, poorly drawn background)"
        )
        seed = gr.Slider(
            label="Seed",
            minimum=0,
            maximum=MAX_SEED,
            step=20,
            value=0,
        )
        randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
        with gr.Row():
            width = gr.Slider(
                label="Width",
                minimum=256,
                maximum=MAX_IMAGE_SIZE,
                step=32,
                value=600,
            )
            height = gr.Slider(
                label="Height",
                minimum=256,
                maximum=MAX_IMAGE_SIZE,
                step=32,
                value=800,
            )
        with gr.Row():
            guidance_scale = gr.Slider(
                label="Guidance scale",
                minimum=0.0,
                maximum=30.0,
                step=0.1,
                value=7,
            )
            batch_size = gr.Slider(
                label="Batch size",
                minimum=1,
                maximum=MAX_BATCH_SIZE,
                step=1,
                value=DEFAULT_BATCH_SIZE,
            )
        with gr.Row():
            num_inference_steps = gr.Slider(
                label="Number of inference steps",
                minimum=1,
                maximum=99,
                step=1,
                value=20,
            )

    with gr.Column(elem_id="col-container"):
        with gr.Row():
            with gr.Row():
                prompt = gr.Text(
                    label="Prompt",
                    show_label=False,
                    max_lines=16,
                    placeholder="Enter your prompt (long prompts are automatically supported)",
                    value="Show me a glorious mountain range covered in colorful crystaline trees with a laser disco show from space.",
                    container=False,
                    )    
                results = [
                    gr.Image(format="png", show_label=False)
                    for _ in range(MAX_BATCH_SIZE)
                ]
                    run_button = gr.Button("Run", scale=0)
                run_button.click(
                concurrency_limit=25,
                fn=infer,
                inputs=[
                    prompt,
                    negative_prompt,
                    seed,
                    randomize_seed,
                    batch_size,
                    width,
                    height,
                    guidance_scale,
                    num_inference_steps,
                ],
                outputs=results,
                )
                
    with gr.Column():
        for _ in range(5):
            with gr.Row():
                for _ in range(2):
                    results.append(gr.Image(format="png", show_label=False, visible=True))

        

demo.queue(default_concurrency_limit=25).launch()