Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -10,7 +10,7 @@ import spaces
|
|
10 |
import torch
|
11 |
|
12 |
from diffusers import DiffusionPipeline
|
13 |
-
from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
|
14 |
from typing import Tuple
|
15 |
|
16 |
bad_words = json.loads(os.getenv('BAD_WORDS', "[]"))
|
@@ -27,19 +27,16 @@ def check_text(prompt, negative=""):
|
|
27 |
return False
|
28 |
|
29 |
style_list = [
|
30 |
-
|
31 |
{
|
32 |
"name": "Photo",
|
33 |
"prompt": "cinematic photo {prompt}. 35mm photograph, film, bokeh, professional, 4k, highly detailed",
|
34 |
"negative_prompt": "drawing, painting, crayon, sketch, graphite, impressionist, noisy, blurry, soft, deformed, ugly",
|
35 |
-
},
|
36 |
-
|
37 |
{
|
38 |
"name": "Cinematic",
|
39 |
"prompt": "cinematic still {prompt}. emotional, harmonious, vignette, highly detailed, high budget, bokeh, cinemascope, moody, epic, gorgeous, film grain, grainy",
|
40 |
"negative_prompt": "anime, cartoon, graphic, text, painting, crayon, graphite, abstract, glitch, deformed, mutated, ugly, disfigured",
|
41 |
},
|
42 |
-
|
43 |
{
|
44 |
"name": "Anime",
|
45 |
"prompt": "anime artwork {prompt}. anime style, key visual, vibrant, studio anime, highly detailed",
|
@@ -67,7 +64,7 @@ def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str
|
|
67 |
negative = ""
|
68 |
return p.replace("{prompt}", positive), n + negative
|
69 |
|
70 |
-
DESCRIPTION = """##
|
71 |
|
72 |
"""
|
73 |
|
@@ -103,10 +100,10 @@ if torch.cuda.is_available():
|
|
103 |
pipe.enable_model_cpu_offload()
|
104 |
pipe2.enable_model_cpu_offload()
|
105 |
else:
|
106 |
-
pipe.to(device)
|
107 |
-
pipe2.to(device)
|
108 |
print("Loaded on Device!")
|
109 |
-
|
110 |
if USE_TORCH_COMPILE:
|
111 |
pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
|
112 |
pipe2.unet = torch.compile(pipe2.unet, mode="reduce-overhead", fullgraph=True)
|
@@ -123,6 +120,7 @@ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
|
123 |
return seed
|
124 |
|
125 |
@spaces.GPU(duration=30)
|
|
|
126 |
def generate(
|
127 |
prompt: str,
|
128 |
negative_prompt: str = "",
|
@@ -133,34 +131,57 @@ def generate(
|
|
133 |
height: int = 1024,
|
134 |
guidance_scale: float = 3,
|
135 |
randomize_seed: bool = False,
|
136 |
-
use_resolution_binning: bool = True,
|
137 |
progress=gr.Progress(track_tqdm=True),
|
138 |
):
|
139 |
if check_text(prompt, negative_prompt):
|
140 |
raise ValueError("Prompt contains restricted words.")
|
|
|
|
|
141 |
|
142 |
-
|
143 |
-
|
144 |
-
|
|
|
|
|
|
|
|
|
145 |
|
146 |
-
|
147 |
-
|
148 |
-
negative_prompt += default_negative
|
149 |
|
150 |
options = {
|
151 |
"prompt": prompt,
|
152 |
-
"negative_prompt":
|
153 |
"width": width,
|
154 |
"height": height,
|
155 |
"guidance_scale": guidance_scale,
|
156 |
-
"num_inference_steps": 25,
|
157 |
"generator": generator,
|
158 |
-
"num_images_per_prompt": NUM_IMAGES_PER_PROMPT,
|
159 |
-
"use_resolution_binning": use_resolution_binning,
|
160 |
"output_type": "pil",
|
161 |
}
|
162 |
|
163 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
164 |
|
165 |
image_paths = [save_image(img) for img in images]
|
166 |
return image_paths, seed
|
@@ -175,14 +196,14 @@ examples = [
|
|
175 |
|
176 |
css = '''
|
177 |
.gradio-container {
|
178 |
-
max-width: 590px !important;
|
179 |
-
margin: 0 auto !important;
|
180 |
}
|
181 |
h1 {
|
182 |
-
text-align: center;
|
183 |
}
|
184 |
footer {
|
185 |
-
visibility: hidden;
|
186 |
}
|
187 |
'''
|
188 |
with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
@@ -196,31 +217,43 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
|
196 |
container=False,
|
197 |
)
|
198 |
run_button = gr.Button("Run", scale=0, variant="primary")
|
199 |
-
result = gr.Gallery(label="Result", columns=1, preview=True)
|
|
|
200 |
with gr.Accordion("Advanced options", open=False):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
201 |
use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True, visible=True)
|
202 |
negative_prompt = gr.Text(
|
203 |
label="Negative prompt",
|
204 |
max_lines=1,
|
205 |
-
placeholder="Enter a negative prompt",
|
206 |
value="(deformed iris, deformed pupils, semi-realistic, cgi, 3d, render, sketch, cartoon, drawing, anime:1.4), text, close up, cropped, out of frame, worst quality, low quality, jpeg artifacts, ugly, duplicate, morbid, mutilated, extra fingers, mutated hands, poorly drawn hands, poorly drawn face, mutation, deformed, blurry, dehydrated, bad anatomy, bad proportions, extra limbs, cloned face, disfigured, gross proportions, malformed limbs, missing arms, missing legs, extra arms, extra legs, fused fingers, too many fingers, long neck",
|
207 |
visible=True,
|
208 |
)
|
|
|
|
|
|
|
209 |
with gr.Row():
|
210 |
-
num_inference_steps = gr.Slider(
|
211 |
-
label="Steps",
|
212 |
minimum=10,
|
213 |
maximum=60,
|
214 |
step=1,
|
215 |
-
value=20,
|
216 |
)
|
217 |
with gr.Row():
|
218 |
-
num_images_per_prompt = gr.Slider(
|
219 |
-
label="Images",
|
220 |
minimum=1,
|
221 |
maximum=4,
|
222 |
step=1,
|
223 |
-
value=2,
|
224 |
)
|
225 |
seed = gr.Slider(
|
226 |
label="Seed",
|
@@ -235,14 +268,14 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
|
235 |
width = gr.Slider(
|
236 |
label="Width",
|
237 |
minimum=512,
|
238 |
-
maximum=
|
239 |
step=8,
|
240 |
value=1024,
|
241 |
)
|
242 |
height = gr.Slider(
|
243 |
label="Height",
|
244 |
minimum=512,
|
245 |
-
maximum=
|
246 |
step=8,
|
247 |
value=1024,
|
248 |
)
|
@@ -254,19 +287,13 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
|
254 |
step=0.1,
|
255 |
value=3.0,
|
256 |
)
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
container=True,
|
261 |
-
interactive=True,
|
262 |
-
choices=STYLE_NAMES,
|
263 |
-
value=DEFAULT_STYLE_NAME,
|
264 |
-
label="Image Style",
|
265 |
-
)
|
266 |
gr.Examples(
|
267 |
examples=examples,
|
268 |
inputs=prompt,
|
269 |
-
outputs=[result, seed],
|
270 |
fn=generate,
|
271 |
cache_examples=CACHE_EXAMPLES,
|
272 |
)
|
@@ -281,7 +308,7 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
|
281 |
gr.on(
|
282 |
triggers=[
|
283 |
prompt.submit,
|
284 |
-
negative_prompt.submit,
|
285 |
run_button.click,
|
286 |
],
|
287 |
fn=generate,
|
@@ -289,7 +316,7 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
|
289 |
prompt,
|
290 |
negative_prompt,
|
291 |
use_negative_prompt,
|
292 |
-
style_selection,
|
293 |
seed,
|
294 |
width,
|
295 |
height,
|
@@ -301,4 +328,12 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
|
301 |
)
|
302 |
|
303 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
304 |
demo.queue(max_size=20).launch(ssr_mode=True, show_error=True, share=True)
|
|
|
10 |
import torch
|
11 |
|
12 |
from diffusers import DiffusionPipeline
|
13 |
+
from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler # EulerAncestralDiscreteScheduler not explicitly used but imported
|
14 |
from typing import Tuple
|
15 |
|
16 |
bad_words = json.loads(os.getenv('BAD_WORDS', "[]"))
|
|
|
27 |
return False
|
28 |
|
29 |
style_list = [
|
|
|
30 |
{
|
31 |
"name": "Photo",
|
32 |
"prompt": "cinematic photo {prompt}. 35mm photograph, film, bokeh, professional, 4k, highly detailed",
|
33 |
"negative_prompt": "drawing, painting, crayon, sketch, graphite, impressionist, noisy, blurry, soft, deformed, ugly",
|
34 |
+
},
|
|
|
35 |
{
|
36 |
"name": "Cinematic",
|
37 |
"prompt": "cinematic still {prompt}. emotional, harmonious, vignette, highly detailed, high budget, bokeh, cinemascope, moody, epic, gorgeous, film grain, grainy",
|
38 |
"negative_prompt": "anime, cartoon, graphic, text, painting, crayon, graphite, abstract, glitch, deformed, mutated, ugly, disfigured",
|
39 |
},
|
|
|
40 |
{
|
41 |
"name": "Anime",
|
42 |
"prompt": "anime artwork {prompt}. anime style, key visual, vibrant, studio anime, highly detailed",
|
|
|
64 |
negative = ""
|
65 |
return p.replace("{prompt}", positive), n + negative
|
66 |
|
67 |
+
DESCRIPTION = """## SDXL Image Generation
|
68 |
|
69 |
"""
|
70 |
|
|
|
100 |
pipe.enable_model_cpu_offload()
|
101 |
pipe2.enable_model_cpu_offload()
|
102 |
else:
|
103 |
+
pipe.to(device)
|
104 |
+
pipe2.to(device)
|
105 |
print("Loaded on Device!")
|
106 |
+
|
107 |
if USE_TORCH_COMPILE:
|
108 |
pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
|
109 |
pipe2.unet = torch.compile(pipe2.unet, mode="reduce-overhead", fullgraph=True)
|
|
|
120 |
return seed
|
121 |
|
122 |
@spaces.GPU(duration=30)
|
123 |
+
@torch.no_grad()
|
124 |
def generate(
|
125 |
prompt: str,
|
126 |
negative_prompt: str = "",
|
|
|
131 |
height: int = 1024,
|
132 |
guidance_scale: float = 3,
|
133 |
randomize_seed: bool = False,
|
134 |
+
use_resolution_binning: bool = True, # This parameter is not exposed in the UI by default
|
135 |
progress=gr.Progress(track_tqdm=True),
|
136 |
):
|
137 |
if check_text(prompt, negative_prompt):
|
138 |
raise ValueError("Prompt contains restricted words.")
|
139 |
+
|
140 |
+
prompt, negative_prompt_from_style = apply_style(style, prompt, "") # Apply style positive first
|
141 |
|
142 |
+
# Combine negative prompts
|
143 |
+
if use_negative_prompt:
|
144 |
+
final_negative_prompt = negative_prompt_from_style + " " + negative_prompt + " " + default_negative
|
145 |
+
else:
|
146 |
+
final_negative_prompt = negative_prompt_from_style + " " + default_negative
|
147 |
+
final_negative_prompt = final_negative_prompt.strip()
|
148 |
+
|
149 |
|
150 |
+
seed = int(randomize_seed_fn(seed, randomize_seed))
|
151 |
+
generator = torch.Generator(device=device).manual_seed(seed) # Ensure generator is on the correct device
|
|
|
152 |
|
153 |
options = {
|
154 |
"prompt": prompt,
|
155 |
+
"negative_prompt": final_negative_prompt,
|
156 |
"width": width,
|
157 |
"height": height,
|
158 |
"guidance_scale": guidance_scale,
|
159 |
+
"num_inference_steps": 25, # This is hardcoded, UI slider for steps is not connected
|
160 |
"generator": generator,
|
161 |
+
"num_images_per_prompt": NUM_IMAGES_PER_PROMPT, # UI slider for images is not connected to this
|
162 |
+
# "use_resolution_binning": use_resolution_binning, # This was in original code, but not defined. Diffusers handles it.
|
163 |
"output_type": "pil",
|
164 |
}
|
165 |
|
166 |
+
# If on CPU, ensure generator is for CPU
|
167 |
+
if device.type == 'cpu':
|
168 |
+
generator = torch.Generator(device='cpu').manual_seed(seed)
|
169 |
+
options["generator"] = generator
|
170 |
+
|
171 |
+
images = []
|
172 |
+
if 'pipe' in globals(): # Check if pipes are loaded (i.e. on GPU)
|
173 |
+
images.extend(pipe(**options).images)
|
174 |
+
images.extend(pipe2(**options).images)
|
175 |
+
else: # Fallback for CPU or if pipes are not loaded (though the DESCRIPTION warns about CPU)
|
176 |
+
# This part would need a CPU-compatible pipeline if one isn't loaded.
|
177 |
+
# For now, it will likely error if pipe/pipe2 aren't available.
|
178 |
+
# Or, we can return a placeholder or raise a specific error.
|
179 |
+
# To prevent errors if running without GPU and models didn't load:
|
180 |
+
placeholder_image = Image.new('RGB', (width, height), color = 'grey')
|
181 |
+
draw = ImageDraw.Draw(placeholder_image)
|
182 |
+
draw.text((10, 10), "GPU models not loaded. Cannot generate image.", fill=(255,0,0))
|
183 |
+
images.append(placeholder_image)
|
184 |
+
|
185 |
|
186 |
image_paths = [save_image(img) for img in images]
|
187 |
return image_paths, seed
|
|
|
196 |
|
197 |
css = '''
|
198 |
.gradio-container {
|
199 |
+
max-width: 590px !important; /* Existing style */
|
200 |
+
margin: 0 auto !important; /* Existing style */
|
201 |
}
|
202 |
h1 {
|
203 |
+
text-align: center; /* Existing style */
|
204 |
}
|
205 |
footer {
|
206 |
+
visibility: hidden; /* Existing style */
|
207 |
}
|
208 |
'''
|
209 |
with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
|
|
217 |
container=False,
|
218 |
)
|
219 |
run_button = gr.Button("Run", scale=0, variant="primary")
|
220 |
+
result = gr.Gallery(label="Result", columns=1, preview=True) # columns=1 for single image below each other if multiple
|
221 |
+
|
222 |
with gr.Accordion("Advanced options", open=False):
|
223 |
+
style_selection = gr.Dropdown( # MODIFIED: Was gr.Radio, moved into accordion
|
224 |
+
label="Image Style",
|
225 |
+
choices=STYLE_NAMES,
|
226 |
+
value=DEFAULT_STYLE_NAME,
|
227 |
+
interactive=True,
|
228 |
+
show_label=True,
|
229 |
+
container=True,
|
230 |
+
)
|
231 |
use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True, visible=True)
|
232 |
negative_prompt = gr.Text(
|
233 |
label="Negative prompt",
|
234 |
max_lines=1,
|
235 |
+
placeholder="Enter a negative prompt (appended to style's negative)",
|
236 |
value="(deformed iris, deformed pupils, semi-realistic, cgi, 3d, render, sketch, cartoon, drawing, anime:1.4), text, close up, cropped, out of frame, worst quality, low quality, jpeg artifacts, ugly, duplicate, morbid, mutilated, extra fingers, mutated hands, poorly drawn hands, poorly drawn face, mutation, deformed, blurry, dehydrated, bad anatomy, bad proportions, extra limbs, cloned face, disfigured, gross proportions, malformed limbs, missing arms, missing legs, extra arms, extra legs, fused fingers, too many fingers, long neck",
|
237 |
visible=True,
|
238 |
)
|
239 |
+
# Note: num_inference_steps and num_images_per_prompt sliders are defined in UI
|
240 |
+
# but not wired to the generate function's parameters that control these aspects.
|
241 |
+
# Keeping them as is, per "Don't alter the remaining functionality".
|
242 |
with gr.Row():
|
243 |
+
num_inference_steps = gr.Slider( # This UI element is not connected to the backend
|
244 |
+
label="Steps (Not Connected)",
|
245 |
minimum=10,
|
246 |
maximum=60,
|
247 |
step=1,
|
248 |
+
value=20, # Default value in UI
|
249 |
)
|
250 |
with gr.Row():
|
251 |
+
num_images_per_prompt = gr.Slider( # This UI element is not connected to the backend
|
252 |
+
label="Images (Not Connected)",
|
253 |
minimum=1,
|
254 |
maximum=4,
|
255 |
step=1,
|
256 |
+
value=2, # Default value in UI (backend NUM_IMAGES_PER_PROMPT is 1, resulting in 2 total)
|
257 |
)
|
258 |
seed = gr.Slider(
|
259 |
label="Seed",
|
|
|
268 |
width = gr.Slider(
|
269 |
label="Width",
|
270 |
minimum=512,
|
271 |
+
maximum=MAX_IMAGE_SIZE, # Use MAX_IMAGE_SIZE
|
272 |
step=8,
|
273 |
value=1024,
|
274 |
)
|
275 |
height = gr.Slider(
|
276 |
label="Height",
|
277 |
minimum=512,
|
278 |
+
maximum=MAX_IMAGE_SIZE, # Use MAX_IMAGE_SIZE
|
279 |
step=8,
|
280 |
value=1024,
|
281 |
)
|
|
|
287 |
step=0.1,
|
288 |
value=3.0,
|
289 |
)
|
290 |
+
|
291 |
+
# Original style_selection gr.Row has been removed from here.
|
292 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
293 |
gr.Examples(
|
294 |
examples=examples,
|
295 |
inputs=prompt,
|
296 |
+
outputs=[result, seed], # seed output is good for reproducibility
|
297 |
fn=generate,
|
298 |
cache_examples=CACHE_EXAMPLES,
|
299 |
)
|
|
|
308 |
gr.on(
|
309 |
triggers=[
|
310 |
prompt.submit,
|
311 |
+
negative_prompt.submit, # Allow submitting negative prompt to trigger run
|
312 |
run_button.click,
|
313 |
],
|
314 |
fn=generate,
|
|
|
316 |
prompt,
|
317 |
negative_prompt,
|
318 |
use_negative_prompt,
|
319 |
+
style_selection, # style_selection is correctly in inputs
|
320 |
seed,
|
321 |
width,
|
322 |
height,
|
|
|
328 |
)
|
329 |
|
330 |
if __name__ == "__main__":
|
331 |
+
# For CPU execution, model loading might take time or fail if not handled.
|
332 |
+
# The `if torch.cuda.is_available():` block handles model loading for GPU.
|
333 |
+
# A CPU fallback for inference would require a CPU-compatible model or different handling in `generate`.
|
334 |
+
# The provided code primarily targets GPU.
|
335 |
+
# Added a basic placeholder image generation in `generate` if pipes are not loaded.
|
336 |
+
# Also need `ImageDraw` for that.
|
337 |
+
from PIL import ImageDraw # Add ImageDraw import for CPU placeholder
|
338 |
+
|
339 |
demo.queue(max_size=20).launch(ssr_mode=True, show_error=True, share=True)
|