Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,68 +1,71 @@
|
|
| 1 |
from diffusers import StableDiffusionXLPipeline
|
| 2 |
-
from huggingface_hub import hf_hub_download, HfApi
|
| 3 |
import torch, os
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
LORA_REPO = "CoolKrishh/Comic-SDXL-LoRA" # LoRA repo on HuggingFace
|
| 9 |
-
LORA_FILENAME = "Comic-SDXL.safetensors" # exact filename in repo
|
| 10 |
-
OUTPUT_REPO = "CoolKrishh/mythic-sdxl" # final full model repo
|
| 11 |
SAVE_DIR = "mythic-sdxl-pipeline"
|
| 12 |
-
# ------------------------------
|
| 13 |
|
| 14 |
print("π Fetching HF token from secrets...")
|
| 15 |
HF_TOKEN = os.getenv("HF_TOKEN") or os.getenv("HUGGINGFACE_TOKEN")
|
| 16 |
-
|
| 17 |
if HF_TOKEN is None:
|
| 18 |
-
raise ValueError("β HF_TOKEN not found! Add in
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
#
|
| 22 |
-
#
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
pipe = StableDiffusionXLPipeline.from_pretrained(
|
| 25 |
"stabilityai/stable-diffusion-xl-base-1.0",
|
| 26 |
torch_dtype=torch.float16
|
| 27 |
)
|
| 28 |
-
pipe.to("cpu")
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
#
|
| 32 |
-
#
|
| 33 |
-
print("β¬οΈ Downloading LoRA weights
|
| 34 |
lora_path = hf_hub_download(
|
| 35 |
repo_id=LORA_REPO,
|
| 36 |
-
filename=LORA_FILENAME
|
|
|
|
| 37 |
)
|
| 38 |
|
| 39 |
-
|
| 40 |
-
# MERGE LoRA INTO PIPELINE
|
| 41 |
-
# ------------------------------
|
| 42 |
-
print("π Applying LoRA weights...")
|
| 43 |
pipe.load_lora_weights(lora_path)
|
| 44 |
-
pipe.fuse_lora()
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
#
|
| 49 |
-
|
| 50 |
-
# ------------------------------
|
| 51 |
-
print("πΎ Saving full pipeline to:", SAVE_DIR)
|
| 52 |
pipe.save_pretrained(SAVE_DIR)
|
| 53 |
|
| 54 |
-
#
|
| 55 |
-
#
|
| 56 |
-
#
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
print(f"β¬οΈ Uploading model to: {OUTPUT_REPO}")
|
| 60 |
-
|
| 61 |
upload_folder(
|
| 62 |
-
repo_id=
|
| 63 |
folder_path=SAVE_DIR,
|
| 64 |
repo_type="model",
|
| 65 |
token=HF_TOKEN
|
| 66 |
)
|
| 67 |
|
| 68 |
-
print("β
DONE
|
|
|
|
| 1 |
from diffusers import StableDiffusionXLPipeline
|
| 2 |
+
from huggingface_hub import hf_hub_download, HfApi, upload_folder
|
| 3 |
import torch, os
|
| 4 |
|
| 5 |
+
HF_REPO = "CoolKrishh/mythic-sdxl" # new repo name
|
| 6 |
+
LORA_REPO = "CoolKrishh/Comic-SDXL-LoRA"
|
| 7 |
+
LORA_FILENAME = "Comic-SDXL.safetensors"
|
|
|
|
|
|
|
|
|
|
| 8 |
SAVE_DIR = "mythic-sdxl-pipeline"
|
|
|
|
| 9 |
|
| 10 |
print("π Fetching HF token from secrets...")
|
| 11 |
HF_TOKEN = os.getenv("HF_TOKEN") or os.getenv("HUGGINGFACE_TOKEN")
|
|
|
|
| 12 |
if HF_TOKEN is None:
|
| 13 |
+
raise ValueError("β HF_TOKEN not found! Add it in Space β Settings β Secrets")
|
| 14 |
+
|
| 15 |
+
api = HfApi()
|
| 16 |
|
| 17 |
+
# ----------------------------------------------------
|
| 18 |
+
# β
Create repo if not exists
|
| 19 |
+
# ----------------------------------------------------
|
| 20 |
+
try:
|
| 21 |
+
api.create_repo(
|
| 22 |
+
repo_id=HF_REPO,
|
| 23 |
+
private=False,
|
| 24 |
+
exist_ok=True,
|
| 25 |
+
token=HF_TOKEN
|
| 26 |
+
)
|
| 27 |
+
print(f"β
Repo exists or created: https://huggingface.co/{HF_REPO}")
|
| 28 |
+
except Exception as e:
|
| 29 |
+
print("β οΈ Repo creation error:", e)
|
| 30 |
+
|
| 31 |
+
# ----------------------------------------------------
|
| 32 |
+
# Download base model
|
| 33 |
+
# ----------------------------------------------------
|
| 34 |
+
print("π Downloading SDXL base config...")
|
| 35 |
pipe = StableDiffusionXLPipeline.from_pretrained(
|
| 36 |
"stabilityai/stable-diffusion-xl-base-1.0",
|
| 37 |
torch_dtype=torch.float16
|
| 38 |
)
|
|
|
|
| 39 |
|
| 40 |
+
# ----------------------------------------------------
|
| 41 |
+
# Download LoRA File
|
| 42 |
+
# ----------------------------------------------------
|
| 43 |
+
print("β¬οΈ Downloading LoRA weights...")
|
| 44 |
lora_path = hf_hub_download(
|
| 45 |
repo_id=LORA_REPO,
|
| 46 |
+
filename=LORA_FILENAME,
|
| 47 |
+
token=HF_TOKEN
|
| 48 |
)
|
| 49 |
|
| 50 |
+
print("π Applying LoRA to pipeline...")
|
|
|
|
|
|
|
|
|
|
| 51 |
pipe.load_lora_weights(lora_path)
|
| 52 |
+
pipe.fuse_lora()
|
| 53 |
|
| 54 |
+
# ----------------------------------------------------
|
| 55 |
+
# Save Pipeline
|
| 56 |
+
# ----------------------------------------------------
|
| 57 |
+
print("πΎ Saving with LoRA merged...")
|
|
|
|
|
|
|
| 58 |
pipe.save_pretrained(SAVE_DIR)
|
| 59 |
|
| 60 |
+
# ----------------------------------------------------
|
| 61 |
+
# Upload to HF
|
| 62 |
+
# ----------------------------------------------------
|
| 63 |
+
print("β¬οΈ Uploading full pipeline to Hugging Face...")
|
|
|
|
|
|
|
|
|
|
| 64 |
upload_folder(
|
| 65 |
+
repo_id=HF_REPO,
|
| 66 |
folder_path=SAVE_DIR,
|
| 67 |
repo_type="model",
|
| 68 |
token=HF_TOKEN
|
| 69 |
)
|
| 70 |
|
| 71 |
+
print("β
DONE! Model uploaded successfully.")
|