File size: 9,748 Bytes
408b3d1
 
 
 
c411baa
541022c
1c2ba36
 
ace5967
408b3d1
 
ace5967
e137c26
541022c
1c2ba36
6b55b6b
 
 
d0149ce
408b3d1
6b55b6b
408b3d1
6b55b6b
 
c411baa
6b55b6b
 
 
 
 
 
 
 
 
 
408b3d1
6b55b6b
 
1c2ba36
541022c
c411baa
408b3d1
 
 
c411baa
408b3d1
 
 
 
 
c411baa
 
f3e1d16
df528d5
408b3d1
 
 
 
 
 
 
 
 
 
6b55b6b
 
c411baa
1c2ba36
 
c411baa
480ec3f
c411baa
480ec3f
d0149ce
c411baa
d0149ce
 
 
 
c411baa
d0149ce
 
 
 
 
400b304
 
 
 
408b3d1
cf651d1
c411baa
408b3d1
c411baa
 
480ec3f
 
408b3d1
 
c411baa
 
408b3d1
 
 
 
 
 
 
 
 
 
 
 
 
 
c411baa
408b3d1
 
 
 
 
 
 
 
 
 
 
 
c411baa
 
408b3d1
c411baa
408b3d1
 
 
1c2ba36
c411baa
ba7ab2e
 
c411baa
 
408b3d1
 
 
 
 
c411baa
408b3d1
 
 
c411baa
 
 
408b3d1
 
 
 
c411baa
ba7ab2e
 
1c2ba36
c7858ca
1c2ba36
c411baa
408b3d1
 
d510d31
408b3d1
 
 
d0149ce
408b3d1
 
 
d510d31
0f0b345
408b3d1
 
 
 
 
 
 
 
4411765
c411baa
4411765
408b3d1
4411765
3774063
6b55b6b
408b3d1
 
 
c091085
408b3d1
 
 
 
 
 
 
 
 
400b304
d510d31
408b3d1
 
 
886f860
d510d31
408b3d1
d0149ce
1c2ba36
c411baa
1c2ba36
d0149ce
 
c411baa
1c2ba36
2c038e8
0f0b345
c411baa
fadea58
408b3d1
c411baa
408b3d1
ba7ab2e
 
fadea58
ba7ab2e
 
408b3d1
 
 
c411baa
 
 
 
 
 
 
 
 
 
 
 
 
 
408b3d1
c411baa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
408b3d1
 
c411baa
 
 
 
 
 
 
 
408b3d1
c411baa
 
 
 
 
 
 
408b3d1
 
c411baa
f3e1d16
d510d31
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import os
import uuid
import time
import random

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

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

pipe = StableDiffusionXLPipeline.from_pretrained(
    "dhead/wai-nsfw-illustrious-sdxl-v140-sdxl",
    torch_dtype=torch.float16,
    variant="fp16",
    use_safetensors=True,
)

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

pipe.text_encoder.to(torch.float16)
pipe.text_encoder_2.to(torch.float16)
pipe.vae.to(torch.float16)
pipe.unet.to(torch.float16)

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 = 1216

OUTPUT_DIR = "/tmp/generated_images"
os.makedirs(OUTPUT_DIR, exist_ok=True)


def save_image_jpg(pil_image: Image.Image) -> str:
    if pil_image.mode != "RGB":
        pil_image = pil_image.convert("RGB")
    path = os.path.join(OUTPUT_DIR, f"{uuid.uuid4().hex}.jpg")
    pil_image.save(path, "JPEG", quality=95)
    return path


@spaces.GPU(duration=15)
def infer(
    prompt,
    negative_prompt,
    seed,
    randomize_seed,
    width,
    height,
    guidance_scale,
    num_inference_steps,
):
    if not prompt.strip():
        raise gr.Error("Prompt cannot be empty.")

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

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

    try:
        conditioning, pooled = compel([prompt, negative_prompt])

        prompt_embeds = conditioning[0:1]
        pooled_prompt_embeds = pooled[0:1]
        negative_prompt_embeds = conditioning[1:2]
        negative_pooled_prompt_embeds = pooled[1:2]

        image = pipe(
            prompt_embeds=prompt_embeds,
            pooled_prompt_embeds=pooled_prompt_embeds,
            negative_prompt_embeds=negative_prompt_embeds,
            negative_pooled_prompt_embeds=negative_pooled_prompt_embeds,
            guidance_scale=guidance_scale,
            num_inference_steps=num_inference_steps,
            width=width,
            height=height,
            generator=generator,
        ).images[0]

        image_path = save_image_jpg(image)
        return image_path, seed

    except RuntimeError as e:
        print(f"Error during generation: {e}")
        blank_image = Image.new("RGB", (width, height), color=(0, 0, 0))
        blank_path = save_image_jpg(blank_image)
        return blank_path, seed


def generation_loop(
    prompt,
    negative_prompt,
    current_seed,
    randomize_seed,
    width,
    height,
    guidance_scale,
    num_inference_steps,
    interval_sec,
):
    if not prompt.strip():
        raise gr.Error("Prompt cannot be empty to start consecutive generation.")

    while True:
        try:
            image_path, new_seed = infer(
                prompt,
                negative_prompt,
                current_seed,
                True,  # 連続生成は毎回seedを変える
                width,
                height,
                guidance_scale,
                num_inference_steps,
            )

            yield {result: image_path, seed: new_seed}
            time.sleep(interval_sec)

        except gr.exceptions.CancelledError:
            print("Generation loop cancelled by user.")
            break


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

