Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -8,15 +8,37 @@ from PIL import Image
|
|
8 |
import spaces
|
9 |
import torch
|
10 |
from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
|
11 |
-
|
12 |
|
13 |
DESCRIPTIONx = """## STABLE HAMSTER
|
14 |
-
|
15 |
"""
|
16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
|
|
|
|
|
|
19 |
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
style_list = [
|
22 |
{
|
@@ -29,34 +51,11 @@ style_list = [
|
|
29 |
"prompt": "hyper-realistic 4K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
|
30 |
"negative_prompt": "cartoonish, low resolution, blurry, simplistic, abstract, deformed, ugly",
|
31 |
},
|
32 |
-
|
33 |
-
{
|
34 |
-
"name": "Photo",
|
35 |
-
"prompt": "cinematic photo {prompt}. 35mm photograph, film, bokeh, professional, 4k, highly detailed",
|
36 |
-
"negative_prompt": "drawing, painting, crayon, sketch, graphite, impressionist, noisy, blurry, soft, deformed, ugly",
|
37 |
-
},
|
38 |
-
|
39 |
-
{
|
40 |
-
"name": "Cinematic",
|
41 |
-
"prompt": "cinematic still {prompt}. emotional, harmonious, vignette, highly detailed, high budget, bokeh, cinemascope, moody, epic, gorgeous, film grain, grainy",
|
42 |
-
"negative_prompt": "anime, cartoon, graphic, text, painting, crayon, graphite, abstract, glitch, deformed, mutated, ugly, disfigured",
|
43 |
-
},
|
44 |
-
|
45 |
-
{
|
46 |
-
"name": "Anime",
|
47 |
-
"prompt": "anime artwork {prompt}. anime style, key visual, vibrant, studio anime, highly detailed",
|
48 |
-
"negative_prompt": "photo, deformed, black and white, realism, disfigured, low contrast",
|
49 |
-
},
|
50 |
{
|
51 |
"name": "3D Model",
|
52 |
"prompt": "professional 3d model {prompt}. octane render, highly detailed, volumetric, dramatic lighting",
|
53 |
"negative_prompt": "ugly, deformed, noisy, low poly, blurry, painting",
|
54 |
},
|
55 |
-
{
|
56 |
-
"name": "(No style)",
|
57 |
-
"prompt": "{prompt}",
|
58 |
-
"negative_prompt": "",
|
59 |
-
},
|
60 |
]
|
61 |
|
62 |
styles = {k["name"]: (k["prompt"], k["negative_prompt"]) for k in style_list}
|
@@ -69,37 +68,6 @@ def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str
|
|
69 |
negative = ""
|
70 |
return p.replace("{prompt}", positive), n + negative
|
71 |
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
# Use environment variables for flexibility
|
77 |
-
MODEL_ID = os.getenv("MODEL_REPO")
|
78 |
-
MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "4096"))
|
79 |
-
USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE", "0") == "1"
|
80 |
-
ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"
|
81 |
-
BATCH_SIZE = int(os.getenv("BATCH_SIZE", "1")) # Allow generating multiple images at once
|
82 |
-
|
83 |
-
# Determine device and load model outside of function for efficiency
|
84 |
-
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
85 |
-
pipe = StableDiffusionXLPipeline.from_pretrained(
|
86 |
-
MODEL_ID,
|
87 |
-
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
88 |
-
use_safetensors=True,
|
89 |
-
add_watermarker=False,
|
90 |
-
).to(device)
|
91 |
-
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
92 |
-
|
93 |
-
# Torch compile for potential speedup (experimental)
|
94 |
-
if USE_TORCH_COMPILE:
|
95 |
-
pipe.compile()
|
96 |
-
|
97 |
-
# CPU offloading for larger RAM capacity (experimental)
|
98 |
-
if ENABLE_CPU_OFFLOAD:
|
99 |
-
pipe.enable_model_cpu_offload()
|
100 |
-
|
101 |
-
MAX_SEED = np.iinfo(np.int32).max
|
102 |
-
|
103 |
def save_image(img):
|
104 |
unique_name = str(uuid.uuid4()) + ".png"
|
105 |
img.save(unique_name)
|
@@ -123,19 +91,21 @@ def generate(
|
|
123 |
randomize_seed: bool = False,
|
124 |
use_resolution_binning: bool = True,
|
125 |
num_images: int = 1, # Number of images to generate
|
|
|
126 |
progress=gr.Progress(track_tqdm=True),
|
127 |
):
|
|
|
128 |
seed = int(randomize_seed_fn(seed, randomize_seed))
|
129 |
generator = torch.Generator(device=device).manual_seed(seed)
|
130 |
|
131 |
# Improved options handling
|
132 |
options = {
|
133 |
-
"prompt": [prompt] * num_images,
|
134 |
-
"negative_prompt": [negative_prompt] * num_images if use_negative_prompt else None,
|
135 |
-
"width": width,
|
136 |
-
"height": height,
|
137 |
-
"guidance_scale": guidance_scale,
|
138 |
-
"num_inference_steps": num_inference_steps,
|
139 |
"generator": generator,
|
140 |
"output_type": "pil",
|
141 |
}
|
@@ -146,7 +116,7 @@ def generate(
|
|
146 |
|
147 |
# Generate images potentially in batches
|
148 |
images = []
|
149 |
-
for i in range(0, num_images, BATCH_SIZE):
|
150 |
batch_options = options.copy()
|
151 |
batch_options["prompt"] = options["prompt"][i:i+BATCH_SIZE]
|
152 |
if "negative_prompt" in batch_options:
|
@@ -242,8 +212,6 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
|
242 |
step=1,
|
243 |
value=8,
|
244 |
)
|
245 |
-
|
246 |
-
with gr.Row(visible=True):
|
247 |
style_selection = gr.Radio(
|
248 |
show_label=True,
|
249 |
container=True,
|
@@ -252,13 +220,11 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
|
252 |
value=DEFAULT_STYLE_NAME,
|
253 |
label="Image Style",
|
254 |
)
|
255 |
-
|
256 |
gr.Examples(
|
257 |
examples=examples,
|
258 |
inputs=prompt,
|
259 |
-
|
260 |
-
fn=generate,
|
261 |
-
cache_examples=CACHE_EXAMPLES,
|
262 |
)
|
263 |
|
264 |
use_negative_prompt.change(
|
@@ -279,14 +245,14 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
|
279 |
prompt,
|
280 |
negative_prompt,
|
281 |
use_negative_prompt,
|
282 |
-
style_selection,
|
283 |
seed,
|
284 |
width,
|
285 |
height,
|
286 |
guidance_scale,
|
287 |
num_inference_steps,
|
288 |
randomize_seed,
|
289 |
-
num_images
|
|
|
290 |
],
|
291 |
outputs=[result, seed],
|
292 |
api_name="run",
|
|
|
8 |
import spaces
|
9 |
import torch
|
10 |
from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
|
11 |
+
from typing import Tuple # Import Tuple from typing module
|
12 |
|
13 |
DESCRIPTIONx = """## STABLE HAMSTER
|
|
|
14 |
"""
|
15 |
|
16 |
+
# Use environment variables for flexibility
|
17 |
+
MODEL_ID = os.getenv("MODEL_REPO")
|
18 |
+
MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "4096"))
|
19 |
+
USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE", "0") == "1"
|
20 |
+
ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"
|
21 |
+
BATCH_SIZE = int(os.getenv("BATCH_SIZE", "1")) # Allow generating multiple images at once
|
22 |
|
23 |
+
# Determine device and load model outside of function for efficiency
|
24 |
+
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
25 |
+
pipe = StableDiffusionXLPipeline.from_pretrained(
|
26 |
+
MODEL_ID,
|
27 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
28 |
+
use_safetensors=True,
|
29 |
+
add_watermarker=False,
|
30 |
+
).to(device)
|
31 |
+
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
32 |
|
33 |
+
# Torch compile for potential speedup (experimental)
|
34 |
+
if USE_TORCH_COMPILE:
|
35 |
+
pipe.compile()
|
36 |
|
37 |
+
# CPU offloading for larger RAM capacity (experimental)
|
38 |
+
if ENABLE_CPU_OFFLOAD:
|
39 |
+
pipe.enable_model_cpu_offload()
|
40 |
+
|
41 |
+
MAX_SEED = np.iinfo(np.int32).max
|
42 |
|
43 |
style_list = [
|
44 |
{
|
|
|
51 |
"prompt": "hyper-realistic 4K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
|
52 |
"negative_prompt": "cartoonish, low resolution, blurry, simplistic, abstract, deformed, ugly",
|
53 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
{
|
55 |
"name": "3D Model",
|
56 |
"prompt": "professional 3d model {prompt}. octane render, highly detailed, volumetric, dramatic lighting",
|
57 |
"negative_prompt": "ugly, deformed, noisy, low poly, blurry, painting",
|
58 |
},
|
|
|
|
|
|
|
|
|
|
|
59 |
]
|
60 |
|
61 |
styles = {k["name"]: (k["prompt"], k["negative_prompt"]) for k in style_list}
|
|
|
68 |
negative = ""
|
69 |
return p.replace("{prompt}", positive), n + negative
|
70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
def save_image(img):
|
72 |
unique_name = str(uuid.uuid4()) + ".png"
|
73 |
img.save(unique_name)
|
|
|
91 |
randomize_seed: bool = False,
|
92 |
use_resolution_binning: bool = True,
|
93 |
num_images: int = 1, # Number of images to generate
|
94 |
+
style: str = DEFAULT_STYLE_NAME,
|
95 |
progress=gr.Progress(track_tqdm=True),
|
96 |
):
|
97 |
+
prompt, negative_prompt = apply_style(style, prompt, negative_prompt)
|
98 |
seed = int(randomize_seed_fn(seed, randomize_seed))
|
99 |
generator = torch.Generator(device=device).manual_seed(seed)
|
100 |
|
101 |
# Improved options handling
|
102 |
options = {
|
103 |
+
"prompt": [prompt] * int(num_images),
|
104 |
+
"negative_prompt": [negative_prompt] * int(num_images) if use_negative_prompt else None,
|
105 |
+
"width": int(width),
|
106 |
+
"height": int(height),
|
107 |
+
"guidance_scale": float(guidance_scale),
|
108 |
+
"num_inference_steps": int(num_inference_steps),
|
109 |
"generator": generator,
|
110 |
"output_type": "pil",
|
111 |
}
|
|
|
116 |
|
117 |
# Generate images potentially in batches
|
118 |
images = []
|
119 |
+
for i in range(0, int(num_images), BATCH_SIZE):
|
120 |
batch_options = options.copy()
|
121 |
batch_options["prompt"] = options["prompt"][i:i+BATCH_SIZE]
|
122 |
if "negative_prompt" in batch_options:
|
|
|
212 |
step=1,
|
213 |
value=8,
|
214 |
)
|
|
|
|
|
215 |
style_selection = gr.Radio(
|
216 |
show_label=True,
|
217 |
container=True,
|
|
|
220 |
value=DEFAULT_STYLE_NAME,
|
221 |
label="Image Style",
|
222 |
)
|
223 |
+
|
224 |
gr.Examples(
|
225 |
examples=examples,
|
226 |
inputs=prompt,
|
227 |
+
cache_examples=False
|
|
|
|
|
228 |
)
|
229 |
|
230 |
use_negative_prompt.change(
|
|
|
245 |
prompt,
|
246 |
negative_prompt,
|
247 |
use_negative_prompt,
|
|
|
248 |
seed,
|
249 |
width,
|
250 |
height,
|
251 |
guidance_scale,
|
252 |
num_inference_steps,
|
253 |
randomize_seed,
|
254 |
+
num_images,
|
255 |
+
style_selection
|
256 |
],
|
257 |
outputs=[result, seed],
|
258 |
api_name="run",
|