Spaces:
Running
Running
prithivMLmods
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -30,11 +30,9 @@ if not torch.cuda.is_available():
|
|
30 |
DESCRIPTION += "\n<p>Running on CPU, This may not work on CPU.</p>"
|
31 |
|
32 |
USE_TORCH_COMPILE = 0
|
33 |
-
ENABLE_CPU_OFFLOAD =
|
34 |
|
35 |
style_list = [
|
36 |
-
|
37 |
-
|
38 |
{
|
39 |
"name": "3840 x 2160",
|
40 |
"prompt": "hyper-realistic 8K image of {prompt} . ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
|
@@ -80,13 +78,11 @@ style_list = [
|
|
80 |
"prompt": "professional 3d model {prompt} . octane render, highly detailed, volumetric, dramatic lighting",
|
81 |
"negative_prompt": "ugly, deformed, noisy, low poly, blurry, painting",
|
82 |
},
|
83 |
-
|
84 |
{
|
85 |
"name": "(No style)",
|
86 |
"prompt": "{prompt}",
|
87 |
"negative_prompt": "",
|
88 |
},
|
89 |
-
|
90 |
]
|
91 |
|
92 |
styles = {k["name"]: (k["prompt"], k["negative_prompt"]) for k in style_list}
|
@@ -95,10 +91,21 @@ DEFAULT_STYLE_NAME = "3840 x 2160"
|
|
95 |
|
96 |
def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str, str]:
|
97 |
p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
|
98 |
-
if not negative:
|
99 |
-
negative = ""
|
100 |
return p.replace("{prompt}", positive), n + negative
|
101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
@spaces.GPU(enable_queue=True)
|
103 |
def stab(
|
104 |
prompt: str,
|
@@ -110,7 +117,7 @@ def stab(
|
|
110 |
seed: int = 0,
|
111 |
width: int = 1024,
|
112 |
height: int = 1024,
|
113 |
-
guidance_scale: float =
|
114 |
randomize_seed: bool = False,
|
115 |
progress=gr.Progress(track_tqdm=True),
|
116 |
):
|
@@ -120,10 +127,6 @@ def stab(
|
|
120 |
negative_prompt = ""
|
121 |
prompt, negative_prompt = apply_style(style, prompt, negative_prompt)
|
122 |
|
123 |
-
prior = StableCascadePriorPipeline.from_pretrained("stabilityai/stable-cascade-prior", variant="bf16", torch_dtype=torch.bfloat16)
|
124 |
-
decoder = StableCascadeDecoderPipeline.from_pretrained("stabilityai/stable-cascade", variant="bf16", torch_dtype=torch.float16)
|
125 |
-
|
126 |
-
prior.enable_model_cpu_offload()
|
127 |
prior_output = prior(
|
128 |
prompt=prompt,
|
129 |
height=height,
|
@@ -134,7 +137,6 @@ def stab(
|
|
134 |
num_inference_steps=num_inference_steps
|
135 |
)
|
136 |
|
137 |
-
decoder.enable_model_cpu_offload()
|
138 |
images = decoder(
|
139 |
image_embeddings=prior_output.image_embeddings.to(torch.float16),
|
140 |
prompt=prompt,
|
@@ -149,10 +151,10 @@ def stab(
|
|
149 |
return image_paths, seed
|
150 |
|
151 |
examples = [
|
152 |
-
"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.
|
153 |
-
"(Pirate ship sailing into a bioluminescence sea with a galaxy in the sky), epic, 4k, ultra,
|
154 |
"Thin burger, realistic photo (without tomato or any other ingredient), smoky flavor, 4K resolution highlights every texture, providing an incredible and appetizing visual experience",
|
155 |
-
"A galaxy with blue water, a red star and many planets in one view, in the style of digital fantasy
|
156 |
"A dark night sky with thick, dense clouds and stars in the background. The main focus is on one of these large cloud formations that has been stylized to resemble an ancient dragon. There's no moon or other celestial bodies visible in the sky. This scene conveys mystery and magic, with the dark blue glow from distant galaxies adding depth and contrast to the night landscape. --ar 8:5 --v 5.2 --style raw"
|
157 |
]
|
158 |
|
@@ -174,7 +176,7 @@ with gr.Blocks(css=css, theme="xiaobaiyuan/theme_brief") as demo:
|
|
174 |
|
175 |
with gr.Group():
|
176 |
with gr.Row():
|
177 |
-
prompt = gr.
|
178 |
label="Prompt",
|
179 |
show_label=False,
|
180 |
max_lines=1,
|
@@ -185,7 +187,7 @@ with gr.Blocks(css=css, theme="xiaobaiyuan/theme_brief") as demo:
|
|
185 |
result = gr.Gallery(label="Result", columns=1, preview=True)
|
186 |
with gr.Accordion("Advanced options", open=False):
|
187 |
use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True, visible=True)
|
188 |
-
negative_prompt = gr.
|
189 |
label="Negative prompt",
|
190 |
max_lines=1,
|
191 |
placeholder="Enter a negative prompt",
|
|
|
30 |
DESCRIPTION += "\n<p>Running on CPU, This may not work on CPU.</p>"
|
31 |
|
32 |
USE_TORCH_COMPILE = 0
|
33 |
+
ENABLE_CPU_OFFLOAD = 1 # Enable CPU offloading to reduce GPU memory usage
|
34 |
|
35 |
style_list = [
|
|
|
|
|
36 |
{
|
37 |
"name": "3840 x 2160",
|
38 |
"prompt": "hyper-realistic 8K image of {prompt} . ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
|
|
|
78 |
"prompt": "professional 3d model {prompt} . octane render, highly detailed, volumetric, dramatic lighting",
|
79 |
"negative_prompt": "ugly, deformed, noisy, low poly, blurry, painting",
|
80 |
},
|
|
|
81 |
{
|
82 |
"name": "(No style)",
|
83 |
"prompt": "{prompt}",
|
84 |
"negative_prompt": "",
|
85 |
},
|
|
|
86 |
]
|
87 |
|
88 |
styles = {k["name"]: (k["prompt"], k["negative_prompt"]) for k in style_list}
|
|
|
91 |
|
92 |
def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str, str]:
|
93 |
p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
|
|
|
|
|
94 |
return p.replace("{prompt}", positive), n + negative
|
95 |
|
96 |
+
# Initialize models once during setup
|
97 |
+
prior = StableCascadePriorPipeline.from_pretrained("stabilityai/stable-cascade-prior", variant="bf16", torch_dtype=torch.bfloat16)
|
98 |
+
decoder = StableCascadeDecoderPipeline.from_pretrained("stabilityai/stable-cascade", variant="bf16", torch_dtype=torch.float16)
|
99 |
+
|
100 |
+
if ENABLE_CPU_OFFLOAD:
|
101 |
+
prior.enable_model_cpu_offload()
|
102 |
+
decoder.enable_model_cpu_offload()
|
103 |
+
else:
|
104 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
105 |
+
prior.to(device)
|
106 |
+
decoder.to(device)
|
107 |
+
print("Loaded on Device!")
|
108 |
+
|
109 |
@spaces.GPU(enable_queue=True)
|
110 |
def stab(
|
111 |
prompt: str,
|
|
|
117 |
seed: int = 0,
|
118 |
width: int = 1024,
|
119 |
height: int = 1024,
|
120 |
+
guidance_scale: float = 6,
|
121 |
randomize_seed: bool = False,
|
122 |
progress=gr.Progress(track_tqdm=True),
|
123 |
):
|
|
|
127 |
negative_prompt = ""
|
128 |
prompt, negative_prompt = apply_style(style, prompt, negative_prompt)
|
129 |
|
|
|
|
|
|
|
|
|
130 |
prior_output = prior(
|
131 |
prompt=prompt,
|
132 |
height=height,
|
|
|
137 |
num_inference_steps=num_inference_steps
|
138 |
)
|
139 |
|
|
|
140 |
images = decoder(
|
141 |
image_embeddings=prior_output.image_embeddings.to(torch.float16),
|
142 |
prompt=prompt,
|
|
|
151 |
return image_paths, seed
|
152 |
|
153 |
examples = [
|
154 |
+
"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)",
|
155 |
+
"(Pirate ship sailing into a bioluminescence sea with a galaxy in the sky), epic, 4k, ultra, the space scene with planets and stars, in the style of ethereal escapism, richly colored skies, vibrant worlds --ar 8:5",
|
156 |
"Thin burger, realistic photo (without tomato or any other ingredient), smoky flavor, 4K resolution highlights every texture, providing an incredible and appetizing visual experience",
|
157 |
+
"A galaxy with blue water, a red star and many planets in one view, in the style of digital fantasy nebulae and cosmos, light black and violet, realistic nebulae paintings, james paick, steve henderson, ue5, cosmic horror --ar 8:5",
|
158 |
"A dark night sky with thick, dense clouds and stars in the background. The main focus is on one of these large cloud formations that has been stylized to resemble an ancient dragon. There's no moon or other celestial bodies visible in the sky. This scene conveys mystery and magic, with the dark blue glow from distant galaxies adding depth and contrast to the night landscape. --ar 8:5 --v 5.2 --style raw"
|
159 |
]
|
160 |
|
|
|
176 |
|
177 |
with gr.Group():
|
178 |
with gr.Row():
|
179 |
+
prompt = gr.Textbox(
|
180 |
label="Prompt",
|
181 |
show_label=False,
|
182 |
max_lines=1,
|
|
|
187 |
result = gr.Gallery(label="Result", columns=1, preview=True)
|
188 |
with gr.Accordion("Advanced options", open=False):
|
189 |
use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True, visible=True)
|
190 |
+
negative_prompt = gr.Textbox(
|
191 |
label="Negative prompt",
|
192 |
max_lines=1,
|
193 |
placeholder="Enter a negative prompt",
|