Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,30 +7,20 @@ import spaces
|
|
| 7 |
import gradio as gr
|
| 8 |
|
| 9 |
from diffusers import AutoPipelineForText2Image
|
|
|
|
| 10 |
from torchvision import transforms
|
| 11 |
|
| 12 |
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
|
| 13 |
dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
| 14 |
-
pipeline = AutoPipelineForText2Image.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=dtype
|
| 15 |
-
pipeline.load_ip_adapter("h94/IP-Adapter", subfolder="sdxl_models", weight_name="ip-adapter_sdxl.bin"
|
| 16 |
-
|
| 17 |
-
def preprocess_image(image_pil, target_size=(1024, 1024)):
|
| 18 |
-
# Resize and convert to tensor
|
| 19 |
-
transform = transforms.Compose([
|
| 20 |
-
transforms.Resize(target_size, interpolation=transforms.InterpolationMode.LANCZOS),
|
| 21 |
-
transforms.ToTensor(),
|
| 22 |
-
])
|
| 23 |
-
image = transform(image_pil).unsqueeze(0)
|
| 24 |
-
image = image.to(device=device, dtype=dtype)
|
| 25 |
-
|
| 26 |
-
return image
|
| 27 |
|
| 28 |
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
| 29 |
if randomize_seed:
|
| 30 |
seed = random.randint(0, 2000)
|
| 31 |
return seed
|
| 32 |
|
| 33 |
-
@spaces.GPU()
|
| 34 |
def create_image(image_pil,
|
| 35 |
prompt,
|
| 36 |
n_prompt,
|
|
@@ -57,8 +47,9 @@ def create_image(image_pil,
|
|
| 57 |
}
|
| 58 |
pipeline.set_ip_adapter_scale(scale)
|
| 59 |
|
| 60 |
-
style_image =
|
| 61 |
generator = torch.Generator(device=device).manual_seed(randomize_seed_fn(seed, False))
|
|
|
|
| 62 |
image = pipeline(
|
| 63 |
prompt=prompt,
|
| 64 |
ip_adapter_image=style_image,
|
|
|
|
| 7 |
import gradio as gr
|
| 8 |
|
| 9 |
from diffusers import AutoPipelineForText2Image
|
| 10 |
+
from diffusers.utils import load_image
|
| 11 |
from torchvision import transforms
|
| 12 |
|
| 13 |
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
|
| 14 |
dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
| 15 |
+
pipeline = AutoPipelineForText2Image.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=dtype, device_map=device)
|
| 16 |
+
pipeline.load_ip_adapter("h94/IP-Adapter", subfolder="sdxl_models", weight_name="ip-adapter_sdxl.bin")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
| 19 |
if randomize_seed:
|
| 20 |
seed = random.randint(0, 2000)
|
| 21 |
return seed
|
| 22 |
|
| 23 |
+
@spaces.GPU(enable_queue=True)
|
| 24 |
def create_image(image_pil,
|
| 25 |
prompt,
|
| 26 |
n_prompt,
|
|
|
|
| 47 |
}
|
| 48 |
pipeline.set_ip_adapter_scale(scale)
|
| 49 |
|
| 50 |
+
style_image = load_image(image_pil)
|
| 51 |
generator = torch.Generator(device=device).manual_seed(randomize_seed_fn(seed, False))
|
| 52 |
+
|
| 53 |
image = pipeline(
|
| 54 |
prompt=prompt,
|
| 55 |
ip_adapter_image=style_image,
|