Spaces:
Runtime error
Runtime error
Delete image_to_image.py
Browse files- image_to_image.py +0 -63
image_to_image.py
DELETED
@@ -1,63 +0,0 @@
|
|
1 |
-
import torch
|
2 |
-
import spaces
|
3 |
-
from diffusers import StableDiffusionXLImg2ImgPipeline, EulerDiscreteScheduler
|
4 |
-
from PIL import Image
|
5 |
-
from io import BytesIO
|
6 |
-
from utils import load_unet_model
|
7 |
-
|
8 |
-
@spaces.GPU
|
9 |
-
class ImageToImage:
|
10 |
-
"""
|
11 |
-
Class to handle Image-to-Image transformations using Stable Diffusion XL.
|
12 |
-
"""
|
13 |
-
def __init__(self, device="cuda"):
|
14 |
-
# Model and repository details
|
15 |
-
self.base = "stabilityai/stable-diffusion-xl-base-1.0"
|
16 |
-
self.repo = "ByteDance/SDXL-Lightning"
|
17 |
-
self.ckpt = "sdxl_lightning_4step_unet.safetensors"
|
18 |
-
self.device = device
|
19 |
-
|
20 |
-
# Load the UNet model
|
21 |
-
print("Loading Image-to-Image model...")
|
22 |
-
self.unet = load_unet_model(self.base, self.repo, self.ckpt, device=self.device)
|
23 |
-
|
24 |
-
# Initialize the pipeline
|
25 |
-
self.pipe = StableDiffusionXLImg2ImgPipeline.from_pretrained(
|
26 |
-
self.base,
|
27 |
-
unet=self.unet,
|
28 |
-
torch_dtype=torch.float16,
|
29 |
-
variant="fp16"
|
30 |
-
).to(self.device)
|
31 |
-
|
32 |
-
# Set the scheduler
|
33 |
-
self.pipe.scheduler = EulerDiscreteScheduler.from_config(
|
34 |
-
self.pipe.scheduler.config,
|
35 |
-
timestep_spacing="trailing"
|
36 |
-
)
|
37 |
-
print("Image-to-Image model loaded successfully.")
|
38 |
-
|
39 |
-
|
40 |
-
async def transform_image(self, image, prompt):
|
41 |
-
"""
|
42 |
-
Transform an uploaded image based on a text prompt.
|
43 |
-
|
44 |
-
Args:
|
45 |
-
image (PIL.Image): The input image to transform.
|
46 |
-
prompt (str): The text prompt to guide the transformation.
|
47 |
-
|
48 |
-
Returns:
|
49 |
-
PIL.Image: The transformed image.
|
50 |
-
"""
|
51 |
-
if not prompt:
|
52 |
-
raise ValueError("Prompt cannot be empty.")
|
53 |
-
|
54 |
-
# Resize the image as required by the model
|
55 |
-
init_image = image.resize((768, 512))
|
56 |
-
with torch.no_grad():
|
57 |
-
transformed_image = self.pipe(
|
58 |
-
prompt=prompt,
|
59 |
-
image=init_image,
|
60 |
-
strength=0.75,
|
61 |
-
guidance_scale=7.5
|
62 |
-
).images[0]
|
63 |
-
return transformed_image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|