Spaces:
Running
on
Zero
Running
on
Zero
Merge branch 'main' of https://huggingface.co/spaces/Hatman/InstantStyle
Browse files
app.py
CHANGED
@@ -3,20 +3,44 @@ import random
|
|
3 |
import spaces
|
4 |
import gradio as gr
|
5 |
from PIL import Image
|
6 |
-
from
|
7 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
@spaces.GPU()
|
20 |
def create_image(image_pil,
|
21 |
prompt,
|
22 |
n_prompt,
|
@@ -28,6 +52,9 @@ def create_image(image_pil,
|
|
28 |
target="Load only style blocks",
|
29 |
):
|
30 |
|
|
|
|
|
|
|
31 |
if target !="Load original IP-Adapter":
|
32 |
if target=="Load only style blocks":
|
33 |
scale = {
|
@@ -43,19 +70,25 @@ def create_image(image_pil,
|
|
43 |
"up": {"block_0": [0.0, control_scale, 0.0]},
|
44 |
}
|
45 |
pipe.set_ip_adapter_scale(scale)
|
46 |
-
|
47 |
-
style_image =
|
48 |
-
generator = torch.Generator().manual_seed(randomize_seed_fn(seed, True))
|
49 |
|
50 |
|
51 |
image = pipe(
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
return image
|
61 |
|
|
|
3 |
import spaces
|
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 |
+
import os
|
10 |
+
from huggingface_hub import login
|
11 |
+
|
12 |
+
TOKEN = os.getenv('TOKEN')
|
13 |
+
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 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
20 |
dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
21 |
+
|
22 |
+
transformer = SD3Transformer2DModel.from_pretrained(
|
23 |
+
model_path, subfolder="transformer", torch_dtype=torch.bfloat16
|
24 |
+
)
|
25 |
+
|
26 |
+
pipe = StableDiffusion3Pipeline.from_pretrained(
|
27 |
+
model_path, transformer=transformer, torch_dtype=torch.bfloat16
|
28 |
+
) ## For ZeroGPU no .to("cuda")
|
29 |
+
|
30 |
+
pipe.init_ipadapter(
|
31 |
+
ip_adapter_path=ip_adapter_path,
|
32 |
+
image_encoder_path=image_encoder_path,
|
33 |
+
nb_token=64,
|
34 |
+
)
|
35 |
+
|
36 |
pipe.to(device)
|
37 |
+
|
38 |
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
39 |
if randomize_seed:
|
40 |
seed = random.randint(0, 2000)
|
41 |
return seed
|
42 |
+
|
43 |
+
@spaces.GPU() ## For ZeroGPU
|
44 |
def create_image(image_pil,
|
45 |
prompt,
|
46 |
n_prompt,
|
|
|
52 |
target="Load only style blocks",
|
53 |
):
|
54 |
|
55 |
+
if image_pil is None:
|
56 |
+
return None
|
57 |
+
|
58 |
if target !="Load original IP-Adapter":
|
59 |
if target=="Load only style blocks":
|
60 |
scale = {
|
|
|
70 |
"up": {"block_0": [0.0, control_scale, 0.0]},
|
71 |
}
|
72 |
pipe.set_ip_adapter_scale(scale)
|
73 |
+
|
74 |
+
style_image = Image.open(image_pil).convert('RGB')
|
|
|
75 |
|
76 |
|
77 |
image = pipe(
|
78 |
+
width=1024,
|
79 |
+
height=1024,
|
80 |
+
prompt=prompt,
|
81 |
+
negative_prompt="lowres, low quality, worst quality",
|
82 |
+
num_inference_steps=24,
|
83 |
+
guidance_scale=guidance_scale,
|
84 |
+
generator=torch.Generator("cuda").manual_seed(randomize_seed_fn(seed, True)), ## For ZeroGPU no device="cpu"
|
85 |
+
clip_image=style_image,
|
86 |
+
ipadapter_scale=scale,
|
87 |
+
).images[0]
|
88 |
+
|
89 |
+
if torch.cuda.is_available():
|
90 |
+
torch.cuda.empty_cache()
|
91 |
+
gc.collect()
|
92 |
|
93 |
return image
|
94 |
|