Spaces:
Running
on
Zero
Running
on
Zero
prithivMLmods
commited on
Commit
•
208bfe1
1
Parent(s):
68c6dd0
Update app.py
Browse files
app.py
CHANGED
@@ -8,14 +8,15 @@ 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 |
# Use environment variables for flexibility
|
17 |
MODEL_ID = os.getenv("MODEL_REPO")
|
18 |
-
MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "
|
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
|
@@ -40,34 +41,6 @@ if ENABLE_CPU_OFFLOAD:
|
|
40 |
|
41 |
MAX_SEED = np.iinfo(np.int32).max
|
42 |
|
43 |
-
style_list = [
|
44 |
-
{
|
45 |
-
"name": "3840 x 2160",
|
46 |
-
"prompt": "hyper-realistic 8K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
|
47 |
-
"negative_prompt": "cartoonish, low resolution, blurry, simplistic, abstract, deformed, ugly",
|
48 |
-
},
|
49 |
-
{
|
50 |
-
"name": "2560 x 1440",
|
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}
|
62 |
-
STYLE_NAMES = list(styles.keys())
|
63 |
-
DEFAULT_STYLE_NAME = "3840 x 2160"
|
64 |
-
|
65 |
-
def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str, str]:
|
66 |
-
p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
|
67 |
-
if not negative:
|
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,21 +64,19 @@ def generate(
|
|
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] *
|
104 |
-
"negative_prompt": [negative_prompt] *
|
105 |
-
"width":
|
106 |
-
"height":
|
107 |
-
"guidance_scale":
|
108 |
-
"num_inference_steps":
|
109 |
"generator": generator,
|
110 |
"output_type": "pil",
|
111 |
}
|
@@ -116,7 +87,7 @@ def generate(
|
|
116 |
|
117 |
# Generate images potentially in batches
|
118 |
images = []
|
119 |
-
for i in range(0,
|
120 |
batch_options = options.copy()
|
121 |
batch_options["prompt"] = options["prompt"][i:i+BATCH_SIZE]
|
122 |
if "negative_prompt" in batch_options:
|
@@ -212,14 +183,6 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
|
212 |
step=1,
|
213 |
value=8,
|
214 |
)
|
215 |
-
style_selection = gr.Radio(
|
216 |
-
show_label=True,
|
217 |
-
container=True,
|
218 |
-
interactive=True,
|
219 |
-
choices=STYLE_NAMES,
|
220 |
-
value=DEFAULT_STYLE_NAME,
|
221 |
-
label="Image Style",
|
222 |
-
)
|
223 |
|
224 |
gr.Examples(
|
225 |
examples=examples,
|
@@ -234,7 +197,6 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
|
234 |
api_name=False,
|
235 |
)
|
236 |
|
237 |
-
|
238 |
gr.on(
|
239 |
triggers=[
|
240 |
prompt.submit,
|
@@ -252,8 +214,7 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
|
252 |
guidance_scale,
|
253 |
num_inference_steps,
|
254 |
randomize_seed,
|
255 |
-
num_images
|
256 |
-
style_selection
|
257 |
],
|
258 |
outputs=[result, seed],
|
259 |
api_name="run",
|
|
|
8 |
import spaces
|
9 |
import torch
|
10 |
from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
|
11 |
+
|
12 |
|
13 |
DESCRIPTIONx = """## STABLE HAMSTER
|
14 |
"""
|
15 |
|
16 |
+
|
17 |
# Use environment variables for flexibility
|
18 |
MODEL_ID = os.getenv("MODEL_REPO")
|
19 |
+
MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "4096"))
|
20 |
USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE", "0") == "1"
|
21 |
ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"
|
22 |
BATCH_SIZE = int(os.getenv("BATCH_SIZE", "1")) # Allow generating multiple images at once
|
|
|
41 |
|
42 |
MAX_SEED = np.iinfo(np.int32).max
|
43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
def save_image(img):
|
45 |
unique_name = str(uuid.uuid4()) + ".png"
|
46 |
img.save(unique_name)
|
|
|
64 |
randomize_seed: bool = False,
|
65 |
use_resolution_binning: bool = True,
|
66 |
num_images: int = 1, # Number of images to generate
|
|
|
67 |
progress=gr.Progress(track_tqdm=True),
|
68 |
):
|
|
|
69 |
seed = int(randomize_seed_fn(seed, randomize_seed))
|
70 |
generator = torch.Generator(device=device).manual_seed(seed)
|
71 |
|
72 |
# Improved options handling
|
73 |
options = {
|
74 |
+
"prompt": [prompt] * num_images,
|
75 |
+
"negative_prompt": [negative_prompt] * num_images if use_negative_prompt else None,
|
76 |
+
"width": width,
|
77 |
+
"height": height,
|
78 |
+
"guidance_scale": guidance_scale,
|
79 |
+
"num_inference_steps": num_inference_steps,
|
80 |
"generator": generator,
|
81 |
"output_type": "pil",
|
82 |
}
|
|
|
87 |
|
88 |
# Generate images potentially in batches
|
89 |
images = []
|
90 |
+
for i in range(0, num_images, BATCH_SIZE):
|
91 |
batch_options = options.copy()
|
92 |
batch_options["prompt"] = options["prompt"][i:i+BATCH_SIZE]
|
93 |
if "negative_prompt" in batch_options:
|
|
|
183 |
step=1,
|
184 |
value=8,
|
185 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
186 |
|
187 |
gr.Examples(
|
188 |
examples=examples,
|
|
|
197 |
api_name=False,
|
198 |
)
|
199 |
|
|
|
200 |
gr.on(
|
201 |
triggers=[
|
202 |
prompt.submit,
|
|
|
214 |
guidance_scale,
|
215 |
num_inference_steps,
|
216 |
randomize_seed,
|
217 |
+
num_images
|
|
|
218 |
],
|
219 |
outputs=[result, seed],
|
220 |
api_name="run",
|