Spaces:
Sleeping
Sleeping
Update core/image_generator.py
Browse files- core/image_generator.py +16 -0
core/image_generator.py
CHANGED
|
@@ -6,6 +6,15 @@ from huggingface_hub import hf_hub_download
|
|
| 6 |
from pathlib import Path
|
| 7 |
from typing import List
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
# ---------------- MODEL CONFIG ----------------
|
| 10 |
MODEL_REPO = "SG161222/RealVisXL_V4.0"
|
| 11 |
MODEL_FILENAME = "realvisxlV40_v40LightningBakedvae.safetensors"
|
|
@@ -51,6 +60,13 @@ def load_pipeline() -> StableDiffusionXLPipeline:
|
|
| 51 |
# ---------------- GLOBAL PIPELINE CACHE ----------------
|
| 52 |
pipe: StableDiffusionXLPipeline | None = None
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
# ---------------- IMAGE GENERATION ----------------
|
| 55 |
def generate_images(prompt: str, seed: int = None, num_images: int = 3) -> List:
|
| 56 |
"""
|
|
|
|
| 6 |
from pathlib import Path
|
| 7 |
from typing import List
|
| 8 |
|
| 9 |
+
|
| 10 |
+
# ---------------- HF CACHE FIX ----------------
|
| 11 |
+
# Force Hugging Face to use a writable cache folder
|
| 12 |
+
HF_CACHE_DIR = Path("/tmp/hf_cache")
|
| 13 |
+
HF_CACHE_DIR.mkdir(parents=True, exist_ok=True)
|
| 14 |
+
os.environ["HF_HOME"] = str(HF_CACHE_DIR)
|
| 15 |
+
os.environ["TRANSFORMERS_CACHE"] = str(HF_CACHE_DIR)
|
| 16 |
+
os.environ["XDG_CACHE_HOME"] = str(HF_CACHE_DIR)
|
| 17 |
+
|
| 18 |
# ---------------- MODEL CONFIG ----------------
|
| 19 |
MODEL_REPO = "SG161222/RealVisXL_V4.0"
|
| 20 |
MODEL_FILENAME = "realvisxlV40_v40LightningBakedvae.safetensors"
|
|
|
|
| 60 |
# ---------------- GLOBAL PIPELINE CACHE ----------------
|
| 61 |
pipe: StableDiffusionXLPipeline | None = None
|
| 62 |
|
| 63 |
+
def pil_to_base64(img: Image.Image) -> str:
|
| 64 |
+
buffered = BytesIO()
|
| 65 |
+
img.save(buffered, format="PNG")
|
| 66 |
+
img_bytes = buffered.getvalue()
|
| 67 |
+
img_b64 = base64.b64encode(img_bytes).decode("utf-8")
|
| 68 |
+
return f"data:image/png;base64,{img_b64}"
|
| 69 |
+
|
| 70 |
# ---------------- IMAGE GENERATION ----------------
|
| 71 |
def generate_images(prompt: str, seed: int = None, num_images: int = 3) -> List:
|
| 72 |
"""
|