Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -5,7 +5,6 @@ import gradio as gr
|
|
5 |
from PIL import Image
|
6 |
from models_transformer_sd3 import SD3Transformer2DModel
|
7 |
from pipeline_stable_diffusion_3_ipa import StableDiffusion3Pipeline
|
8 |
-
import gc
|
9 |
import os
|
10 |
from huggingface_hub import login
|
11 |
|
@@ -14,6 +13,7 @@ login(TOKEN)
|
|
14 |
|
15 |
model_path = 'stabilityai/stable-diffusion-3.5-large'
|
16 |
ip_adapter_path = './ip-adapter.bin'
|
|
|
17 |
image_encoder_path = "google/siglip-so400m-patch14-384"
|
18 |
|
19 |
transformer = SD3Transformer2DModel.from_pretrained(
|
@@ -22,7 +22,7 @@ transformer = SD3Transformer2DModel.from_pretrained(
|
|
22 |
|
23 |
pipe = StableDiffusion3Pipeline.from_pretrained(
|
24 |
model_path, transformer=transformer, torch_dtype=torch.bfloat16
|
25 |
-
)
|
26 |
|
27 |
pipe.init_ipadapter(
|
28 |
ip_adapter_path=ip_adapter_path,
|
@@ -35,6 +35,13 @@ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
|
35 |
seed = random.randint(0, 2000)
|
36 |
return seed
|
37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
@spaces.GPU() ## For ZeroGPU
|
39 |
def create_image(image_pil,
|
40 |
prompt,
|
@@ -47,6 +54,9 @@ def create_image(image_pil,
|
|
47 |
target="Load only style blocks",
|
48 |
):
|
49 |
|
|
|
|
|
|
|
50 |
if target !="Load original IP-Adapter":
|
51 |
if target=="Load only style blocks":
|
52 |
scale = {
|
@@ -63,12 +73,10 @@ def create_image(image_pil,
|
|
63 |
}
|
64 |
#pipe.set_ip_adapter_scale(scale) ## Waiting for SD3 Diffuser integration
|
65 |
|
66 |
-
if
|
67 |
-
|
68 |
-
else: # If it's already a PIL Image
|
69 |
-
style_image = image_pil.convert('RGB')
|
70 |
|
71 |
-
|
72 |
generator = torch.Generator().manual_seed(randomize_seed_fn(seed, True))
|
73 |
|
74 |
image = pipe(
|
@@ -76,20 +84,10 @@ def create_image(image_pil,
|
|
76 |
height=1024,
|
77 |
prompt=prompt,
|
78 |
negative_prompt="lowres, low quality, worst quality",
|
79 |
-
num_inference_steps=24,
|
80 |
-
guidance_scale=guidance_scale,
|
81 |
generator=generator, ## For ZeroGPU no device="cpu"
|
82 |
-
clip_image=
|
83 |
-
ipadapter_scale=
|
84 |
).images[0]
|
85 |
-
|
86 |
-
|
87 |
-
if torch.cuda.is_available():
|
88 |
-
torch.cuda.empty_cache()
|
89 |
-
gc.collect()
|
90 |
-
|
91 |
-
|
92 |
-
print(image)
|
93 |
|
94 |
return image
|
95 |
|
|
|
5 |
from PIL import Image
|
6 |
from models_transformer_sd3 import SD3Transformer2DModel
|
7 |
from pipeline_stable_diffusion_3_ipa import StableDiffusion3Pipeline
|
|
|
8 |
import os
|
9 |
from huggingface_hub import login
|
10 |
|
|
|
13 |
|
14 |
model_path = 'stabilityai/stable-diffusion-3.5-large'
|
15 |
ip_adapter_path = './ip-adapter.bin'
|
16 |
+
##ipadapter_path = hf_hub_download(repo_id="InstantX/SD3.5-Large-IP-Adapter", filename="ip-adapter.bin")
|
17 |
image_encoder_path = "google/siglip-so400m-patch14-384"
|
18 |
|
19 |
transformer = SD3Transformer2DModel.from_pretrained(
|
|
|
22 |
|
23 |
pipe = StableDiffusion3Pipeline.from_pretrained(
|
24 |
model_path, transformer=transformer, torch_dtype=torch.bfloat16
|
25 |
+
).to("cuda")
|
26 |
|
27 |
pipe.init_ipadapter(
|
28 |
ip_adapter_path=ip_adapter_path,
|
|
|
35 |
seed = random.randint(0, 2000)
|
36 |
return seed
|
37 |
|
38 |
+
def resize_img(image, max_size=1024):
|
39 |
+
width, height = image.size
|
40 |
+
scaling_factor = min(max_size / width, max_size / height)
|
41 |
+
new_width = int(width * scaling_factor)
|
42 |
+
new_height = int(height * scaling_factor)
|
43 |
+
return image.resize((new_width, new_height), Image.LANCZOS)
|
44 |
+
|
45 |
@spaces.GPU() ## For ZeroGPU
|
46 |
def create_image(image_pil,
|
47 |
prompt,
|
|
|
54 |
target="Load only style blocks",
|
55 |
):
|
56 |
|
57 |
+
if image_pil is None:
|
58 |
+
return None
|
59 |
+
|
60 |
if target !="Load original IP-Adapter":
|
61 |
if target=="Load only style blocks":
|
62 |
scale = {
|
|
|
73 |
}
|
74 |
#pipe.set_ip_adapter_scale(scale) ## Waiting for SD3 Diffuser integration
|
75 |
|
76 |
+
if not isinstance(image_pil, Image.Image): # If it's a file
|
77 |
+
image_pil = Image.fromarray(image_pil)
|
|
|
|
|
78 |
|
79 |
+
image_pil = resize_img(image_pil)
|
80 |
generator = torch.Generator().manual_seed(randomize_seed_fn(seed, True))
|
81 |
|
82 |
image = pipe(
|
|
|
84 |
height=1024,
|
85 |
prompt=prompt,
|
86 |
negative_prompt="lowres, low quality, worst quality",
|
|
|
|
|
87 |
generator=generator, ## For ZeroGPU no device="cpu"
|
88 |
+
clip_image=image_pil,
|
89 |
+
ipadapter_scale=1,
|
90 |
).images[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
|
92 |
return image
|
93 |
|