Spaces:
Runtime error
Runtime error
Delete text_to_image.py
Browse files- text_to_image.py +0 -56
text_to_image.py
DELETED
@@ -1,56 +0,0 @@
|
|
1 |
-
import torch
|
2 |
-
import spaces
|
3 |
-
from diffusers import StableDiffusionXLPipeline, EulerDiscreteScheduler
|
4 |
-
from PIL import Image
|
5 |
-
from io import BytesIO
|
6 |
-
from utils import load_unet_model
|
7 |
-
|
8 |
-
@spaces.GPU
|
9 |
-
class TextToImage:
|
10 |
-
"""
|
11 |
-
Class to handle Text-to-Image generation 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 Text-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 = StableDiffusionXLPipeline.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("Text-to-Image model loaded successfully.")
|
38 |
-
|
39 |
-
|
40 |
-
async def generate_image(self, prompt):
|
41 |
-
"""
|
42 |
-
Generate an image from a text prompt.
|
43 |
-
|
44 |
-
Args:
|
45 |
-
prompt (str): The text prompt to generate the image.
|
46 |
-
|
47 |
-
Returns:
|
48 |
-
PIL.Image: The generated image.
|
49 |
-
"""
|
50 |
-
with torch.no_grad():
|
51 |
-
image = self.pipe(
|
52 |
-
prompt,
|
53 |
-
num_inference_steps=4,
|
54 |
-
guidance_scale=0
|
55 |
-
).images[0]
|
56 |
-
return image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|