Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,27 +1,22 @@
|
|
1 |
-
import sys
|
2 |
-
sys.path.append('./')
|
3 |
-
|
4 |
import torch
|
5 |
import random
|
6 |
import spaces
|
7 |
import gradio as gr
|
8 |
|
9 |
from diffusers import AutoPipelineForText2Image
|
10 |
-
from transformers import CLIPVisionModelWithProjection
|
11 |
from diffusers.utils import load_image
|
12 |
-
from torchvision import transforms
|
13 |
|
14 |
-
device =
|
15 |
dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
16 |
-
|
17 |
-
|
18 |
|
19 |
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
20 |
if randomize_seed:
|
21 |
seed = random.randint(0, 2000)
|
22 |
return seed
|
23 |
|
24 |
-
@spaces.GPU(
|
25 |
def create_image(image_pil,
|
26 |
prompt,
|
27 |
n_prompt,
|
@@ -47,20 +42,19 @@ def create_image(image_pil,
|
|
47 |
"down": {"block_2": [0.0, control_scale]},
|
48 |
"up": {"block_0": [0.0, control_scale, 0.0]},
|
49 |
}
|
50 |
-
|
51 |
|
52 |
style_image = load_image(image_pil)
|
53 |
-
generator = torch.Generator(
|
54 |
|
55 |
|
56 |
-
image =
|
57 |
prompt=prompt,
|
58 |
ip_adapter_image=style_image,
|
59 |
negative_prompt=n_prompt,
|
60 |
guidance_scale=guidance_scale,
|
61 |
num_inference_steps=num_inference_steps,
|
62 |
generator=generator,
|
63 |
-
device=device
|
64 |
)
|
65 |
return image
|
66 |
|
|
|
|
|
|
|
|
|
1 |
import torch
|
2 |
import random
|
3 |
import spaces
|
4 |
import gradio as gr
|
5 |
|
6 |
from diffusers import AutoPipelineForText2Image
|
|
|
7 |
from diffusers.utils import load_image
|
|
|
8 |
|
9 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
10 |
dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
11 |
+
pipe = AutoPipelineForText2Image.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=dtype).to("cuda")
|
12 |
+
pipe.load_ip_adapter("h94/IP-Adapter", subfolder="sdxl_models", weight_name="ip-adapter_sdxl.bin")
|
13 |
|
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,
|
|
|
42 |
"down": {"block_2": [0.0, control_scale]},
|
43 |
"up": {"block_0": [0.0, control_scale, 0.0]},
|
44 |
}
|
45 |
+
pipe.set_ip_adapter_scale(scale)
|
46 |
|
47 |
style_image = load_image(image_pil)
|
48 |
+
generator = torch.Generator().manual_seed(randomize_seed_fn(seed, True))
|
49 |
|
50 |
|
51 |
+
image = pipe(
|
52 |
prompt=prompt,
|
53 |
ip_adapter_image=style_image,
|
54 |
negative_prompt=n_prompt,
|
55 |
guidance_scale=guidance_scale,
|
56 |
num_inference_steps=num_inference_steps,
|
57 |
generator=generator,
|
|
|
58 |
)
|
59 |
return image
|
60 |
|