CoolKrishh commited on
Commit
ee19e85
Β·
verified Β·
1 Parent(s): 7338dd2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -25
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 = "Comic-SDXL.safetensors"
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("⬇️ Downloading LoRA from Hugging Face...")
24
  lora_path = hf_hub_download(LORA_REPO, LORA_FILE, token=TOKEN)
25
 
26
- print("πŸš€ Loading base SDXL...")
27
  pipe = StableDiffusionXLPipeline.from_pretrained(
28
  BASE_MODEL, torch_dtype=torch.float16
29
  )
30
 
31
- print("🧬 Merging LoRA into model...")
32
  pipe.load_lora_weights(lora_path, adapter_name="default")
33
- pipe.fuse_lora(lora_scale=1.0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
- print("πŸ’Ύ Saving merged model...")
36
- os.makedirs("./out", exist_ok=True)
37
- pipe.save_pretrained("./out", safe_serialization=True)
38
 
39
- # Rename to final checkpoint name
40
- for file in os.listdir("./out"):
41
- if file.endswith(".safetensors"):
42
- os.rename(f"./out/{file}", f"./out/{OUTPUT_CKPT}")
43
 
44
- file_path = os.path.abspath(f"./out/{OUTPUT_CKPT}")
 
45
 
46
- # Verify file exists
47
- if not os.path.exists(file_path):
48
- raise FileNotFoundError(f"❌ File NOT found! Path: {file_path}")
49
 
50
- print(f"βœ… Model saved at: {file_path}")
51
 
52
- print("☁️ Uploading to Hugging Face...")
 
53
  api.upload_file(
54
- path_or_fileobj=file_path,
55
  path_in_repo=OUTPUT_CKPT,
56
  repo_id=DEST_REPO,
57
  token=TOKEN
58
  )
59
 
60
- print("\nπŸŽ‰ DONE β€” Your model is now Inference API ready!")
61
- print(f"πŸ‘‰ https://huggingface.co/{DEST_REPO}")
 
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}")