Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,11 +1,10 @@
|
|
| 1 |
import torch
|
| 2 |
from diffusers import StableDiffusionXLPipeline
|
| 3 |
-
from huggingface_hub import login
|
| 4 |
|
| 5 |
|
| 6 |
class StableDiffusionEngine:
|
| 7 |
"""
|
| 8 |
-
|
| 9 |
"""
|
| 10 |
|
| 11 |
def __init__(
|
|
@@ -13,17 +12,6 @@ class StableDiffusionEngine:
|
|
| 13 |
model_id: str = "stabilityai/sdxl-base-1.0",
|
| 14 |
hf_token: str | None = None,
|
| 15 |
):
|
| 16 |
-
"""
|
| 17 |
-
Initialize the Stable Diffusion pipeline.
|
| 18 |
-
|
| 19 |
-
Args:
|
| 20 |
-
model_id (str): Hugging Face model ID
|
| 21 |
-
hf_token (str): Hugging Face access token
|
| 22 |
-
"""
|
| 23 |
-
|
| 24 |
-
if hf_token:
|
| 25 |
-
login(token=hf_token)
|
| 26 |
-
|
| 27 |
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 28 |
|
| 29 |
self.pipe = StableDiffusionXLPipeline.from_pretrained(
|
|
@@ -31,6 +19,7 @@ class StableDiffusionEngine:
|
|
| 31 |
torch_dtype=torch.float16 if self.device == "cuda" else torch.float32,
|
| 32 |
use_safetensors=True,
|
| 33 |
safety_checker=True,
|
|
|
|
| 34 |
)
|
| 35 |
|
| 36 |
self.pipe.to(self.device)
|
|
@@ -43,20 +32,6 @@ class StableDiffusionEngine:
|
|
| 43 |
width: int = 1024,
|
| 44 |
height: int = 1024,
|
| 45 |
):
|
| 46 |
-
"""
|
| 47 |
-
Generate an image from a text prompt.
|
| 48 |
-
|
| 49 |
-
Args:
|
| 50 |
-
prompt (str): Text prompt
|
| 51 |
-
num_inference_steps (int): Diffusion steps
|
| 52 |
-
guidance_scale (float): CFG scale
|
| 53 |
-
width (int): Image width
|
| 54 |
-
height (int): Image height
|
| 55 |
-
|
| 56 |
-
Returns:
|
| 57 |
-
PIL.Image.Image
|
| 58 |
-
"""
|
| 59 |
-
|
| 60 |
with torch.no_grad():
|
| 61 |
image = self.pipe(
|
| 62 |
prompt=prompt,
|
|
|
|
| 1 |
import torch
|
| 2 |
from diffusers import StableDiffusionXLPipeline
|
|
|
|
| 3 |
|
| 4 |
|
| 5 |
class StableDiffusionEngine:
|
| 6 |
"""
|
| 7 |
+
Stable Diffusion XL Engine (HF Spaces safe version)
|
| 8 |
"""
|
| 9 |
|
| 10 |
def __init__(
|
|
|
|
| 12 |
model_id: str = "stabilityai/sdxl-base-1.0",
|
| 13 |
hf_token: str | None = None,
|
| 14 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 16 |
|
| 17 |
self.pipe = StableDiffusionXLPipeline.from_pretrained(
|
|
|
|
| 19 |
torch_dtype=torch.float16 if self.device == "cuda" else torch.float32,
|
| 20 |
use_safetensors=True,
|
| 21 |
safety_checker=True,
|
| 22 |
+
token=hf_token, # ✅ CRITICAL FIX
|
| 23 |
)
|
| 24 |
|
| 25 |
self.pipe.to(self.device)
|
|
|
|
| 32 |
width: int = 1024,
|
| 33 |
height: int = 1024,
|
| 34 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
with torch.no_grad():
|
| 36 |
image = self.pipe(
|
| 37 |
prompt=prompt,
|