Update app.py
Browse files
app.py
CHANGED
|
@@ -10,6 +10,7 @@ import urllib
|
|
| 10 |
import time
|
| 11 |
import os
|
| 12 |
import datetime
|
|
|
|
| 13 |
|
| 14 |
from models.transformer_sd3 import SD3Transformer2DModel
|
| 15 |
#from diffusers import StableDiffusion3Pipeline
|
|
@@ -34,7 +35,6 @@ torch.backends.cudnn.deterministic = False
|
|
| 34 |
torch.backends.cudnn.benchmark = False
|
| 35 |
#torch.backends.cuda.preferred_blas_library="cublas"
|
| 36 |
#torch.backends.cuda.preferred_linalg_library="cusolver"
|
| 37 |
-
torch.set_float32_matmul_precision("highest")
|
| 38 |
|
| 39 |
hftoken = os.getenv("HF_TOKEN")
|
| 40 |
|
|
@@ -57,42 +57,37 @@ def upload_to_ftp(filename):
|
|
| 57 |
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
| 58 |
torch_dtype = torch.bfloat16
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
)
|
| 64 |
-
vaeX=AutoencoderKL.from_pretrained("ford442/stable-diffusion-3.5-large-fp32", safety_checker=None, use_safetensors=True, low_cpu_mem_usage=False, subfolder='vae', torch_dtype=torch.float32, token=True)
|
| 65 |
-
pipe = StableDiffusion3Pipeline.from_pretrained(
|
| 66 |
-
#"stabilityai # stable-diffusion-3.5-large",
|
| 67 |
-
"ford442/stable-diffusion-3.5-large-bf16",
|
| 68 |
-
#scheduler = FlowMatchHeunDiscreteScheduler.from_pretrained('ford442/stable-diffusion-3.5-large-bf16', subfolder='scheduler',token=True),
|
| 69 |
-
text_encoder=None, #CLIPTextModelWithProjection.from_pretrained("ford442/stable-diffusion-3.5-large-bf16", subfolder='text_encoder', token=True),
|
| 70 |
-
text_encoder_2=None, #CLIPTextModelWithProjection.from_pretrained("ford442/stable-diffusion-3.5-large-bf16", subfolder='text_encoder_2',token=True),
|
| 71 |
-
text_encoder_3=None, #T5EncoderModel.from_pretrained("ford442/stable-diffusion-3.5-large-bf16", subfolder='text_encoder_3',token=True),
|
| 72 |
-
#tokenizer=CLIPTokenizer.from_pretrained("ford442/stable-diffusion-3.5-large-bf16", add_prefix_space=True, subfolder="tokenizer", token=True),
|
| 73 |
-
#tokenizer_2=CLIPTokenizer.from_pretrained("ford442/stable-diffusion-3.5-large-bf16", add_prefix_space=True, subfolder="tokenizer_2", token=True),
|
| 74 |
-
tokenizer_3=T5TokenizerFast.from_pretrained("ford442/stable-diffusion-3.5-large-bf16", use_fast=True, subfolder="tokenizer_3", token=True),
|
| 75 |
-
#torch_dtype=torch.bfloat16,
|
| 76 |
-
transformer=transformer,
|
| 77 |
-
vae=None
|
| 78 |
-
#use_safetensors=False,
|
| 79 |
-
)
|
| 80 |
-
torch.cuda.empty_cache()
|
| 81 |
-
pipe.to(device=device, dtype=torch.bfloat16)
|
| 82 |
-
pipe.vae=vaeX.to(device)
|
| 83 |
-
upscaler = UpscaleWithModel.from_pretrained("Kim2091/ClearRealityV1").to(torch.device("cuda:0"))
|
| 84 |
-
torch.cuda.empty_cache()
|
| 85 |
-
return pipe, upscaler
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
-
pipe,
|
| 92 |
|
| 93 |
#pipe.to(device)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
-
|
| 96 |
|
| 97 |
MAX_SEED = np.iinfo(np.int32).max
|
| 98 |
MAX_IMAGE_SIZE = 4096
|
|
@@ -131,8 +126,10 @@ def infer(
|
|
| 131 |
nb_token=64,
|
| 132 |
)
|
| 133 |
upscaler_2.to(torch.device('cpu'))
|
|
|
|
| 134 |
torch.cuda.empty_cache()
|
| 135 |
torch.cuda.reset_peak_memory_stats()
|
|
|
|
| 136 |
seed = random.randint(0, MAX_SEED)
|
| 137 |
generator = torch.Generator(device='cuda').manual_seed(seed)
|
| 138 |
enhanced_prompt = prompt
|
|
@@ -190,6 +187,9 @@ def infer(
|
|
| 190 |
sd_image.save(rv_path,optimize=False,compress_level=0)
|
| 191 |
upload_to_ftp(rv_path)
|
| 192 |
upscaler_2.to(torch.device('cuda'))
|
|
|
|
|
|
|
|
|
|
| 193 |
with torch.no_grad():
|
| 194 |
upscale2 = upscaler_2(sd_image, tiling=True, tile_width=256, tile_height=256)
|
| 195 |
print('-- got upscaled image --')
|
|
@@ -219,7 +219,7 @@ body{
|
|
| 219 |
|
| 220 |
with gr.Blocks(theme=gr.themes.Origin(),css=css) as demo:
|
| 221 |
with gr.Column(elem_id="col-container"):
|
| 222 |
-
gr.Markdown(" # StableDiffusion 3.5 Large with IP Adapter
|
| 223 |
expanded_prompt_output = gr.Textbox(label="Prompt", lines=5)
|
| 224 |
with gr.Row():
|
| 225 |
prompt = gr.Text(
|
|
@@ -281,7 +281,7 @@ with gr.Blocks(theme=gr.themes.Origin(),css=css) as demo:
|
|
| 281 |
value=1.0,
|
| 282 |
)
|
| 283 |
image_encoder_path = gr.Dropdown(
|
| 284 |
-
["google/siglip-so400m-patch14-384", "
|
| 285 |
label="CLIP Model",
|
| 286 |
)
|
| 287 |
ip_scale = gr.Slider(
|
|
|
|
| 10 |
import time
|
| 11 |
import os
|
| 12 |
import datetime
|
| 13 |
+
import gc
|
| 14 |
|
| 15 |
from models.transformer_sd3 import SD3Transformer2DModel
|
| 16 |
#from diffusers import StableDiffusion3Pipeline
|
|
|
|
| 35 |
torch.backends.cudnn.benchmark = False
|
| 36 |
#torch.backends.cuda.preferred_blas_library="cublas"
|
| 37 |
#torch.backends.cuda.preferred_linalg_library="cusolver"
|
|
|
|
| 38 |
|
| 39 |
hftoken = os.getenv("HF_TOKEN")
|
| 40 |
|
|
|
|
| 57 |
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
| 58 |
torch_dtype = torch.bfloat16
|
| 59 |
|
| 60 |
+
transformer = SD3Transformer2DModel.from_pretrained(
|
| 61 |
+
model_path, subfolder="transformer" #, torch_dtype=torch.bfloat16
|
| 62 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
+
vaeX=AutoencoderKL.from_pretrained("ford442/stable-diffusion-3.5-large-fp32", safety_checker=None, use_safetensors=True, low_cpu_mem_usage=False, subfolder='vae', torch_dtype=torch.float32, token=True)
|
| 65 |
+
|
| 66 |
+
pipe = StableDiffusion3Pipeline.from_pretrained(
|
| 67 |
+
#"stabilityai # stable-diffusion-3.5-large",
|
| 68 |
+
"ford442/stable-diffusion-3.5-large-bf16",
|
| 69 |
+
#scheduler = FlowMatchHeunDiscreteScheduler.from_pretrained('ford442/stable-diffusion-3.5-large-bf16', subfolder='scheduler',token=True),
|
| 70 |
+
text_encoder=None, #CLIPTextModelWithProjection.from_pretrained("ford442/stable-diffusion-3.5-large-bf16", subfolder='text_encoder', token=True),
|
| 71 |
+
text_encoder_2=None, #CLIPTextModelWithProjection.from_pretrained("ford442/stable-diffusion-3.5-large-bf16", subfolder='text_encoder_2',token=True),
|
| 72 |
+
text_encoder_3=None, #T5EncoderModel.from_pretrained("ford442/stable-diffusion-3.5-large-bf16", subfolder='text_encoder_3',token=True),
|
| 73 |
+
#tokenizer=CLIPTokenizer.from_pretrained("ford442/stable-diffusion-3.5-large-bf16", add_prefix_space=True, subfolder="tokenizer", token=True),
|
| 74 |
+
#tokenizer_2=CLIPTokenizer.from_pretrained("ford442/stable-diffusion-3.5-large-bf16", add_prefix_space=True, subfolder="tokenizer_2", token=True),
|
| 75 |
+
tokenizer_3=T5TokenizerFast.from_pretrained("ford442/stable-diffusion-3.5-large-bf16", use_fast=True, subfolder="tokenizer_3", token=True),
|
| 76 |
+
#torch_dtype=torch.bfloat16,
|
| 77 |
+
transformer=transformer,
|
| 78 |
+
vae=None
|
| 79 |
+
#use_safetensors=False,
|
| 80 |
+
)
|
| 81 |
|
| 82 |
+
pipe.to(device=device, dtype=torch.bfloat16)
|
| 83 |
|
| 84 |
#pipe.to(device)
|
| 85 |
+
pipe.vae=vaeX.to(device)
|
| 86 |
+
text_encoder=CLIPTextModelWithProjection.from_pretrained("ford442/stable-diffusion-3.5-large-bf16", subfolder='text_encoder', token=True).to(device=device, dtype=torch.bfloat16)
|
| 87 |
+
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)
|
| 88 |
+
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)
|
| 89 |
|
| 90 |
+
upscaler_2 = UpscaleWithModel.from_pretrained("Kim2091/ClearRealityV1").to(torch.device("cuda:0"))
|
| 91 |
|
| 92 |
MAX_SEED = np.iinfo(np.int32).max
|
| 93 |
MAX_IMAGE_SIZE = 4096
|
|
|
|
| 126 |
nb_token=64,
|
| 127 |
)
|
| 128 |
upscaler_2.to(torch.device('cpu'))
|
| 129 |
+
gc.collect()
|
| 130 |
torch.cuda.empty_cache()
|
| 131 |
torch.cuda.reset_peak_memory_stats()
|
| 132 |
+
torch.set_float32_matmul_precision("highest")
|
| 133 |
seed = random.randint(0, MAX_SEED)
|
| 134 |
generator = torch.Generator(device='cuda').manual_seed(seed)
|
| 135 |
enhanced_prompt = prompt
|
|
|
|
| 187 |
sd_image.save(rv_path,optimize=False,compress_level=0)
|
| 188 |
upload_to_ftp(rv_path)
|
| 189 |
upscaler_2.to(torch.device('cuda'))
|
| 190 |
+
gc.collect()
|
| 191 |
+
torch.cuda.empty_cache()
|
| 192 |
+
torch.cuda.reset_peak_memory_stats()
|
| 193 |
with torch.no_grad():
|
| 194 |
upscale2 = upscaler_2(sd_image, tiling=True, tile_width=256, tile_height=256)
|
| 195 |
print('-- got upscaled image --')
|
|
|
|
| 219 |
|
| 220 |
with gr.Blocks(theme=gr.themes.Origin(),css=css) as demo:
|
| 221 |
with gr.Column(elem_id="col-container"):
|
| 222 |
+
gr.Markdown(" # StableDiffusion 3.5 Large with IP Adapter")
|
| 223 |
expanded_prompt_output = gr.Textbox(label="Prompt", lines=5)
|
| 224 |
with gr.Row():
|
| 225 |
prompt = gr.Text(
|
|
|
|
| 281 |
value=1.0,
|
| 282 |
)
|
| 283 |
image_encoder_path = gr.Dropdown(
|
| 284 |
+
["google/siglip-so400m-patch14-384", "jancuhel/google-siglip-so400m-patch14-384-img-text-relevancy", "laion/CLIP-ViT-H-14-laion2B-s32B-b79K"],
|
| 285 |
label="CLIP Model",
|
| 286 |
)
|
| 287 |
ip_scale = gr.Slider(
|