Spaces:
Running
on
Zero
Running
on
Zero
Upload app.py
Browse files
app.py
CHANGED
@@ -1,92 +1,137 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
|
|
|
|
3 |
import random
|
4 |
-
|
5 |
-
|
6 |
-
from diffusers import DiffusionPipeline
|
7 |
import torch
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
MAX_SEED = np.iinfo(np.int32).max
|
21 |
-
MAX_IMAGE_SIZE =
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
36 |
if randomize_seed:
|
37 |
seed = random.randint(0, MAX_SEED)
|
38 |
|
39 |
-
generator = torch.Generator().manual_seed(seed)
|
40 |
-
|
41 |
-
|
42 |
-
prompt
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
-
return image, seed
|
52 |
-
|
53 |
-
|
54 |
-
examples = [
|
55 |
-
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
56 |
-
"An astronaut riding a green horse",
|
57 |
-
"A delicious ceviche cheesecake slice",
|
58 |
-
]
|
59 |
|
60 |
css = """
|
61 |
#col-container {
|
62 |
margin: 0 auto;
|
63 |
-
max-width:
|
64 |
}
|
65 |
"""
|
66 |
|
67 |
with gr.Blocks(css=css) as demo:
|
|
|
68 |
with gr.Column(elem_id="col-container"):
|
69 |
-
gr.Markdown(" # Text-to-Image Gradio Template")
|
70 |
|
71 |
with gr.Row():
|
72 |
prompt = gr.Text(
|
73 |
label="Prompt",
|
74 |
show_label=False,
|
75 |
max_lines=1,
|
76 |
-
placeholder="Enter your prompt",
|
77 |
container=False,
|
78 |
)
|
79 |
|
80 |
-
run_button = gr.Button("Run", scale=0
|
81 |
-
|
82 |
-
result = gr.Image(label="Result", show_label=False)
|
83 |
|
|
|
|
|
84 |
with gr.Accordion("Advanced Settings", open=False):
|
|
|
85 |
negative_prompt = gr.Text(
|
86 |
label="Negative prompt",
|
87 |
max_lines=1,
|
88 |
placeholder="Enter a negative prompt",
|
89 |
-
|
|
|
90 |
)
|
91 |
|
92 |
seed = gr.Slider(
|
@@ -105,7 +150,7 @@ with gr.Blocks(css=css) as demo:
|
|
105 |
minimum=256,
|
106 |
maximum=MAX_IMAGE_SIZE,
|
107 |
step=32,
|
108 |
-
value=1024,
|
109 |
)
|
110 |
|
111 |
height = gr.Slider(
|
@@ -113,42 +158,30 @@ with gr.Blocks(css=css) as demo:
|
|
113 |
minimum=256,
|
114 |
maximum=MAX_IMAGE_SIZE,
|
115 |
step=32,
|
116 |
-
value=
|
117 |
)
|
118 |
|
119 |
with gr.Row():
|
120 |
guidance_scale = gr.Slider(
|
121 |
label="Guidance scale",
|
122 |
minimum=0.0,
|
123 |
-
maximum=
|
124 |
step=0.1,
|
125 |
-
value=
|
126 |
)
|
127 |
|
128 |
num_inference_steps = gr.Slider(
|
129 |
label="Number of inference steps",
|
130 |
minimum=1,
|
131 |
-
maximum=
|
132 |
step=1,
|
133 |
-
value=
|
134 |
)
|
135 |
|
136 |
-
|
137 |
-
gr.on(
|
138 |
-
triggers=[run_button.click, prompt.submit],
|
139 |
fn=infer,
|
140 |
-
inputs=[
|
141 |
-
|
142 |
-
negative_prompt,
|
143 |
-
seed,
|
144 |
-
randomize_seed,
|
145 |
-
width,
|
146 |
-
height,
|
147 |
-
guidance_scale,
|
148 |
-
num_inference_steps,
|
149 |
-
],
|
150 |
-
outputs=[result, seed],
|
151 |
)
|
152 |
|
153 |
-
|
154 |
-
demo.launch()
|
|
|
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 |
+
hf_token=os.getenv("HUGGING_FACE_TOKEN"))
|
12 |
+
|
13 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
14 |
+
|
15 |
+
# Make sure to use torch.float16 consistently throughout the pipeline
|
16 |
+
pipe = StableDiffusionXLPipeline.from_pretrained(
|
17 |
+
"dhead/wai-nsfw-illustrious-sdxl-v140-sdxl",
|
18 |
+
torch_dtype=torch.float16,
|
19 |
+
variant="fp16", # Explicitly use fp16 variant
|
20 |
+
use_safetensors=True # Use safetensors if available
|
21 |
+
)
|
22 |
+
|
23 |
+
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
24 |
+
pipe.to(device)
|
25 |
+
|
26 |
+
# Force all components to use the same dtype
|
27 |
+
pipe.text_encoder.to(torch.float16)
|
28 |
+
pipe.text_encoder_2.to(torch.float16)
|
29 |
+
pipe.vae.to(torch.float16)
|
30 |
+
pipe.unet.to(torch.float16)
|
31 |
+
|
32 |
+
# 追加: Initialize Compel for long prompt processing
|
33 |
+
compel = Compel(
|
34 |
+
tokenizer=[pipe.tokenizer, pipe.tokenizer_2],
|
35 |
+
text_encoder=[pipe.text_encoder, pipe.text_encoder_2],
|
36 |
+
returned_embeddings_type=ReturnedEmbeddingsType.PENULTIMATE_HIDDEN_STATES_NON_NORMALIZED,
|
37 |
+
requires_pooled=[False, True],
|
38 |
+
truncate_long_prompts=False
|
39 |
+
)
|
40 |
|
41 |
MAX_SEED = np.iinfo(np.int32).max
|
42 |
+
MAX_IMAGE_SIZE = 1216
|
43 |
+
|
44 |
+
# 追加: Simple long prompt processing function
|
45 |
+
def process_long_prompt(prompt, negative_prompt=""):
|
46 |
+
"""Simple long prompt processing using Compel"""
|
47 |
+
try:
|
48 |
+
conditioning, pooled = compel([prompt, negative_prompt])
|
49 |
+
return conditioning, pooled
|
50 |
+
except Exception as e:
|
51 |
+
print(f"Long prompt processing failed: {e}, falling back to standard processing")
|
52 |
+
return None, None
|
53 |
+
|
54 |
+
@spaces.GPU
|
55 |
+
def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
|
56 |
+
# 変更: Remove the 60-word limit warning and add long prompt check
|
57 |
+
use_long_prompt = len(prompt.split()) > 60 or len(prompt) > 300
|
58 |
+
|
59 |
if randomize_seed:
|
60 |
seed = random.randint(0, MAX_SEED)
|
61 |
|
62 |
+
generator = torch.Generator(device=device).manual_seed(seed)
|
63 |
+
|
64 |
+
try:
|
65 |
+
# 追加: Try long prompt processing first if prompt is long
|
66 |
+
if use_long_prompt:
|
67 |
+
print("Using long prompt processing...")
|
68 |
+
conditioning, pooled = process_long_prompt(prompt, negative_prompt)
|
69 |
+
|
70 |
+
if conditioning is not None:
|
71 |
+
output_image = pipe(
|
72 |
+
prompt_embeds=conditioning[0:1],
|
73 |
+
pooled_prompt_embeds=pooled[0:1],
|
74 |
+
negative_prompt_embeds=conditioning[1:2],
|
75 |
+
negative_pooled_prompt_embeds=pooled[1:2],
|
76 |
+
guidance_scale=guidance_scale,
|
77 |
+
num_inference_steps=num_inference_steps,
|
78 |
+
width=width,
|
79 |
+
height=height,
|
80 |
+
generator=generator
|
81 |
+
).images[0]
|
82 |
+
return output_image
|
83 |
+
|
84 |
+
# Fall back to standard processing
|
85 |
+
output_image = pipe(
|
86 |
+
prompt=prompt,
|
87 |
+
negative_prompt=negative_prompt,
|
88 |
+
guidance_scale=guidance_scale,
|
89 |
+
num_inference_steps=num_inference_steps,
|
90 |
+
width=width,
|
91 |
+
height=height,
|
92 |
+
generator=generator
|
93 |
+
).images[0]
|
94 |
+
|
95 |
+
return output_image
|
96 |
+
except RuntimeError as e:
|
97 |
+
print(f"Error during generation: {e}")
|
98 |
+
# Return a blank image with error message
|
99 |
+
error_img = Image.new('RGB', (width, height), color=(0, 0, 0))
|
100 |
+
return error_img
|
101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
|
103 |
css = """
|
104 |
#col-container {
|
105 |
margin: 0 auto;
|
106 |
+
max-width: 1024px;
|
107 |
}
|
108 |
"""
|
109 |
|
110 |
with gr.Blocks(css=css) as demo:
|
111 |
+
|
112 |
with gr.Column(elem_id="col-container"):
|
|
|
113 |
|
114 |
with gr.Row():
|
115 |
prompt = gr.Text(
|
116 |
label="Prompt",
|
117 |
show_label=False,
|
118 |
max_lines=1,
|
119 |
+
placeholder="Enter your prompt (long prompts are automatically supported)",
|
120 |
container=False,
|
121 |
)
|
122 |
|
123 |
+
run_button = gr.Button("Run", scale=0)
|
|
|
|
|
124 |
|
125 |
+
result = gr.Image(format="png", label="Result", show_label=False)
|
126 |
+
|
127 |
with gr.Accordion("Advanced Settings", open=False):
|
128 |
+
|
129 |
negative_prompt = gr.Text(
|
130 |
label="Negative prompt",
|
131 |
max_lines=1,
|
132 |
placeholder="Enter a negative prompt",
|
133 |
+
# value="bad quality,worst quality,worst detail,sketch,censor,"
|
134 |
+
value="monochrome, (low quality, worst quality:1.2), very displeasing, 3d, watermark, signature, ugly, poorly drawn,"
|
135 |
)
|
136 |
|
137 |
seed = gr.Slider(
|
|
|
150 |
minimum=256,
|
151 |
maximum=MAX_IMAGE_SIZE,
|
152 |
step=32,
|
153 |
+
value=1024,
|
154 |
)
|
155 |
|
156 |
height = gr.Slider(
|
|
|
158 |
minimum=256,
|
159 |
maximum=MAX_IMAGE_SIZE,
|
160 |
step=32,
|
161 |
+
value=MAX_IMAGE_SIZE,
|
162 |
)
|
163 |
|
164 |
with gr.Row():
|
165 |
guidance_scale = gr.Slider(
|
166 |
label="Guidance scale",
|
167 |
minimum=0.0,
|
168 |
+
maximum=20.0,
|
169 |
step=0.1,
|
170 |
+
value=7,
|
171 |
)
|
172 |
|
173 |
num_inference_steps = gr.Slider(
|
174 |
label="Number of inference steps",
|
175 |
minimum=1,
|
176 |
+
maximum=28,
|
177 |
step=1,
|
178 |
+
value=28,
|
179 |
)
|
180 |
|
181 |
+
run_button.click(
|
|
|
|
|
182 |
fn=infer,
|
183 |
+
inputs=[prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
184 |
+
outputs=[result]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
185 |
)
|
186 |
|
187 |
+
demo.queue().launch()
|
|