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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import spaces
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import numpy as np
|
| 4 |
+
import PIL.Image
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import random
|
| 7 |
+
from diffusers import StableDiffusionXLPipeline
|
| 8 |
+
from diffusers import EulerAncestralDiscreteScheduler
|
| 9 |
+
import torch
|
| 10 |
+
from compel import Compel, ReturnedEmbeddingsType
|
| 11 |
+
import time
|
| 12 |
+
import io
|
| 13 |
+
import base64
|
| 14 |
+
|
| 15 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 16 |
+
|
| 17 |
+
pipe = StableDiffusionXLPipeline.from_pretrained(
|
| 18 |
+
"dhead/wai-nsfw-illustrious-sdxl-v140-sdxl",
|
| 19 |
+
torch_dtype=torch.float16,
|
| 20 |
+
variant="fp16",
|
| 21 |
+
use_safetensors=True
|
| 22 |
+
)
|
| 23 |
+
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
| 24 |
+
pipe.to(device)
|
| 25 |
+
|
| 26 |
+
pipe.text_encoder.to(torch.float16)
|
| 27 |
+
pipe.text_encoder_2.to(torch.float16)
|
| 28 |
+
pipe.vae.to(torch.float16)
|
| 29 |
+
pipe.unet.to(torch.float16)
|
| 30 |
+
|
| 31 |
+
compel = Compel(
|
| 32 |
+
tokenizer=[pipe.tokenizer, pipe.tokenizer_2],
|
| 33 |
+
text_encoder=[pipe.text_encoder, pipe.text_encoder_2],
|
| 34 |
+
returned_embeddings_type=ReturnedEmbeddingsType.PENULTIMATE_HIDDEN_STATES_NON_NORMALIZED,
|
| 35 |
+
requires_pooled=[False, True],
|
| 36 |
+
truncate_long_prompts=False
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
MAX_SEED = np.iinfo(np.int32).max
|
| 40 |
+
MAX_IMAGE_SIZE = 1216
|
| 41 |
+
|
| 42 |
+
# --- 変更点: PIL画像をJPEG形式のBase64データURIに変換する関数 ---
|
| 43 |
+
def pil_to_base64(pil_image):
|
| 44 |
+
with io.BytesIO() as stream:
|
| 45 |
+
# PILはRGBモードでないとJPEGで保存できないため、変換を試みる
|
| 46 |
+
if pil_image.mode != "RGB":
|
| 47 |
+
pil_image = pil_image.convert("RGB")
|
| 48 |
+
pil_image.save(stream, "JPEG")
|
| 49 |
+
base64_str = base64.b64encode(stream.getvalue()).decode("utf-8")
|
| 50 |
+
return "data:image/jpeg;base64," + base64_str
|
| 51 |
+
|
| 52 |
+
@spaces.GPU(duration=15)
|
| 53 |
+
def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
|
| 54 |
+
if not prompt.strip():
|
| 55 |
+
raise gr.Error("Prompt cannot be empty.")
|
| 56 |
+
|
| 57 |
+
if randomize_seed:
|
| 58 |
+
seed = random.randint(0, MAX_SEED)
|
| 59 |
+
generator = torch.Generator(device=device).manual_seed(seed)
|
| 60 |
+
|
| 61 |
+
try:
|
| 62 |
+
conditioning, pooled = compel([prompt, negative_prompt])
|
| 63 |
+
prompt_embeds = conditioning[0:1]
|
| 64 |
+
pooled_prompt_embeds = pooled[0:1]
|
| 65 |
+
negative_prompt_embeds = conditioning[1:2]
|
| 66 |
+
negative_pooled_prompt_embeds = pooled[1:2]
|
| 67 |
+
|
| 68 |
+
image = pipe(
|
| 69 |
+
prompt_embeds=prompt_embeds,
|
| 70 |
+
pooled_prompt_embeds=pooled_prompt_embeds,
|
| 71 |
+
negative_prompt_embeds=negative_prompt_embeds,
|
| 72 |
+
negative_pooled_prompt_embeds=negative_pooled_prompt_embeds,
|
| 73 |
+
guidance_scale=guidance_scale,
|
| 74 |
+
num_inference_steps=num_inference_steps,
|
| 75 |
+
width=width,
|
| 76 |
+
height=height,
|
| 77 |
+
generator=generator
|
| 78 |
+
).images[0]
|
| 79 |
+
image_b64 = pil_to_base64(image)
|
| 80 |
+
return image, seed, image_b64
|
| 81 |
+
|
| 82 |
+
except RuntimeError as e:
|
| 83 |
+
print(f"Error during generation: {e}")
|
| 84 |
+
blank_image = Image.new('RGB', (width, height), color=(0, 0, 0))
|
| 85 |
+
blank_b64 = pil_to_base64(blank_image)
|
| 86 |
+
return blank_image, seed, blank_b64
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
css = """
|
| 90 |
+
#col-container {
|
| 91 |
+
margin: 0 auto;
|
| 92 |
+
max-width: 1024px;
|
| 93 |
+
}
|
| 94 |
+
"""
|
| 95 |
+
|
| 96 |
+
with gr.Blocks(css=css) as demo:
|
| 97 |
+
image_b64_state = gr.State()
|
| 98 |
+
|
| 99 |
+
with gr.Column(elem_id="col-container"):
|
| 100 |
+
with gr.Row():
|
| 101 |
+
# --- 変更点: formatを"jpeg"に変更 ---
|
| 102 |
+
result = gr.Image(format="jpeg", label="Result", show_label=False, interactive=False)
|
| 103 |
+
copy_button = gr.Button("Copy", scale=0, variant="secondary")
|
| 104 |
+
|
| 105 |
+
gr.Markdown("<br>" * 5)
|
| 106 |
+
|
| 107 |
+
with gr.Row():
|
| 108 |
+
prompt = gr.Text(
|
| 109 |
+
label="Prompt", show_label=False, max_lines=1,
|
| 110 |
+
placeholder="Enter your prompt",
|
| 111 |
+
value="", container=False,
|
| 112 |
+
)
|
| 113 |
+
run_button = gr.Button("Generate", scale=0, interactive=False)
|
| 114 |
+
consecutive_button = gr.Button("Consecutive", scale=0, interactive=False)
|
| 115 |
+
stop_button = gr.Button("Stop", scale=0, visible=True, interactive=True)
|
| 116 |
+
clear_button = gr.Button("Trash", scale=0, variant="secondary")
|
| 117 |
+
|
| 118 |
+
with gr.Accordion("Advanced Settings", open=False):
|
| 119 |
+
negative_prompt = gr.Text(
|
| 120 |
+
label="Negative prompt", max_lines=1, placeholder="Enter a negative prompt",
|
| 121 |
+
value="bad anatomy, bad quality, low quality, worst quality, worst detail"
|
| 122 |
+
)
|
| 123 |
+
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
|
| 124 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 125 |
+
with gr.Row():
|
| 126 |
+
width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024)
|
| 127 |
+
height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024)
|
| 128 |
+
with gr.Row():
|
| 129 |
+
guidance_scale = gr.Slider(label="Guidance scale", minimum=0.0, maximum=20.0, step=0.1, value=7)
|
| 130 |
+
num_inference_steps = gr.Slider(label="Number of inference steps", minimum=1, maximum=28, step=1, value=25)
|
| 131 |
+
|
| 132 |
+
interval_seconds = gr.Slider(label="Interval (seconds)", minimum=1, maximum=60, step=1, value=1)
|
| 133 |
+
gr.Markdown("<br>" * 20)
|
| 134 |
+
gr.Examples(
|
| 135 |
+
examples=[
|
| 136 |
+
["masterpiece, solo, A little girl with blonde short side tails, red eyes, "],
|
| 137 |
+
],
|
| 138 |
+
inputs=[prompt],
|
| 139 |
+
label="Examples (Click to copy to prompt)"
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
def generation_loop(prompt, negative_prompt, current_seed, randomize_seed, width, height, guidance_scale, num_inference_steps, interval_sec):
|
| 143 |
+
if not prompt.strip():
|
| 144 |
+
raise gr.Error("Prompt cannot be empty to start consecutive generation.")
|
| 145 |
+
while True:
|
| 146 |
+
try:
|
| 147 |
+
image, new_seed, image_b64 = infer(prompt, negative_prompt, current_seed, True, width, height, guidance_scale, num_inference_steps)
|
| 148 |
+
yield {result: image, seed: new_seed, image_b64_state: image_b64}
|
| 149 |
+
time.sleep(interval_sec)
|
| 150 |
+
except gr.exceptions.CancelledError:
|
| 151 |
+
print("Generation loop cancelled by user.")
|
| 152 |
+
break
|
| 153 |
+
|
| 154 |
+
prompt.input(
|
| 155 |
+
fn=None,
|
| 156 |
+
inputs=[prompt],
|
| 157 |
+
outputs=[run_button, consecutive_button],
|
| 158 |
+
js="(p) => { const interactive = p.trim().length > 0; return [{ interactive: interactive, '__type__': 'update' }, { interactive: interactive, '__type__': 'update' }]; }"
|
| 159 |
+
)
|
| 160 |
+
|
| 161 |
+
clear_button.click(
|
| 162 |
+
fn=None,
|
| 163 |
+
inputs=None,
|
| 164 |
+
outputs=[prompt, run_button, consecutive_button],
|
| 165 |
+
js="""
|
| 166 |
+
function() {
|
| 167 |
+
return ["", { "interactive": false, "__type__": "update" }, { "interactive": false, "__type__": "update" }];
|
| 168 |
+
}
|
| 169 |
+
"""
|
| 170 |
+
)
|
| 171 |
+
|
| 172 |
+
run_button.click(
|
| 173 |
+
fn=infer,
|
| 174 |
+
inputs=[prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
| 175 |
+
outputs=[result, seed, image_b64_state]
|
| 176 |
+
)
|
| 177 |
+
|
| 178 |
+
gen_inputs = [
|
| 179 |
+
prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps,
|
| 180 |
+
interval_seconds
|
| 181 |
+
]
|
| 182 |
+
|
| 183 |
+
consecutive_event = consecutive_button.click(
|
| 184 |
+
fn=generation_loop,
|
| 185 |
+
inputs=gen_inputs,
|
| 186 |
+
outputs=[result, seed, image_b64_state]
|
| 187 |
+
)
|
| 188 |
+
|
| 189 |
+
stop_button.click(
|
| 190 |
+
fn=None,
|
| 191 |
+
inputs=None,
|
| 192 |
+
outputs=None,
|
| 193 |
+
cancels=[consecutive_event]
|
| 194 |
+
)
|
| 195 |
+
|
| 196 |
+
copy_button.click(
|
| 197 |
+
fn=None,
|
| 198 |
+
inputs=[image_b64_state],
|
| 199 |
+
outputs=None,
|
| 200 |
+
js="""
|
| 201 |
+
async (b64) => {
|
| 202 |
+
if (!b64) { return; }
|
| 203 |
+
try {
|
| 204 |
+
const blob = await fetch(b64).then(res => res.blob());
|
| 205 |
+
await navigator.clipboard.write([
|
| 206 |
+
new ClipboardItem({ [blob.type]: blob })
|
| 207 |
+
]);
|
| 208 |
+
console.log('Image copied to clipboard');
|
| 209 |
+
} catch (err) {
|
| 210 |
+
console.error('Failed to copy image:', err);
|
| 211 |
+
}
|
| 212 |
+
}
|
| 213 |
+
"""
|
| 214 |
+
)
|
| 215 |
+
|
| 216 |
+
demo.queue().launch()
|
requirements.txt
CHANGED
|
@@ -6,4 +6,4 @@ transformers==4.56.2
|
|
| 6 |
xformers==0.0.32.post2
|
| 7 |
compel==2.1.1
|
| 8 |
pydantic==2.10.6
|
| 9 |
-
gradio
|
|
|
|
| 6 |
xformers==0.0.32.post2
|
| 7 |
compel==2.1.1
|
| 8 |
pydantic==2.10.6
|
| 9 |
+
gradio
|