Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,31 +1,68 @@
|
|
| 1 |
-
import os
|
| 2 |
-
from huggingface_hub import HfApi, HfFolder, upload_folder
|
| 3 |
from diffusers import StableDiffusionXLPipeline
|
| 4 |
-
import
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
print("
|
| 11 |
-
|
| 12 |
-
print("π Loading LoRA weights...")
|
| 13 |
-
pipe.load_lora_weights(lora_model_id)
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
print("
|
| 28 |
-
api.create_repo(repo_id=repo_id, repo_type="model", private=False, exist_ok=True, token=token)
|
| 29 |
-
upload_folder(folder_path=output_dir, repo_id=repo_id, repo_type="model", token=token)
|
| 30 |
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from diffusers import StableDiffusionXLPipeline
|
| 2 |
+
from huggingface_hub import hf_hub_download, HfApi
|
| 3 |
+
import torch, os
|
| 4 |
|
| 5 |
+
# ------------------------------
|
| 6 |
+
# SETTINGS
|
| 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 HF Space β Secrets β New variable β HF_TOKEN")
|
| 19 |
|
| 20 |
+
# ------------------------------
|
| 21 |
+
# DOWNLOAD BASE MODEL
|
| 22 |
+
# ------------------------------
|
| 23 |
+
print("π Downloading SDXL base pipeline...")
|
| 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 |
+
# DOWNLOAD LORA WEIGHTS
|
| 32 |
+
# ------------------------------
|
| 33 |
+
print("β¬οΈ Downloading LoRA weights from:", LORA_REPO)
|
| 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() # <---- applies LoRA permanently into UNet/TextEncoder
|
| 45 |
|
| 46 |
+
print("β
LoRA successfully merged into the base model!")
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
# ------------------------------
|
| 49 |
+
# SAVE FULL PIPELINE
|
| 50 |
+
# ------------------------------
|
| 51 |
+
print("πΎ Saving full pipeline to:", SAVE_DIR)
|
| 52 |
+
pipe.save_pretrained(SAVE_DIR)
|
| 53 |
+
|
| 54 |
+
# ------------------------------
|
| 55 |
+
# UPLOAD MODEL TO HF
|
| 56 |
+
# ------------------------------
|
| 57 |
+
from huggingface_hub import upload_folder
|
| 58 |
+
|
| 59 |
+
print(f"β¬οΈ Uploading model to: {OUTPUT_REPO}")
|
| 60 |
+
|
| 61 |
+
upload_folder(
|
| 62 |
+
repo_id=OUTPUT_REPO,
|
| 63 |
+
folder_path=SAVE_DIR,
|
| 64 |
+
repo_type="model",
|
| 65 |
+
token=HF_TOKEN
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
print("β
DONE β Full model uploaded to Hugging Face successfully!")
|