CoolKrishh commited on
Commit
f5d14b3
Β·
verified Β·
1 Parent(s): 9499889

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -51
app.py CHANGED
@@ -1,71 +1,62 @@
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.")
 
 
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}")