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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -40
app.py CHANGED
@@ -1,68 +1,71 @@
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!")
 
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.")