Spaces:
Running
on
Zero
Running
on
Zero
hatmanstack
commited on
Commit
·
408db9c
1
Parent(s):
228ed52
stability-ai 3.5
Browse files
app.py
CHANGED
@@ -1,22 +1,41 @@
|
|
1 |
import torch
|
2 |
import random
|
3 |
-
|
4 |
import gradio as gr
|
5 |
-
from
|
6 |
-
from
|
|
|
7 |
import gc
|
8 |
|
|
|
|
|
|
|
|
|
9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
10 |
dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
pipe.to(device)
|
|
|
14 |
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
15 |
if randomize_seed:
|
16 |
seed = random.randint(0, 2000)
|
17 |
return seed
|
18 |
-
|
19 |
-
|
20 |
def create_image(image_pil,
|
21 |
prompt,
|
22 |
n_prompt,
|
@@ -44,18 +63,20 @@ def create_image(image_pil,
|
|
44 |
}
|
45 |
pipe.set_ip_adapter_scale(scale)
|
46 |
|
47 |
-
style_image =
|
48 |
-
generator = torch.Generator(device="cpu").manual_seed(randomize_seed_fn(seed, True)) ## For ZeroGPU no device="cpu"
|
49 |
|
50 |
|
51 |
image = pipe(
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
59 |
|
60 |
if torch.cuda.is_available():
|
61 |
torch.cuda.empty_cache()
|
@@ -87,7 +108,6 @@ article = r"""
|
|
87 |
author={Wang, Haofan and Wang, Qixun and Bai, Xu and Qin, Zekui and Chen, Anthony},
|
88 |
journal={arXiv preprint arXiv:2404.02733},
|
89 |
year={2024}
|
90 |
-
}
|
91 |
```
|
92 |
"""
|
93 |
|
|
|
1 |
import torch
|
2 |
import random
|
3 |
+
import spaces ## For ZeroGPU
|
4 |
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 |
|
10 |
+
model_path = 'stabilityai/stable-diffusion-3.5-large'
|
11 |
+
ip_adapter_path = './ip-adapter.bin'
|
12 |
+
image_encoder_path = "google/siglip-so400m-patch14-384"
|
13 |
+
|
14 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
15 |
dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
16 |
+
|
17 |
+
transformer = SD3Transformer2DModel.from_pretrained(
|
18 |
+
model_path, subfolder="transformer", torch_dtype=torch.bfloat16
|
19 |
+
)
|
20 |
+
|
21 |
+
pipe = StableDiffusion3Pipeline.from_pretrained(
|
22 |
+
model_path, transformer=transformer, torch_dtype=torch.bfloat16
|
23 |
+
) ## For ZeroGPU no .to("cuda")
|
24 |
+
|
25 |
+
pipe.init_ipadapter(
|
26 |
+
ip_adapter_path=ip_adapter_path,
|
27 |
+
image_encoder_path=image_encoder_path,
|
28 |
+
nb_token=64,
|
29 |
+
)
|
30 |
+
|
31 |
pipe.to(device)
|
32 |
+
|
33 |
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
34 |
if randomize_seed:
|
35 |
seed = random.randint(0, 2000)
|
36 |
return seed
|
37 |
+
|
38 |
+
@spaces.GPU() ## For ZeroGPU
|
39 |
def create_image(image_pil,
|
40 |
prompt,
|
41 |
n_prompt,
|
|
|
63 |
}
|
64 |
pipe.set_ip_adapter_scale(scale)
|
65 |
|
66 |
+
style_image = Image.open(image_pil).convert('RGB')
|
|
|
67 |
|
68 |
|
69 |
image = pipe(
|
70 |
+
width=1024,
|
71 |
+
height=1024,
|
72 |
+
prompt=prompt,
|
73 |
+
negative_prompt="lowres, low quality, worst quality",
|
74 |
+
num_inference_steps=24,
|
75 |
+
guidance_scale=guidance_scale,
|
76 |
+
generator=torch.Generator("cuda").manual_seed(randomize_seed_fn(seed, True)), ## For ZeroGPU no device="cpu"
|
77 |
+
clip_image=style_image,
|
78 |
+
ipadapter_scale=scale,
|
79 |
+
).images[0]
|
80 |
|
81 |
if torch.cuda.is_available():
|
82 |
torch.cuda.empty_cache()
|
|
|
108 |
author={Wang, Haofan and Wang, Qixun and Bai, Xu and Qin, Zekui and Chen, Anthony},
|
109 |
journal={arXiv preprint arXiv:2404.02733},
|
110 |
year={2024}
|
|
|
111 |
```
|
112 |
"""
|
113 |
|