Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,71 +1,62 @@
|
|
| 1 |
from diffusers import StableDiffusionXLPipeline
|
| 2 |
-
from huggingface_hub import
|
| 3 |
import torch, os
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 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 |
-
|
| 22 |
-
|
| 23 |
-
|
| 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 |
-
|
| 42 |
-
# ----------------------------------------------------
|
| 43 |
-
print("β¬οΈ Downloading LoRA weights...")
|
| 44 |
lora_path = hf_hub_download(
|
| 45 |
repo_id=LORA_REPO,
|
| 46 |
filename=LORA_FILENAME,
|
| 47 |
-
token=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
)
|
| 49 |
|
| 50 |
-
print("
|
| 51 |
pipe.load_lora_weights(lora_path)
|
| 52 |
-
pipe.fuse_lora()
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 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=
|
| 66 |
-
folder_path=
|
| 67 |
-
|
| 68 |
-
token=
|
| 69 |
)
|
| 70 |
|
| 71 |
-
print("β
DONE!
|
|
|
|
|
|
| 1 |
from diffusers import StableDiffusionXLPipeline
|
| 2 |
+
from huggingface_hub import HfApi, HfFolder, create_repo, hf_hub_download, upload_folder
|
| 3 |
import torch, os
|
| 4 |
|
| 5 |
+
# ---------------------------------------------------
|
| 6 |
+
# β
CONFIGURATION β CHANGE THESE
|
| 7 |
+
# ---------------------------------------------------
|
| 8 |
+
BASE_MODEL = "stabilityai/stable-diffusion-xl-base-1.0"
|
| 9 |
+
LORA_REPO = "CoolKrishh/Comic-SDXL-LoRA" # where LoRA exists
|
| 10 |
+
LORA_FILENAME = "comic-sdxl.safetensors" # LoRA file name inside repo
|
| 11 |
|
| 12 |
+
REPO_ID = "CoolKrishh/mythic-sdxl" # final merged model repo
|
| 13 |
+
# ---------------------------------------------------
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
token = os.getenv("HF_TOKEN")
|
| 16 |
+
if not token:
|
| 17 |
+
raise ValueError("β Missing HF_TOKEN env var!")
|
| 18 |
+
|
| 19 |
+
print("πΉ Authenticatingβ¦")
|
| 20 |
+
HfFolder.save_token(token)
|
| 21 |
api = HfApi()
|
| 22 |
|
| 23 |
+
# β
create repo (ignore if exists)
|
|
|
|
|
|
|
| 24 |
try:
|
| 25 |
+
create_repo(REPO_ID, exist_ok=True, private=False)
|
| 26 |
+
except Exception:
|
| 27 |
+
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
# β
download LoRA file from HF repo
|
| 30 |
+
print("β¬οΈ Downloading LoRA from Hugging Face...")
|
|
|
|
|
|
|
| 31 |
lora_path = hf_hub_download(
|
| 32 |
repo_id=LORA_REPO,
|
| 33 |
filename=LORA_FILENAME,
|
| 34 |
+
token=token
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
print(f"β
LoRA downloaded: {lora_path}")
|
| 38 |
+
|
| 39 |
+
print("πΉ Loading SDXL base pipeline...")
|
| 40 |
+
pipe = StableDiffusionXLPipeline.from_pretrained(
|
| 41 |
+
BASE_MODEL,
|
| 42 |
+
torch_dtype=torch.float16
|
| 43 |
)
|
| 44 |
|
| 45 |
+
print("πΉ Applying LoRA weights into pipeline...")
|
| 46 |
pipe.load_lora_weights(lora_path)
|
| 47 |
+
pipe.fuse_lora() # <-- merges LoRA into UNet + Text Encoder
|
| 48 |
|
| 49 |
+
OUTPUT_DIR = "./merged_sdxl"
|
| 50 |
+
print(f"πΎ Saving merged pipeline to {OUTPUT_DIR}...")
|
| 51 |
+
pipe.save_pretrained(OUTPUT_DIR, safe_serialization=True)
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
print("βοΈ Uploading merged model to Hugging Face...")
|
|
|
|
|
|
|
|
|
|
| 54 |
upload_folder(
|
| 55 |
+
repo_id=REPO_ID,
|
| 56 |
+
folder_path=OUTPUT_DIR,
|
| 57 |
+
commit_message="Merged SDXL + LoRA β full diffusers pipeline",
|
| 58 |
+
token=token
|
| 59 |
)
|
| 60 |
|
| 61 |
+
print("\nβ
DONE! Your merged SDXL model is uploaded and ready for Inference API.")
|
| 62 |
+
print(f"π https://huggingface.co/{REPO_ID}")
|