/* 完全透過(非表示だがクリック等は可能なまま) */
.transparent-btn,
.transparent-btn * {
  opacity: 0 !important;
}

.transparent-btn button {
  background: transparent !important;
  border: 0 !important;
  box-shadow: none !important;
}

.transparent-btn button:focus,
.transparent-btn button:focus-visible {
  outline: none !important;
}
"""

with gr.Blocks(css=css) as demo:
    with gr.Column(elem_id="col-container"):
        gr.Markdown("<br>" * 1)
        # Prompt(右にGenerateは置かない)
        with gr.Row(equal_height=True):
            prompt = gr.Text(
                label="Prompt",
                show_label=False,
                max_lines=1,
                placeholder="Enter your prompt",
                value="",
                container=False,
                scale=1,
            )

        # 画像表示
        with gr.Row():
            result = gr.Image(format="jpeg", show_label=False, interactive=False, elem_id="result_image")

        # 画像表示欄のすぐ下、20行改行のすぐ上:GenerateとConsecutiveを横並び、完全透過
        with gr.Row(equal_height=True):
            run_button = gr.Button("Generate", scale=0, interactive=False, elem_classes=["transparent-btn"])
            consecutive_button = gr.Button("Consecutive", scale=0, interactive=False, elem_classes=["transparent-btn"])

        gr.Markdown("<br>" * 20)

        # 停止/クリア
        with gr.Row():
            stop_button = gr.Button("Stop", scale=0, visible=True, interactive=True)
            clear_button = gr.Button("Trash", scale=0, variant="secondary")

        # Copy の右に URL欄(Trash と Advanced Settings の間)
        with gr.Row(equal_height=True):
            copy_button = gr.Button("Copy", scale=0, variant="secondary")
            image_url = gr.Textbox(
                label="Image URL",
                show_label=False,
                interactive=False,
                max_lines=2,
                placeholder="生成後、ここに外部URLが表示されます",
                scale=1,
            )

        with gr.Accordion("Advanced Settings", open=False):
            negative_prompt = gr.Text(
                label="Negative prompt",
                max_lines=1,
                placeholder="Enter a negative prompt",
                value="photoreal, bad quality, low quality, worst quality, worst detail, bad anatomy, extra hand, viewer's hand",
            )

            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=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024)
                height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024)

            with gr.Row():
                guidance_scale = gr.Slider(label="Guidance scale", minimum=0.0, maximum=20.0, step=0.1, value=8)
                num_inference_steps = gr.Slider(label="Number of inference steps", minimum=1, maximum=28, step=1, value=25)

            interval_seconds = gr.Slider(label="Interval (seconds)", minimum=1, maximum=60, step=1, value=1)

        gr.Markdown("<br>" * 20)

        gr.Examples(
            examples=[
                ["masterpiece, solo, A little girl with blonde short side tails, red eyes, "],
            ],
            inputs=[prompt],
            label="Examples (Click to copy to prompt)",
        )

        # Promptが空でなければボタンを押せるようにする
        prompt.input(
            fn=None,
            inputs=[prompt],
            outputs=[run_button, consecutive_button],
            js="(p) => { const interactive = p.trim().length > 0; return [{ interactive: interactive, '__type__': 'update' }, { interactive: interactive, '__type__': 'update' }]; }",
        )

        # クリア:promptを空にしてボタン無効、URL欄も空にする
        clear_button.click(
            fn=None,
            inputs=None,
            outputs=[prompt, run_button, consecutive_button, image_url],
            js="""
function() {
  return [
    "",
    { "interactive": false, "__type__": "update" },
    { "interactive": false, "__type__": "update" },
    ""
  ];
}
""",
        )

        # 生成
        run_button.click(
            fn=infer,
            inputs=[prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
            outputs=[result, seed],
        )

        # 連続生成
        gen_inputs = [
            prompt,
            negative_prompt,
            seed,
            randomize_seed,
            width,
            height,
            guidance_scale,
            num_inference_steps,
            interval_seconds,
        ]

        consecutive_event = consecutive_button.click(
            fn=generation_loop,
            inputs=gen_inputs,
            outputs=[result, seed],
        )

        # 停止
        stop_button.click(
            fn=None,
            inputs=None,
            outputs=None,
            cancels=[consecutive_event],
        )

        # resultが更新されたら、表示中のimg.src(= /file=...)を拾って表示
        result.change(
            fn=None,
            inputs=None,
            outputs=[image_url],
            js=r"""
() => {
  const img = document.querySelector("#result_image img");
  if (!img || !img.src) return "";
  return new URL(img.src, window.location.href).href;
}
""",
        )

        # Copyボタン:URL文字列をコピー
        copy_button.click(
            fn=None,
            inputs=[image_url],
            outputs=None,
            js=r"""
async (url) => {
  if (!url) return;
  try {
    await navigator.clipboard.writeText(url);
    console.log("URL copied");
  } catch (e) {
    console.error("Copy failed", e);
  }
}
""",
        )

demo.queue().launch()