Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -12,7 +12,7 @@ from typing import Tuple
|
|
12 |
|
13 |
bad_words = json.loads(os.getenv('BAD_WORDS', "[]"))
|
14 |
bad_words_negative = json.loads(os.getenv('BAD_WORDS_NEGATIVE', "[]"))
|
15 |
-
default_negative = os.getenv("default_negative","")
|
16 |
|
17 |
def check_text(prompt, negative=""):
|
18 |
for i in bad_words:
|
@@ -23,8 +23,7 @@ def check_text(prompt, negative=""):
|
|
23 |
return True
|
24 |
return False
|
25 |
|
26 |
-
#Quality/Style
|
27 |
-
|
28 |
style_list = [
|
29 |
{
|
30 |
"name": "3840 x 2160",
|
@@ -36,23 +35,19 @@ style_list = [
|
|
36 |
"prompt": "hyper-realistic 4K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
|
37 |
"negative_prompt": "cartoonish, low resolution, blurry, simplistic, abstract, deformed, ugly",
|
38 |
},
|
39 |
-
|
40 |
{
|
41 |
"name": "HD+",
|
42 |
"prompt": "hyper-realistic 2K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
|
43 |
"negative_prompt": "cartoonish, low resolution, blurry, simplistic, abstract, deformed, ugly",
|
44 |
},
|
45 |
-
|
46 |
{
|
47 |
"name": "Style Zero",
|
48 |
"prompt": "{prompt}",
|
49 |
"negative_prompt": "",
|
50 |
},
|
51 |
-
|
52 |
]
|
53 |
|
54 |
-
#
|
55 |
-
|
56 |
collage_style_list = [
|
57 |
{
|
58 |
"name": "Hi-Res",
|
@@ -109,9 +104,6 @@ collage_style_list = [
|
|
109 |
"prompt": "thematic collage of {prompt}. cohesive theme, well-organized, matching colors, creative layout",
|
110 |
"negative_prompt": "random, messy, unorganized, clashing colors",
|
111 |
},
|
112 |
-
|
113 |
-
#DuoTones by Canva --------------------------------------------------------------------------------------------------------------- Alters only the i++ Part / not Zero Tones
|
114 |
-
|
115 |
{
|
116 |
"name": "Cherry",
|
117 |
"prompt": "Duotone style Cherry tone applied to {prompt}",
|
@@ -182,11 +174,9 @@ collage_style_list = [
|
|
182 |
"prompt": "{prompt}",
|
183 |
"negative_prompt": "",
|
184 |
},
|
185 |
-
|
186 |
]
|
187 |
|
188 |
-
#
|
189 |
-
|
190 |
filters = {
|
191 |
"Vivid": {
|
192 |
"prompt": "extra vivid {prompt}",
|
@@ -212,13 +202,10 @@ filters = {
|
|
212 |
"prompt": "{prompt} with a minimalist design",
|
213 |
"negative_prompt": "cluttered, ornate"
|
214 |
},
|
215 |
-
|
216 |
"Zero filter": {
|
217 |
"prompt": "{prompt}",
|
218 |
"negative_prompt": ""
|
219 |
},
|
220 |
-
|
221 |
-
|
222 |
}
|
223 |
|
224 |
styles = {k["name"]: (k["prompt"], k["negative_prompt"]) for k in style_list}
|
@@ -256,28 +243,42 @@ USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE", "0") == "1"
|
|
256 |
ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"
|
257 |
|
258 |
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
259 |
-
#
|
|
|
|
|
|
|
260 |
if torch.cuda.is_available():
|
261 |
pipe = StableDiffusionXLPipeline.from_pretrained(
|
262 |
-
"SG161222/RealVisXL_V5.0_Lightning",
|
263 |
-
torch_dtype=
|
264 |
use_safetensors=True,
|
265 |
add_watermarker=False
|
266 |
).to(device)
|
267 |
-
|
|
|
|
|
268 |
if ENABLE_CPU_OFFLOAD:
|
269 |
pipe.enable_model_cpu_offload()
|
270 |
else:
|
271 |
pipe.to(device)
|
272 |
print("Loaded on Device!")
|
273 |
-
|
274 |
if USE_TORCH_COMPILE:
|
275 |
pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
|
276 |
print("Model Compiled!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
277 |
|
278 |
def save_image(img, path):
|
279 |
img.save(path)
|
280 |
-
|
281 |
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
282 |
if randomize_seed:
|
283 |
seed = random.randint(0, MAX_SEED)
|
@@ -311,10 +312,10 @@ def generate(
|
|
311 |
prompt, negative_prompt = apply_style(style, prompt, negative_prompt)
|
312 |
|
313 |
seed = int(randomize_seed_fn(seed, randomize_seed))
|
314 |
-
generator = torch.Generator().manual_seed(seed)
|
315 |
|
316 |
if not use_negative_prompt:
|
317 |
-
negative_prompt = ""
|
318 |
negative_prompt += default_negative
|
319 |
|
320 |
grid_sizes = {
|
@@ -342,7 +343,7 @@ def generate(
|
|
342 |
"output_type": "pil",
|
343 |
}
|
344 |
|
345 |
-
torch.cuda.empty_cache() # Clear GPU memory
|
346 |
images = pipe(**options).images
|
347 |
|
348 |
grid_img = Image.new('RGB', (width * grid_size_x, height * grid_size_y))
|
@@ -355,18 +356,15 @@ def generate(
|
|
355 |
return [unique_name], seed
|
356 |
|
357 |
examples = [
|
358 |
-
|
359 |
-
"Chocolate dripping from a donut against a yellow background, in the style of brocore, hyper-realistic oil --ar 2:3 --q 2 --s 750 --v 5 --ar 2:3 --q 2 --s 750 --v 5",
|
360 |
"3d image, cute girl, in the style of Pixar --ar 1:2 --stylize 750, 4K resolution highlights, Sharp focus, octane render, ray tracing, Ultra-High-Definition, 8k, UHD, HDR, (Masterpiece:1.5), (best quality:1.5)",
|
361 |
"Cold coffee in a cup bokeh --ar 85:128 --v 6.0 --style raw5,4k",
|
362 |
"Food photography of a milk shake with flying strawberrys against a pink background, professionally studio shot with cinematic lighting. The image is in the style of a professional studio shot --ar 85:128 --v 6.0 --style raw"
|
363 |
-
|
364 |
]
|
365 |
|
366 |
css = '''
|
367 |
.gradio-container{max-width: 888px !important}
|
368 |
h1{text-align:center}
|
369 |
-
|
370 |
.submit-btn {
|
371 |
background: linear-gradient(90deg, #4B79A1 0%, #283E51 100%) !important;
|
372 |
border: none !important;
|
|
|
12 |
|
13 |
bad_words = json.loads(os.getenv('BAD_WORDS', "[]"))
|
14 |
bad_words_negative = json.loads(os.getenv('BAD_WORDS_NEGATIVE', "[]"))
|
15 |
+
default_negative = os.getenv("default_negative", "")
|
16 |
|
17 |
def check_text(prompt, negative=""):
|
18 |
for i in bad_words:
|
|
|
23 |
return True
|
24 |
return False
|
25 |
|
26 |
+
# Quality/Style
|
|
|
27 |
style_list = [
|
28 |
{
|
29 |
"name": "3840 x 2160",
|
|
|
35 |
"prompt": "hyper-realistic 4K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
|
36 |
"negative_prompt": "cartoonish, low resolution, blurry, simplistic, abstract, deformed, ugly",
|
37 |
},
|
|
|
38 |
{
|
39 |
"name": "HD+",
|
40 |
"prompt": "hyper-realistic 2K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
|
41 |
"negative_prompt": "cartoonish, low resolution, blurry, simplistic, abstract, deformed, ugly",
|
42 |
},
|
|
|
43 |
{
|
44 |
"name": "Style Zero",
|
45 |
"prompt": "{prompt}",
|
46 |
"negative_prompt": "",
|
47 |
},
|
|
|
48 |
]
|
49 |
|
50 |
+
# Collage styles
|
|
|
51 |
collage_style_list = [
|
52 |
{
|
53 |
"name": "Hi-Res",
|
|
|
104 |
"prompt": "thematic collage of {prompt}. cohesive theme, well-organized, matching colors, creative layout",
|
105 |
"negative_prompt": "random, messy, unorganized, clashing colors",
|
106 |
},
|
|
|
|
|
|
|
107 |
{
|
108 |
"name": "Cherry",
|
109 |
"prompt": "Duotone style Cherry tone applied to {prompt}",
|
|
|
174 |
"prompt": "{prompt}",
|
175 |
"negative_prompt": "",
|
176 |
},
|
|
|
177 |
]
|
178 |
|
179 |
+
# Filters
|
|
|
180 |
filters = {
|
181 |
"Vivid": {
|
182 |
"prompt": "extra vivid {prompt}",
|
|
|
202 |
"prompt": "{prompt} with a minimalist design",
|
203 |
"negative_prompt": "cluttered, ornate"
|
204 |
},
|
|
|
205 |
"Zero filter": {
|
206 |
"prompt": "{prompt}",
|
207 |
"negative_prompt": ""
|
208 |
},
|
|
|
|
|
209 |
}
|
210 |
|
211 |
styles = {k["name"]: (k["prompt"], k["negative_prompt"]) for k in style_list}
|
|
|
243 |
ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"
|
244 |
|
245 |
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
246 |
+
# Set dtype based on device: half precision for CUDA, full precision for CPU
|
247 |
+
dtype = torch.float16 if device.type == "cuda" else torch.float32
|
248 |
+
|
249 |
+
# Load the pipeline
|
250 |
if torch.cuda.is_available():
|
251 |
pipe = StableDiffusionXLPipeline.from_pretrained(
|
252 |
+
"SG161222/RealVisXL_V5.0_Lightning",
|
253 |
+
torch_dtype=dtype,
|
254 |
use_safetensors=True,
|
255 |
add_watermarker=False
|
256 |
).to(device)
|
257 |
+
# Ensure the text encoder is in half precision to match the rest of the model
|
258 |
+
pipe.text_encoder = pipe.text_encoder.half()
|
259 |
+
|
260 |
if ENABLE_CPU_OFFLOAD:
|
261 |
pipe.enable_model_cpu_offload()
|
262 |
else:
|
263 |
pipe.to(device)
|
264 |
print("Loaded on Device!")
|
265 |
+
|
266 |
if USE_TORCH_COMPILE:
|
267 |
pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
|
268 |
print("Model Compiled!")
|
269 |
+
else:
|
270 |
+
# On CPU, load with float32
|
271 |
+
pipe = StableDiffusionXLPipeline.from_pretrained(
|
272 |
+
"SG161222/RealVisXL_V5.0_Lightning",
|
273 |
+
torch_dtype=dtype,
|
274 |
+
use_safetensors=True,
|
275 |
+
add_watermarker=False
|
276 |
+
).to(device)
|
277 |
+
print("Running on CPU; model loaded in float32.")
|
278 |
|
279 |
def save_image(img, path):
|
280 |
img.save(path)
|
281 |
+
|
282 |
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
283 |
if randomize_seed:
|
284 |
seed = random.randint(0, MAX_SEED)
|
|
|
312 |
prompt, negative_prompt = apply_style(style, prompt, negative_prompt)
|
313 |
|
314 |
seed = int(randomize_seed_fn(seed, randomize_seed))
|
315 |
+
generator = torch.Generator(device=device).manual_seed(seed)
|
316 |
|
317 |
if not use_negative_prompt:
|
318 |
+
negative_prompt = ""
|
319 |
negative_prompt += default_negative
|
320 |
|
321 |
grid_sizes = {
|
|
|
343 |
"output_type": "pil",
|
344 |
}
|
345 |
|
346 |
+
torch.cuda.empty_cache() # Clear GPU memory if available
|
347 |
images = pipe(**options).images
|
348 |
|
349 |
grid_img = Image.new('RGB', (width * grid_size_x, height * grid_size_y))
|
|
|
356 |
return [unique_name], seed
|
357 |
|
358 |
examples = [
|
359 |
+
"Chocolate dripping from a donut against a yellow background, in the style of brocore, hyper-realistic oil --ar 2:3 --q 2 --s 750 --v 5",
|
|
|
360 |
"3d image, cute girl, in the style of Pixar --ar 1:2 --stylize 750, 4K resolution highlights, Sharp focus, octane render, ray tracing, Ultra-High-Definition, 8k, UHD, HDR, (Masterpiece:1.5), (best quality:1.5)",
|
361 |
"Cold coffee in a cup bokeh --ar 85:128 --v 6.0 --style raw5,4k",
|
362 |
"Food photography of a milk shake with flying strawberrys against a pink background, professionally studio shot with cinematic lighting. The image is in the style of a professional studio shot --ar 85:128 --v 6.0 --style raw"
|
|
|
363 |
]
|
364 |
|
365 |
css = '''
|
366 |
.gradio-container{max-width: 888px !important}
|
367 |
h1{text-align:center}
|
|
|
368 |
.submit-btn {
|
369 |
background: linear-gradient(90deg, #4B79A1 0%, #283E51 100%) !important;
|
370 |
border: none !important;
|