Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,61 +1,72 @@
|
|
| 1 |
-
import os, torch
|
| 2 |
from diffusers import StableDiffusionXLPipeline
|
| 3 |
from huggingface_hub import HfApi, hf_hub_download, create_repo
|
| 4 |
|
| 5 |
# ====================== CONFIG ======================
|
| 6 |
BASE_MODEL = "stabilityai/stable-diffusion-xl-base-1.0"
|
| 7 |
LORA_REPO = "CoolKrishh/Comic-SDXL-LoRA"
|
| 8 |
-
LORA_FILE = "
|
| 9 |
DEST_REPO = "CoolKrishh/mythic-sdxl"
|
| 10 |
OUTPUT_CKPT = "mythic-sdxl.safetensors"
|
|
|
|
| 11 |
# ===================================================
|
| 12 |
|
| 13 |
-
print("π Authenticating...")
|
| 14 |
TOKEN = os.getenv("HF_TOKEN")
|
| 15 |
if not TOKEN:
|
| 16 |
raise ValueError("β HF_TOKEN not set!")
|
| 17 |
|
| 18 |
api = HfApi(token=TOKEN)
|
| 19 |
-
|
| 20 |
-
print("π¦ Creating repo if not exists...")
|
| 21 |
create_repo(repo_id=DEST_REPO, exist_ok=True, token=TOKEN)
|
| 22 |
|
| 23 |
-
print("
|
| 24 |
lora_path = hf_hub_download(LORA_REPO, LORA_FILE, token=TOKEN)
|
| 25 |
|
| 26 |
-
print("π Loading base
|
| 27 |
pipe = StableDiffusionXLPipeline.from_pretrained(
|
| 28 |
BASE_MODEL, torch_dtype=torch.float16
|
| 29 |
)
|
| 30 |
|
| 31 |
-
print("𧬠Merging LoRA
|
| 32 |
pipe.load_lora_weights(lora_path, adapter_name="default")
|
| 33 |
-
pipe.fuse_lora(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
pipe.save_pretrained("./out", safe_serialization=True)
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
if file.endswith(".safetensors"):
|
| 42 |
-
os.rename(f"./out/{file}", f"./out/{OUTPUT_CKPT}")
|
| 43 |
|
| 44 |
-
|
|
|
|
| 45 |
|
| 46 |
-
#
|
| 47 |
-
if not os.path.exists(
|
| 48 |
-
raise FileNotFoundError(f"β
|
| 49 |
|
| 50 |
-
print(f"β
|
| 51 |
|
| 52 |
-
|
|
|
|
| 53 |
api.upload_file(
|
| 54 |
-
path_or_fileobj=
|
| 55 |
path_in_repo=OUTPUT_CKPT,
|
| 56 |
repo_id=DEST_REPO,
|
| 57 |
token=TOKEN
|
| 58 |
)
|
| 59 |
|
| 60 |
-
print("\nπ
|
| 61 |
-
print(f"
|
|
|
|
| 1 |
+
import os, torch, shutil
|
| 2 |
from diffusers import StableDiffusionXLPipeline
|
| 3 |
from huggingface_hub import HfApi, hf_hub_download, create_repo
|
| 4 |
|
| 5 |
# ====================== CONFIG ======================
|
| 6 |
BASE_MODEL = "stabilityai/stable-diffusion-xl-base-1.0"
|
| 7 |
LORA_REPO = "CoolKrishh/Comic-SDXL-LoRA"
|
| 8 |
+
LORA_FILE = "comic-sdxl.safetensors"
|
| 9 |
DEST_REPO = "CoolKrishh/mythic-sdxl"
|
| 10 |
OUTPUT_CKPT = "mythic-sdxl.safetensors"
|
| 11 |
+
OUTPUT_DIR = "/app/out"
|
| 12 |
# ===================================================
|
| 13 |
|
|
|
|
| 14 |
TOKEN = os.getenv("HF_TOKEN")
|
| 15 |
if not TOKEN:
|
| 16 |
raise ValueError("β HF_TOKEN not set!")
|
| 17 |
|
| 18 |
api = HfApi(token=TOKEN)
|
|
|
|
|
|
|
| 19 |
create_repo(repo_id=DEST_REPO, exist_ok=True, token=TOKEN)
|
| 20 |
|
| 21 |
+
print("β¬ Downloading LoRA...")
|
| 22 |
lora_path = hf_hub_download(LORA_REPO, LORA_FILE, token=TOKEN)
|
| 23 |
|
| 24 |
+
print("π Loading SDXL base...")
|
| 25 |
pipe = StableDiffusionXLPipeline.from_pretrained(
|
| 26 |
BASE_MODEL, torch_dtype=torch.float16
|
| 27 |
)
|
| 28 |
|
| 29 |
+
print("𧬠Merging LoRA...")
|
| 30 |
pipe.load_lora_weights(lora_path, adapter_name="default")
|
| 31 |
+
pipe.fuse_lora()
|
| 32 |
+
|
| 33 |
+
# Ensure output folder exists
|
| 34 |
+
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 35 |
+
|
| 36 |
+
print("πΎ Saving merged pipeline...")
|
| 37 |
+
pipe.save_pretrained(OUTPUT_DIR, safe_serialization=True)
|
| 38 |
+
|
| 39 |
+
# ---- FIND REAL SAVED SAFETENSORS ----
|
| 40 |
+
print("π Scanning saved files...")
|
| 41 |
+
found = None
|
| 42 |
+
for f in os.listdir(OUTPUT_DIR):
|
| 43 |
+
if f.endswith(".safetensors"):
|
| 44 |
+
found = f
|
| 45 |
+
break
|
| 46 |
|
| 47 |
+
if not found:
|
| 48 |
+
raise FileNotFoundError("β No .safetensors file was created by diffusers!")
|
|
|
|
| 49 |
|
| 50 |
+
src = os.path.join(OUTPUT_DIR, found)
|
| 51 |
+
dst = os.path.join(OUTPUT_DIR, OUTPUT_CKPT)
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
print(f"π Renaming:\n {src}\n β {dst}")
|
| 54 |
+
shutil.move(src, dst)
|
| 55 |
|
| 56 |
+
# ---- VERIFY FINAL FILE EXISTS ----
|
| 57 |
+
if not os.path.exists(dst):
|
| 58 |
+
raise FileNotFoundError(f"β Still missing after rename: {dst}")
|
| 59 |
|
| 60 |
+
print(f"β
Final model file ready at:\n{dst}")
|
| 61 |
|
| 62 |
+
# ---- UPLOAD ----
|
| 63 |
+
print("β Uploading to Hugging Face...")
|
| 64 |
api.upload_file(
|
| 65 |
+
path_or_fileobj=dst,
|
| 66 |
path_in_repo=OUTPUT_CKPT,
|
| 67 |
repo_id=DEST_REPO,
|
| 68 |
token=TOKEN
|
| 69 |
)
|
| 70 |
|
| 71 |
+
print("\nπ SUCCESS! Your model is now uploaded & inference-ready.")
|
| 72 |
+
print(f"π https://huggingface.co/{DEST_REPO}")
|