Update app.py
Browse files
app.py
CHANGED
|
@@ -9,6 +9,16 @@ import numpy as np
|
|
| 9 |
import random
|
| 10 |
|
| 11 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
from diffusers import StableDiffusion3Pipeline, SD3Transformer2DModel, AutoencoderKL
|
| 13 |
from transformers import CLIPTextModelWithProjection, T5EncoderModel
|
| 14 |
from transformers import CLIPTokenizer, T5TokenizerFast
|
|
@@ -21,22 +31,8 @@ from image_gen_aux import UpscaleWithModel
|
|
| 21 |
from huggingface_hub import hf_hub_download
|
| 22 |
import datetime
|
| 23 |
import cyper
|
| 24 |
-
|
| 25 |
-
#from models.transformer_sd3 import SD3Transformer2DModel
|
| 26 |
-
#from pipeline_stable_diffusion_3_ipa import StableDiffusion3Pipeline
|
| 27 |
-
|
| 28 |
from PIL import Image
|
| 29 |
|
| 30 |
-
torch.backends.cuda.matmul.allow_tf32 = False
|
| 31 |
-
torch.backends.cuda.matmul.allow_bf16_reduced_precision_reduction = False
|
| 32 |
-
torch.backends.cuda.matmul.allow_fp16_reduced_precision_reduction = False
|
| 33 |
-
torch.backends.cudnn.allow_tf32 = False
|
| 34 |
-
torch.backends.cudnn.deterministic = False
|
| 35 |
-
torch.backends.cudnn.benchmark = False
|
| 36 |
-
#torch.backends.cuda.preferred_blas_library="cublas"
|
| 37 |
-
#torch.backends.cuda.preferred_linalg_library="cusolver"
|
| 38 |
-
torch.set_float32_matmul_precision("highest")
|
| 39 |
-
|
| 40 |
hftoken = os.getenv("HF_AUTH_TOKEN")
|
| 41 |
|
| 42 |
code = r'''
|
|
@@ -65,7 +61,8 @@ def upload_to_ftp(filename):
|
|
| 65 |
pyx = cyper.inline(code, fast_indexing=True, directives=dict(boundscheck=False, wraparound=False, language_level=3))
|
| 66 |
|
| 67 |
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
| 68 |
-
|
|
|
|
| 69 |
vaeX=AutoencoderKL.from_pretrained("ford442/stable-diffusion-3.5-large-fp32", safety_checker=None, use_safetensors=True, subfolder='vae', low_cpu_mem_usage=False, torch_dtype=torch.float32, token=True)
|
| 70 |
pipe = StableDiffusion3Pipeline.from_pretrained(
|
| 71 |
#"stabilityai # stable-diffusion-3.5-large",
|
|
@@ -92,67 +89,14 @@ text_encoder_3=T5EncoderModel.from_pretrained("ford442/stable-diffusion-3.5-larg
|
|
| 92 |
ll_transformer=SD3Transformer2DModel.from_pretrained("ford442/stable-diffusion-3.5-large-bf16", subfolder='transformer',token=True).to(torch.device("cuda:0"), dtype=torch.bfloat16)
|
| 93 |
pipe.transformer=ll_transformer
|
| 94 |
pipe.load_lora_weights("ford442/sdxl-vae-bf16", weight_name="LoRA/UltraReal.safetensors")
|
| 95 |
-
|
| 96 |
pipe.to(device=device, dtype=torch.bfloat16)
|
| 97 |
-
|
| 98 |
-
#pipe.vae=vaeX.to('cpu')
|
| 99 |
upscaler_2 = UpscaleWithModel.from_pretrained("Kim2091/ClearRealityV1").to(torch.device('cpu'))
|
| 100 |
|
| 101 |
MAX_SEED = np.iinfo(np.int32).max
|
| 102 |
|
| 103 |
MAX_IMAGE_SIZE = 4096
|
| 104 |
|
| 105 |
-
@spaces.GPU(duration=40)
|
| 106 |
-
def infer_30(
|
| 107 |
-
prompt,
|
| 108 |
-
negative_prompt_1,
|
| 109 |
-
negative_prompt_2,
|
| 110 |
-
negative_prompt_3,
|
| 111 |
-
width,
|
| 112 |
-
height,
|
| 113 |
-
guidance_scale,
|
| 114 |
-
num_inference_steps,
|
| 115 |
-
progress=gr.Progress(track_tqdm=True),
|
| 116 |
-
):
|
| 117 |
-
pipe.vae=vaeX.to('cpu')
|
| 118 |
-
pipe.transformer=ll_transformer
|
| 119 |
-
pipe.text_encoder=text_encoder #CLIPTextModelWithProjection.from_pretrained("ford442/stable-diffusion-3.5-large-bf16", subfolder='text_encoder', token=True).to(device=device, dtype=torch.bfloat16)
|
| 120 |
-
pipe.text_encoder_2=text_encoder_2 #CLIPTextModelWithProjection.from_pretrained("ford442/stable-diffusion-3.5-large-bf16", subfolder='text_encoder_2',token=True).to(device=device, dtype=torch.bfloat16)
|
| 121 |
-
pipe.text_encoder_3=text_encoder_3 #T5EncoderModel.from_pretrained("ford442/stable-diffusion-3.5-large-bf16", subfolder='text_encoder_3',token=True).to(device=device, dtype=torch.bfloat16)
|
| 122 |
-
seed = random.randint(0, MAX_SEED)
|
| 123 |
-
generator = torch.Generator(device='cuda').manual_seed(seed)
|
| 124 |
-
print('-- generating image --')
|
| 125 |
-
sd_image = pipe(
|
| 126 |
-
prompt=prompt,
|
| 127 |
-
prompt_2=prompt,
|
| 128 |
-
prompt_3=prompt,
|
| 129 |
-
negative_prompt=negative_prompt_1,
|
| 130 |
-
negative_prompt_2=negative_prompt_2,
|
| 131 |
-
negative_prompt_3=negative_prompt_3,
|
| 132 |
-
guidance_scale=guidance_scale,
|
| 133 |
-
num_inference_steps=num_inference_steps,
|
| 134 |
-
width=width,
|
| 135 |
-
height=height,
|
| 136 |
-
# cross_attention_kwargs={"scale": 0.75},
|
| 137 |
-
generator=generator,
|
| 138 |
-
max_sequence_length=512
|
| 139 |
-
).images[0]
|
| 140 |
-
print('-- got image --')
|
| 141 |
-
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 142 |
-
sd35_path = f"sd35ll_{timestamp}.png"
|
| 143 |
-
sd_image.save(sd35_path,optimize=False,compress_level=0)
|
| 144 |
-
pyx.upload_to_ftp(sd35_path)
|
| 145 |
-
# pipe.unet.to('cpu')
|
| 146 |
-
upscaler_2.to(torch.device('cuda'))
|
| 147 |
-
with torch.no_grad():
|
| 148 |
-
upscale2 = upscaler_2(sd_image, tiling=True, tile_width=256, tile_height=256)
|
| 149 |
-
print('-- got upscaled image --')
|
| 150 |
-
downscale2 = upscale2.resize((upscale2.width // 4, upscale2.height // 4),Image.LANCZOS)
|
| 151 |
-
upscale_path = f"sd35ll_upscale_{timestamp}.png"
|
| 152 |
-
downscale2.save(upscale_path,optimize=False,compress_level=0)
|
| 153 |
-
pyx.upload_to_ftp(upscale_path)
|
| 154 |
-
return sd_image, prompt
|
| 155 |
-
|
| 156 |
@spaces.GPU(duration=70)
|
| 157 |
def infer_60(
|
| 158 |
prompt,
|
|
@@ -184,7 +128,6 @@ def infer_60(
|
|
| 184 |
num_inference_steps=num_inference_steps,
|
| 185 |
width=width,
|
| 186 |
height=height,
|
| 187 |
-
# cross_attention_kwargs={"scale": 0.75},
|
| 188 |
generator=generator,
|
| 189 |
max_sequence_length=512
|
| 190 |
).images[0]
|
|
@@ -193,7 +136,6 @@ def infer_60(
|
|
| 193 |
sd35_path = f"sd35ll_{timestamp}.png"
|
| 194 |
sd_image.save(sd35_path,optimize=False,compress_level=0)
|
| 195 |
pyx.upload_to_ftp(sd35_path)
|
| 196 |
-
# pipe.unet.to('cpu')
|
| 197 |
upscaler_2.to(torch.device('cuda'))
|
| 198 |
with torch.no_grad():
|
| 199 |
upscale2 = upscaler_2(sd_image, tiling=True, tile_width=256, tile_height=256)
|
|
@@ -235,7 +177,6 @@ def infer_90(
|
|
| 235 |
num_inference_steps=num_inference_steps,
|
| 236 |
width=width,
|
| 237 |
height=height,
|
| 238 |
-
# cross_attention_kwargs={"scale": 0.75},
|
| 239 |
generator=generator,
|
| 240 |
max_sequence_length=512
|
| 241 |
).images[0]
|
|
@@ -244,7 +185,6 @@ def infer_90(
|
|
| 244 |
sd35_path = f"sd35ll_{timestamp}.png"
|
| 245 |
sd_image.save(sd35_path,optimize=False,compress_level=0)
|
| 246 |
pyx.upload_to_ftp(sd35_path)
|
| 247 |
-
# pipe.unet.to('cpu')
|
| 248 |
upscaler_2.to(torch.device('cuda'))
|
| 249 |
with torch.no_grad():
|
| 250 |
upscale2 = upscaler_2(sd_image, tiling=True, tile_width=256, tile_height=256)
|
|
@@ -255,8 +195,8 @@ def infer_90(
|
|
| 255 |
pyx.upload_to_ftp(upscale_path)
|
| 256 |
return sd_image, prompt
|
| 257 |
|
| 258 |
-
@spaces.GPU(duration=
|
| 259 |
-
def
|
| 260 |
prompt,
|
| 261 |
negative_prompt_1,
|
| 262 |
negative_prompt_2,
|
|
@@ -294,7 +234,6 @@ def infer_100(
|
|
| 294 |
sd35_path = f"sd35ll_{timestamp}.png"
|
| 295 |
sd_image.save(sd35_path,optimize=False,compress_level=0)
|
| 296 |
pyx.upload_to_ftp(sd35_path)
|
| 297 |
-
# pipe.unet.to('cpu')
|
| 298 |
upscaler_2.to(torch.device('cuda'))
|
| 299 |
with torch.no_grad():
|
| 300 |
upscale2 = upscaler_2(sd_image, tiling=True, tile_width=256, tile_height=256)
|
|
@@ -322,10 +261,9 @@ with gr.Blocks(theme=gr.themes.Origin(),css=css) as demo:
|
|
| 322 |
placeholder="Enter your prompt",
|
| 323 |
container=False,
|
| 324 |
)
|
| 325 |
-
run_button_30 = gr.Button("Run 30", scale=0, variant="primary")
|
| 326 |
run_button_60 = gr.Button("Run 60", scale=0, variant="primary")
|
| 327 |
run_button_90 = gr.Button("Run 90", scale=0, variant="primary")
|
| 328 |
-
|
| 329 |
result = gr.Image(label="Result", show_label=False)
|
| 330 |
with gr.Accordion("Advanced Settings", open=True):
|
| 331 |
negative_prompt_1 = gr.Text(
|
|
@@ -382,21 +320,6 @@ with gr.Blocks(theme=gr.themes.Origin(),css=css) as demo:
|
|
| 382 |
value=50,
|
| 383 |
)
|
| 384 |
gr.on(
|
| 385 |
-
triggers=[run_button_30.click, prompt.submit],
|
| 386 |
-
fn=infer_30,
|
| 387 |
-
inputs=[
|
| 388 |
-
prompt,
|
| 389 |
-
negative_prompt_1,
|
| 390 |
-
negative_prompt_2,
|
| 391 |
-
negative_prompt_3,
|
| 392 |
-
width,
|
| 393 |
-
height,
|
| 394 |
-
guidance_scale,
|
| 395 |
-
num_inference_steps,
|
| 396 |
-
],
|
| 397 |
-
outputs=[result, expanded_prompt_output],
|
| 398 |
-
)
|
| 399 |
-
gr.on(
|
| 400 |
triggers=[run_button_60.click, prompt.submit],
|
| 401 |
fn=infer_60,
|
| 402 |
inputs=[
|
|
@@ -427,8 +350,8 @@ with gr.Blocks(theme=gr.themes.Origin(),css=css) as demo:
|
|
| 427 |
outputs=[result, expanded_prompt_output],
|
| 428 |
)
|
| 429 |
gr.on(
|
| 430 |
-
triggers=[
|
| 431 |
-
fn=
|
| 432 |
inputs=[
|
| 433 |
prompt,
|
| 434 |
negative_prompt_1,
|
|
|
|
| 9 |
import random
|
| 10 |
|
| 11 |
import torch
|
| 12 |
+
torch.backends.cuda.matmul.allow_tf32 = False
|
| 13 |
+
torch.backends.cuda.matmul.allow_bf16_reduced_precision_reduction = False
|
| 14 |
+
torch.backends.cuda.matmul.allow_fp16_reduced_precision_reduction = False
|
| 15 |
+
torch.backends.cudnn.allow_tf32 = False
|
| 16 |
+
torch.backends.cudnn.deterministic = False
|
| 17 |
+
torch.backends.cudnn.benchmark = False
|
| 18 |
+
#torch.backends.cuda.preferred_blas_library="cublas"
|
| 19 |
+
#torch.backends.cuda.preferred_linalg_library="cusolver"
|
| 20 |
+
torch.set_float32_matmul_precision("highest")
|
| 21 |
+
|
| 22 |
from diffusers import StableDiffusion3Pipeline, SD3Transformer2DModel, AutoencoderKL
|
| 23 |
from transformers import CLIPTextModelWithProjection, T5EncoderModel
|
| 24 |
from transformers import CLIPTokenizer, T5TokenizerFast
|
|
|
|
| 31 |
from huggingface_hub import hf_hub_download
|
| 32 |
import datetime
|
| 33 |
import cyper
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
from PIL import Image
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
hftoken = os.getenv("HF_AUTH_TOKEN")
|
| 37 |
|
| 38 |
code = r'''
|
|
|
|
| 61 |
pyx = cyper.inline(code, fast_indexing=True, directives=dict(boundscheck=False, wraparound=False, language_level=3))
|
| 62 |
|
| 63 |
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
| 64 |
+
|
| 65 |
+
#vae=AutoencoderKL.from_pretrained("ford442/sdxl-vae-bf16", use_safetensors=True, subfolder='vae',token=True)
|
| 66 |
vaeX=AutoencoderKL.from_pretrained("ford442/stable-diffusion-3.5-large-fp32", safety_checker=None, use_safetensors=True, subfolder='vae', low_cpu_mem_usage=False, torch_dtype=torch.float32, token=True)
|
| 67 |
pipe = StableDiffusion3Pipeline.from_pretrained(
|
| 68 |
#"stabilityai # stable-diffusion-3.5-large",
|
|
|
|
| 89 |
ll_transformer=SD3Transformer2DModel.from_pretrained("ford442/stable-diffusion-3.5-large-bf16", subfolder='transformer',token=True).to(torch.device("cuda:0"), dtype=torch.bfloat16)
|
| 90 |
pipe.transformer=ll_transformer
|
| 91 |
pipe.load_lora_weights("ford442/sdxl-vae-bf16", weight_name="LoRA/UltraReal.safetensors")
|
|
|
|
| 92 |
pipe.to(device=device, dtype=torch.bfloat16)
|
| 93 |
+
|
|
|
|
| 94 |
upscaler_2 = UpscaleWithModel.from_pretrained("Kim2091/ClearRealityV1").to(torch.device('cpu'))
|
| 95 |
|
| 96 |
MAX_SEED = np.iinfo(np.int32).max
|
| 97 |
|
| 98 |
MAX_IMAGE_SIZE = 4096
|
| 99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
@spaces.GPU(duration=70)
|
| 101 |
def infer_60(
|
| 102 |
prompt,
|
|
|
|
| 128 |
num_inference_steps=num_inference_steps,
|
| 129 |
width=width,
|
| 130 |
height=height,
|
|
|
|
| 131 |
generator=generator,
|
| 132 |
max_sequence_length=512
|
| 133 |
).images[0]
|
|
|
|
| 136 |
sd35_path = f"sd35ll_{timestamp}.png"
|
| 137 |
sd_image.save(sd35_path,optimize=False,compress_level=0)
|
| 138 |
pyx.upload_to_ftp(sd35_path)
|
|
|
|
| 139 |
upscaler_2.to(torch.device('cuda'))
|
| 140 |
with torch.no_grad():
|
| 141 |
upscale2 = upscaler_2(sd_image, tiling=True, tile_width=256, tile_height=256)
|
|
|
|
| 177 |
num_inference_steps=num_inference_steps,
|
| 178 |
width=width,
|
| 179 |
height=height,
|
|
|
|
| 180 |
generator=generator,
|
| 181 |
max_sequence_length=512
|
| 182 |
).images[0]
|
|
|
|
| 185 |
sd35_path = f"sd35ll_{timestamp}.png"
|
| 186 |
sd_image.save(sd35_path,optimize=False,compress_level=0)
|
| 187 |
pyx.upload_to_ftp(sd35_path)
|
|
|
|
| 188 |
upscaler_2.to(torch.device('cuda'))
|
| 189 |
with torch.no_grad():
|
| 190 |
upscale2 = upscaler_2(sd_image, tiling=True, tile_width=256, tile_height=256)
|
|
|
|
| 195 |
pyx.upload_to_ftp(upscale_path)
|
| 196 |
return sd_image, prompt
|
| 197 |
|
| 198 |
+
@spaces.GPU(duration=120)
|
| 199 |
+
def infer_110(
|
| 200 |
prompt,
|
| 201 |
negative_prompt_1,
|
| 202 |
negative_prompt_2,
|
|
|
|
| 234 |
sd35_path = f"sd35ll_{timestamp}.png"
|
| 235 |
sd_image.save(sd35_path,optimize=False,compress_level=0)
|
| 236 |
pyx.upload_to_ftp(sd35_path)
|
|
|
|
| 237 |
upscaler_2.to(torch.device('cuda'))
|
| 238 |
with torch.no_grad():
|
| 239 |
upscale2 = upscaler_2(sd_image, tiling=True, tile_width=256, tile_height=256)
|
|
|
|
| 261 |
placeholder="Enter your prompt",
|
| 262 |
container=False,
|
| 263 |
)
|
|
|
|
| 264 |
run_button_60 = gr.Button("Run 60", scale=0, variant="primary")
|
| 265 |
run_button_90 = gr.Button("Run 90", scale=0, variant="primary")
|
| 266 |
+
run_button_110 = gr.Button("Run 110", scale=0, variant="primary")
|
| 267 |
result = gr.Image(label="Result", show_label=False)
|
| 268 |
with gr.Accordion("Advanced Settings", open=True):
|
| 269 |
negative_prompt_1 = gr.Text(
|
|
|
|
| 320 |
value=50,
|
| 321 |
)
|
| 322 |
gr.on(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 323 |
triggers=[run_button_60.click, prompt.submit],
|
| 324 |
fn=infer_60,
|
| 325 |
inputs=[
|
|
|
|
| 350 |
outputs=[result, expanded_prompt_output],
|
| 351 |
)
|
| 352 |
gr.on(
|
| 353 |
+
triggers=[run_button_110.click, prompt.submit],
|
| 354 |
+
fn=infer_110,
|
| 355 |
inputs=[
|
| 356 |
prompt,
|
| 357 |
negative_prompt_1,
|