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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -22
app.py CHANGED
@@ -1,31 +1,68 @@
1
- import os
2
- from huggingface_hub import HfApi, HfFolder, upload_folder
3
  from diffusers import StableDiffusionXLPipeline
4
- import torch
 
5
 
6
- base_model = "stabilityai/stable-diffusion-xl-base-1.0"
7
- lora_model_id = "CoolKrishh/Comic-SDXL-LoRA"
8
- output_dir = "comic-sdxl-full"
 
 
 
 
 
9
 
10
- print("⬇️ Loading base model...")
11
- pipe = StableDiffusionXLPipeline.from_pretrained(base_model, torch_dtype=torch.float16)
12
- print("πŸ”— Loading LoRA weights...")
13
- pipe.load_lora_weights(lora_model_id)
14
 
15
- print("πŸ’Ύ Saving merged pipeline...")
16
- pipe.save_pretrained(output_dir)
17
 
18
- # βœ… Use token from environment
19
- token = os.getenv("HF_TOKEN")
 
 
 
 
 
 
 
20
 
21
- if not token:
22
- raise ValueError("❌ No HF_TOKEN found. Add it in your Space secrets.")
 
 
 
 
 
 
23
 
24
- api = HfApi()
25
- repo_id = "CoolKrishh/Comic-SDXL-Full"
 
 
 
 
26
 
27
- print("☁️ Uploading to Hugging Face Hub...")
28
- api.create_repo(repo_id=repo_id, repo_type="model", private=False, exist_ok=True, token=token)
29
- upload_folder(folder_path=output_dir, repo_id=repo_id, repo_type="model", token=token)
30
 
31
- print(f"πŸš€ Uploaded successfully to https://huggingface.co/{repo_id}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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!")