Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,18 +7,29 @@ import spaces
|
|
| 7 |
import gradio as gr
|
| 8 |
|
| 9 |
from diffusers import AutoPipelineForText2Image
|
| 10 |
-
from
|
| 11 |
-
|
| 12 |
-
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
| 13 |
-
if randomize_seed:
|
| 14 |
-
seed = random.randint(0, 2000)
|
| 15 |
-
return seed
|
| 16 |
|
| 17 |
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
|
| 18 |
dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
| 19 |
pipeline = AutoPipelineForText2Image.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=dtype).to(device)
|
| 20 |
pipeline.load_ip_adapter("h94/IP-Adapter", subfolder="sdxl_models", weight_name="ip-adapter_sdxl.bin", device=device, dtype=dtype)
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
@spaces.GPU()
|
| 23 |
def create_image(image_pil,
|
| 24 |
prompt,
|
|
@@ -46,7 +57,7 @@ def create_image(image_pil,
|
|
| 46 |
}
|
| 47 |
pipeline.set_ip_adapter_scale(scale)
|
| 48 |
|
| 49 |
-
style_image =
|
| 50 |
generator = torch.Generator(device=device).manual_seed(randomize_seed_fn(seed, False))
|
| 51 |
image = pipeline(
|
| 52 |
prompt=prompt,
|
|
|
|
| 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).to(device)
|
| 15 |
pipeline.load_ip_adapter("h94/IP-Adapter", subfolder="sdxl_models", weight_name="ip-adapter_sdxl.bin", device=device, dtype=dtype)
|
| 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,
|
|
|
|
| 57 |
}
|
| 58 |
pipeline.set_ip_adapter_scale(scale)
|
| 59 |
|
| 60 |
+
style_image = preprocess_image(image_pil)
|
| 61 |
generator = torch.Generator(device=device).manual_seed(randomize_seed_fn(seed, False))
|
| 62 |
image = pipeline(
|
| 63 |
prompt=prompt,
|