Spaces:
Running
Update app.py
Browse filesTrying to get it to work, sorry my commit messages are slack
Key Changes and Explanations:
CSS Styling:
The CSS is now correctly passed as a string to the css parameter of gr.Blocks().
I've added an elem_id="main-container" to a gr.Column that wraps the input components and the button. This is important because the CSS targets #main-container to control the layout. Without this, the CSS wouldn't apply correctly.
Markdown Content:
The Markdown content is integrated using gr.Markdown().
I've made some minor formatting improvements:
Used more specific headings (e.g., "### π₯ Input Sources Supported:").
Used bold text (**) to highlight important points (like "CPU" and "WRITE").
Added Markdown links for the Hugging Face token page, the GitHub repository, and the Ko-fi page. The format is [Link Text](URL).
gr.Column: Using a gr.Column with elem_id="main-container" is crucial for applying the CSS that controls the layout (making the button stick to the bottom).
No other code changes: Functionality remains the same.
Key Changes in this Complete Code:
cached_download Import Removed: The unnecessary import of cached_download is removed.
get_from_cache Used Correctly: The download_model function now correctly uses get_from_cache to check for cached URLs.
Manual Download and Caching: If a URL is not cached, the code downloads it using requests, determines a filename, and saves it to the standard Hugging Face cache directory (HUGGINGFACE_HUB_CACHE).
HF Cache dir: Downloads now go to the correct HF cache, inside a subfolder called "downloads".
HUGGINGFACE_HUB_CACHE Imported: The constant for the cache directory is imported.
|
@@ -13,31 +13,45 @@ import subprocess
|
|
| 13 |
from urllib.parse import urlparse, unquote
|
| 14 |
from pathlib import Path
|
| 15 |
import tempfile
|
| 16 |
-
#from tqdm import tqdm
|
| 17 |
import psutil
|
| 18 |
import math
|
| 19 |
import shutil
|
| 20 |
import hashlib
|
| 21 |
from datetime import datetime
|
| 22 |
from typing import Dict, List, Optional
|
| 23 |
-
from huggingface_hub import login, HfApi, hf_hub_download #
|
| 24 |
from huggingface_hub.utils import validate_repo_id, HFValidationError
|
| 25 |
from huggingface_hub.errors import HfHubHTTPError
|
| 26 |
-
from huggingface_hub import
|
| 27 |
-
|
| 28 |
-
from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE
|
| 29 |
|
| 30 |
# ---------------------- DEPENDENCIES ----------------------
|
| 31 |
def install_dependencies_gradio():
|
| 32 |
"""Installs the necessary dependencies."""
|
| 33 |
try:
|
| 34 |
-
subprocess.run(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
print("Dependencies installed successfully.")
|
| 36 |
except Exception as e:
|
| 37 |
print(f"Error installing dependencies: {e}")
|
| 38 |
|
|
|
|
| 39 |
# ---------------------- UTILITY FUNCTIONS ----------------------
|
| 40 |
|
|
|
|
| 41 |
def increment_filename(filename):
|
| 42 |
"""Increments the filename to avoid overwriting existing files."""
|
| 43 |
base, ext = os.path.splitext(filename)
|
|
@@ -47,10 +61,15 @@ def increment_filename(filename):
|
|
| 47 |
counter += 1
|
| 48 |
return filename
|
| 49 |
|
|
|
|
| 50 |
# ---------------------- UPLOAD FUNCTION ----------------------
|
| 51 |
def create_model_repo(api, user, orgs_name, model_name, make_private=False):
|
| 52 |
"""Creates a Hugging Face model repository."""
|
| 53 |
-
repo_id =
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
try:
|
| 55 |
api.create_repo(repo_id=repo_id, repo_type="model", private=make_private)
|
| 56 |
print(f"Model repo '{repo_id}' created.")
|
|
@@ -58,6 +77,7 @@ def create_model_repo(api, user, orgs_name, model_name, make_private=False):
|
|
| 58 |
print(f"Model repo '{repo_id}' already exists.")
|
| 59 |
return repo_id
|
| 60 |
|
|
|
|
| 61 |
# ---------------------- MODEL LOADING AND CONVERSION ----------------------
|
| 62 |
def download_model(model_path_or_url):
|
| 63 |
"""Downloads a model, handling URLs, HF repos, and local paths, caching appropriately."""
|
|
@@ -65,14 +85,16 @@ def download_model(model_path_or_url):
|
|
| 65 |
# 1. Check if it's a valid Hugging Face repo ID (and potentially a file within)
|
| 66 |
try:
|
| 67 |
validate_repo_id(model_path_or_url)
|
| 68 |
-
# It's a valid repo ID; use hf_hub_download
|
| 69 |
local_path = hf_hub_download(repo_id=model_path_or_url)
|
| 70 |
return local_path
|
| 71 |
except HFValidationError:
|
| 72 |
pass # Not a simple repo ID. Might be repo ID + filename, or a URL.
|
| 73 |
|
| 74 |
# 2. Check if it's a URL
|
| 75 |
-
if model_path_or_url.startswith("http://") or model_path_or_url.startswith(
|
|
|
|
|
|
|
| 76 |
# Check if it's already in the cache
|
| 77 |
cache_path = get_from_cache(model_path_or_url) # Use get_from_cache
|
| 78 |
if cache_path is not None:
|
|
@@ -86,11 +108,11 @@ def download_model(model_path_or_url):
|
|
| 86 |
parsed_url = urlparse(model_path_or_url)
|
| 87 |
filename = os.path.basename(unquote(parsed_url.path))
|
| 88 |
if not filename:
|
| 89 |
-
|
| 90 |
|
| 91 |
-
# Construct the cache path (using HF_HUB_CACHE + "downloads"
|
| 92 |
cache_dir = os.path.join(HUGGINGFACE_HUB_CACHE, "downloads")
|
| 93 |
-
os.makedirs(cache_dir, exist_ok=True)
|
| 94 |
local_path = os.path.join(cache_dir, filename)
|
| 95 |
|
| 96 |
with open(local_path, "wb") as f:
|
|
@@ -112,7 +134,7 @@ def download_model(model_path_or_url):
|
|
| 112 |
local_path = hf_hub_download(repo_id=repo_id, filename=filename)
|
| 113 |
return local_path
|
| 114 |
else:
|
| 115 |
-
|
| 116 |
|
| 117 |
except HFValidationError:
|
| 118 |
raise ValueError(f"Invalid model path or URL: {model_path_or_url}")
|
|
@@ -125,9 +147,11 @@ def load_sdxl_checkpoint(checkpoint_path):
|
|
| 125 |
"""Loads an SDXL checkpoint (.ckpt or .safetensors) and returns components."""
|
| 126 |
|
| 127 |
if checkpoint_path.endswith(".safetensors"):
|
| 128 |
-
state_dict = load_file(checkpoint_path, device="cpu")
|
| 129 |
elif checkpoint_path.endswith(".ckpt"):
|
| 130 |
-
state_dict = torch.load(checkpoint_path, map_location="cpu")[
|
|
|
|
|
|
|
| 131 |
else:
|
| 132 |
raise ValueError("Unsupported checkpoint format. Must be .safetensors or .ckpt")
|
| 133 |
|
|
@@ -138,17 +162,34 @@ def load_sdxl_checkpoint(checkpoint_path):
|
|
| 138 |
|
| 139 |
for key, value in state_dict.items():
|
| 140 |
if key.startswith("first_stage_model."): # VAE
|
| 141 |
-
vae_state[key.replace("first_stage_model.", "")] = value.to(
|
|
|
|
|
|
|
| 142 |
elif key.startswith("condition_model.model.text_encoder."): # Text Encoder 1
|
| 143 |
-
text_encoder1_state[
|
| 144 |
-
|
| 145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
elif key.startswith("model.diffusion_model."): # UNet
|
| 147 |
-
unet_state[key.replace("model.diffusion_model.", "")] = value.to(
|
|
|
|
|
|
|
| 148 |
|
| 149 |
return text_encoder1_state, text_encoder2_state, vae_state, unet_state
|
| 150 |
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
| 152 |
"""Builds the Diffusers pipeline components from the loaded state dicts."""
|
| 153 |
|
| 154 |
# Default to SDXL base 1.0 if no reference model is provided
|
|
@@ -156,8 +197,12 @@ def build_diffusers_model(text_encoder1_state, text_encoder2_state, vae_state, u
|
|
| 156 |
reference_model_path = "stabilityai/stable-diffusion-xl-base-1.0"
|
| 157 |
|
| 158 |
# 1. Text Encoders
|
| 159 |
-
config_text_encoder1 = CLIPTextConfig.from_pretrained(
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
text_encoder1 = CLIPTextModel(config_text_encoder1)
|
| 163 |
text_encoder2 = CLIPTextModel(config_text_encoder2)
|
|
@@ -179,9 +224,11 @@ def build_diffusers_model(text_encoder1_state, text_encoder2_state, vae_state, u
|
|
| 179 |
return text_encoder1, text_encoder2, vae, unet
|
| 180 |
|
| 181 |
|
| 182 |
-
|
| 183 |
-
|
|
|
|
| 184 |
"""Converts an SDXL checkpoint to Diffusers format and saves it.
|
|
|
|
| 185 |
Args:
|
| 186 |
checkpoint_path_or_url: The path/URL/repo ID of the checkpoint.
|
| 187 |
"""
|
|
@@ -189,21 +236,31 @@ def convert_and_save_sdxl_to_diffusers(checkpoint_path_or_url, output_path, refe
|
|
| 189 |
# Download the model if necessary (handles URLs, repo IDs, and local paths)
|
| 190 |
checkpoint_path = download_model(checkpoint_path_or_url)
|
| 191 |
|
| 192 |
-
text_encoder1_state, text_encoder2_state, vae_state, unet_state =
|
| 193 |
-
|
| 194 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
|
| 196 |
# Load tokenizer and scheduler from the reference model
|
| 197 |
-
pipeline = StableDiffusionXLPipeline.from_pretrained(
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
|
|
|
|
|
|
| 203 |
pipeline.to("cpu")
|
| 204 |
pipeline.save_pretrained(output_path)
|
| 205 |
print(f"Model saved as Diffusers format: {output_path}")
|
| 206 |
|
|
|
|
| 207 |
# ---------------------- UPLOAD FUNCTION ----------------------
|
| 208 |
def upload_to_huggingface(model_path, hf_token, orgs_name, model_name, make_private):
|
| 209 |
"""Uploads a model to the Hugging Face Hub."""
|
|
@@ -214,6 +271,7 @@ def upload_to_huggingface(model_path, hf_token, orgs_name, model_name, make_priv
|
|
| 214 |
api.upload_folder(folder_path=model_path, repo_id=model_repo)
|
| 215 |
print(f"Model uploaded to: https://huggingface.co/{model_repo}")
|
| 216 |
|
|
|
|
| 217 |
# ---------------------- GRADIO INTERFACE ----------------------
|
| 218 |
def main(model_to_load, reference_model, output_path, hf_token, orgs_name, model_name, make_private):
|
| 219 |
"""Main function: SDXL checkpoint to Diffusers, always fp16."""
|
|
@@ -223,21 +281,91 @@ def main(model_to_load, reference_model, output_path, hf_token, orgs_name, model
|
|
| 223 |
upload_to_huggingface(output_path, hf_token, orgs_name, model_name, make_private)
|
| 224 |
return "Conversion and upload completed successfully!"
|
| 225 |
except Exception as e:
|
| 226 |
-
return f"An error occurred: {e}"
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
|
| 243 |
demo.launch()
|
|
|
|
| 13 |
from urllib.parse import urlparse, unquote
|
| 14 |
from pathlib import Path
|
| 15 |
import tempfile
|
| 16 |
+
# from tqdm import tqdm # Removed: not crucial and can break display in gradio.
|
| 17 |
import psutil
|
| 18 |
import math
|
| 19 |
import shutil
|
| 20 |
import hashlib
|
| 21 |
from datetime import datetime
|
| 22 |
from typing import Dict, List, Optional
|
| 23 |
+
from huggingface_hub import login, HfApi, hf_hub_download, get_from_cache # Corrected import
|
| 24 |
from huggingface_hub.utils import validate_repo_id, HFValidationError
|
| 25 |
from huggingface_hub.errors import HfHubHTTPError
|
| 26 |
+
from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE # Import HUGGINGFACE_HUB_CACHE
|
| 27 |
+
|
|
|
|
| 28 |
|
| 29 |
# ---------------------- DEPENDENCIES ----------------------
|
| 30 |
def install_dependencies_gradio():
|
| 31 |
"""Installs the necessary dependencies."""
|
| 32 |
try:
|
| 33 |
+
subprocess.run(
|
| 34 |
+
[
|
| 35 |
+
"pip",
|
| 36 |
+
"install",
|
| 37 |
+
"-U",
|
| 38 |
+
"torch",
|
| 39 |
+
"diffusers",
|
| 40 |
+
"transformers",
|
| 41 |
+
"accelerate",
|
| 42 |
+
"safetensors",
|
| 43 |
+
"huggingface_hub",
|
| 44 |
+
"xformers",
|
| 45 |
+
]
|
| 46 |
+
)
|
| 47 |
print("Dependencies installed successfully.")
|
| 48 |
except Exception as e:
|
| 49 |
print(f"Error installing dependencies: {e}")
|
| 50 |
|
| 51 |
+
|
| 52 |
# ---------------------- UTILITY FUNCTIONS ----------------------
|
| 53 |
|
| 54 |
+
|
| 55 |
def increment_filename(filename):
|
| 56 |
"""Increments the filename to avoid overwriting existing files."""
|
| 57 |
base, ext = os.path.splitext(filename)
|
|
|
|
| 61 |
counter += 1
|
| 62 |
return filename
|
| 63 |
|
| 64 |
+
|
| 65 |
# ---------------------- UPLOAD FUNCTION ----------------------
|
| 66 |
def create_model_repo(api, user, orgs_name, model_name, make_private=False):
|
| 67 |
"""Creates a Hugging Face model repository."""
|
| 68 |
+
repo_id = (
|
| 69 |
+
f"{orgs_name}/{model_name.strip()}"
|
| 70 |
+
if orgs_name
|
| 71 |
+
else f"{user['name']}/{model_name.strip()}"
|
| 72 |
+
)
|
| 73 |
try:
|
| 74 |
api.create_repo(repo_id=repo_id, repo_type="model", private=make_private)
|
| 75 |
print(f"Model repo '{repo_id}' created.")
|
|
|
|
| 77 |
print(f"Model repo '{repo_id}' already exists.")
|
| 78 |
return repo_id
|
| 79 |
|
| 80 |
+
|
| 81 |
# ---------------------- MODEL LOADING AND CONVERSION ----------------------
|
| 82 |
def download_model(model_path_or_url):
|
| 83 |
"""Downloads a model, handling URLs, HF repos, and local paths, caching appropriately."""
|
|
|
|
| 85 |
# 1. Check if it's a valid Hugging Face repo ID (and potentially a file within)
|
| 86 |
try:
|
| 87 |
validate_repo_id(model_path_or_url)
|
| 88 |
+
# It's a valid repo ID; use hf_hub_download (it handles caching)
|
| 89 |
local_path = hf_hub_download(repo_id=model_path_or_url)
|
| 90 |
return local_path
|
| 91 |
except HFValidationError:
|
| 92 |
pass # Not a simple repo ID. Might be repo ID + filename, or a URL.
|
| 93 |
|
| 94 |
# 2. Check if it's a URL
|
| 95 |
+
if model_path_or_url.startswith("http://") or model_path_or_url.startswith(
|
| 96 |
+
"https://"
|
| 97 |
+
):
|
| 98 |
# Check if it's already in the cache
|
| 99 |
cache_path = get_from_cache(model_path_or_url) # Use get_from_cache
|
| 100 |
if cache_path is not None:
|
|
|
|
| 108 |
parsed_url = urlparse(model_path_or_url)
|
| 109 |
filename = os.path.basename(unquote(parsed_url.path))
|
| 110 |
if not filename:
|
| 111 |
+
filename = hashlib.sha256(model_path_or_url.encode()).hexdigest()
|
| 112 |
|
| 113 |
+
# Construct the cache path (using HF_HUB_CACHE + "downloads")
|
| 114 |
cache_dir = os.path.join(HUGGINGFACE_HUB_CACHE, "downloads")
|
| 115 |
+
os.makedirs(cache_dir, exist_ok=True) # Ensure cache directory exists
|
| 116 |
local_path = os.path.join(cache_dir, filename)
|
| 117 |
|
| 118 |
with open(local_path, "wb") as f:
|
|
|
|
| 134 |
local_path = hf_hub_download(repo_id=repo_id, filename=filename)
|
| 135 |
return local_path
|
| 136 |
else:
|
| 137 |
+
raise ValueError("Invalid input format.")
|
| 138 |
|
| 139 |
except HFValidationError:
|
| 140 |
raise ValueError(f"Invalid model path or URL: {model_path_or_url}")
|
|
|
|
| 147 |
"""Loads an SDXL checkpoint (.ckpt or .safetensors) and returns components."""
|
| 148 |
|
| 149 |
if checkpoint_path.endswith(".safetensors"):
|
| 150 |
+
state_dict = load_file(checkpoint_path, device="cpu") # Load to CPU
|
| 151 |
elif checkpoint_path.endswith(".ckpt"):
|
| 152 |
+
state_dict = torch.load(checkpoint_path, map_location="cpu")[
|
| 153 |
+
"state_dict"
|
| 154 |
+
] # Load to CPU, access ["state_dict"]
|
| 155 |
else:
|
| 156 |
raise ValueError("Unsupported checkpoint format. Must be .safetensors or .ckpt")
|
| 157 |
|
|
|
|
| 162 |
|
| 163 |
for key, value in state_dict.items():
|
| 164 |
if key.startswith("first_stage_model."): # VAE
|
| 165 |
+
vae_state[key.replace("first_stage_model.", "")] = value.to(
|
| 166 |
+
torch.float16
|
| 167 |
+
) # FP16 conversion
|
| 168 |
elif key.startswith("condition_model.model.text_encoder."): # Text Encoder 1
|
| 169 |
+
text_encoder1_state[
|
| 170 |
+
key.replace("condition_model.model.text_encoder.", "")
|
| 171 |
+
] = value.to(
|
| 172 |
+
torch.float16
|
| 173 |
+
) # FP16
|
| 174 |
+
elif key.startswith(
|
| 175 |
+
"condition_model.model.text_encoder_2."
|
| 176 |
+
): # Text Encoder 2
|
| 177 |
+
text_encoder2_state[
|
| 178 |
+
key.replace("condition_model.model.text_encoder_2.", "")
|
| 179 |
+
] = value.to(
|
| 180 |
+
torch.float16
|
| 181 |
+
) # FP16
|
| 182 |
elif key.startswith("model.diffusion_model."): # UNet
|
| 183 |
+
unet_state[key.replace("model.diffusion_model.", "")] = value.to(
|
| 184 |
+
torch.float16
|
| 185 |
+
) # FP16
|
| 186 |
|
| 187 |
return text_encoder1_state, text_encoder2_state, vae_state, unet_state
|
| 188 |
|
| 189 |
+
|
| 190 |
+
def build_diffusers_model(
|
| 191 |
+
text_encoder1_state, text_encoder2_state, vae_state, unet_state, reference_model_path=None
|
| 192 |
+
):
|
| 193 |
"""Builds the Diffusers pipeline components from the loaded state dicts."""
|
| 194 |
|
| 195 |
# Default to SDXL base 1.0 if no reference model is provided
|
|
|
|
| 197 |
reference_model_path = "stabilityai/stable-diffusion-xl-base-1.0"
|
| 198 |
|
| 199 |
# 1. Text Encoders
|
| 200 |
+
config_text_encoder1 = CLIPTextConfig.from_pretrained(
|
| 201 |
+
reference_model_path, subfolder="text_encoder"
|
| 202 |
+
)
|
| 203 |
+
config_text_encoder2 = CLIPTextConfig.from_pretrained(
|
| 204 |
+
reference_model_path, subfolder="text_encoder_2"
|
| 205 |
+
)
|
| 206 |
|
| 207 |
text_encoder1 = CLIPTextModel(config_text_encoder1)
|
| 208 |
text_encoder2 = CLIPTextModel(config_text_encoder2)
|
|
|
|
| 224 |
return text_encoder1, text_encoder2, vae, unet
|
| 225 |
|
| 226 |
|
| 227 |
+
def convert_and_save_sdxl_to_diffusers(
|
| 228 |
+
checkpoint_path_or_url, output_path, reference_model_path
|
| 229 |
+
):
|
| 230 |
"""Converts an SDXL checkpoint to Diffusers format and saves it.
|
| 231 |
+
|
| 232 |
Args:
|
| 233 |
checkpoint_path_or_url: The path/URL/repo ID of the checkpoint.
|
| 234 |
"""
|
|
|
|
| 236 |
# Download the model if necessary (handles URLs, repo IDs, and local paths)
|
| 237 |
checkpoint_path = download_model(checkpoint_path_or_url)
|
| 238 |
|
| 239 |
+
text_encoder1_state, text_encoder2_state, vae_state, unet_state = (
|
| 240 |
+
load_sdxl_checkpoint(checkpoint_path)
|
| 241 |
+
)
|
| 242 |
+
text_encoder1, text_encoder2, vae, unet = build_diffusers_model(
|
| 243 |
+
text_encoder1_state,
|
| 244 |
+
text_encoder2_state,
|
| 245 |
+
vae_state,
|
| 246 |
+
unet_state,
|
| 247 |
+
reference_model_path,
|
| 248 |
+
)
|
| 249 |
|
| 250 |
# Load tokenizer and scheduler from the reference model
|
| 251 |
+
pipeline = StableDiffusionXLPipeline.from_pretrained(
|
| 252 |
+
reference_model_path,
|
| 253 |
+
text_encoder=text_encoder1,
|
| 254 |
+
text_encoder_2=text_encoder2,
|
| 255 |
+
vae=vae,
|
| 256 |
+
unet=unet,
|
| 257 |
+
torch_dtype=torch.float16,
|
| 258 |
+
)
|
| 259 |
pipeline.to("cpu")
|
| 260 |
pipeline.save_pretrained(output_path)
|
| 261 |
print(f"Model saved as Diffusers format: {output_path}")
|
| 262 |
|
| 263 |
+
|
| 264 |
# ---------------------- UPLOAD FUNCTION ----------------------
|
| 265 |
def upload_to_huggingface(model_path, hf_token, orgs_name, model_name, make_private):
|
| 266 |
"""Uploads a model to the Hugging Face Hub."""
|
|
|
|
| 271 |
api.upload_folder(folder_path=model_path, repo_id=model_repo)
|
| 272 |
print(f"Model uploaded to: https://huggingface.co/{model_repo}")
|
| 273 |
|
| 274 |
+
|
| 275 |
# ---------------------- GRADIO INTERFACE ----------------------
|
| 276 |
def main(model_to_load, reference_model, output_path, hf_token, orgs_name, model_name, make_private):
|
| 277 |
"""Main function: SDXL checkpoint to Diffusers, always fp16."""
|
|
|
|
| 281 |
upload_to_huggingface(output_path, hf_token, orgs_name, model_name, make_private)
|
| 282 |
return "Conversion and upload completed successfully!"
|
| 283 |
except Exception as e:
|
| 284 |
+
return f"An error occurred: {e}" # Return the error message
|
| 285 |
+
|
| 286 |
+
|
| 287 |
+
css = """
|
| 288 |
+
#main-container {
|
| 289 |
+
display: flex;
|
| 290 |
+
flex-direction: column;
|
| 291 |
+
height: 100vh;
|
| 292 |
+
justify-content: space-between;
|
| 293 |
+
font-family: 'Arial', sans-serif;
|
| 294 |
+
font-size: 16px;
|
| 295 |
+
color: #333;
|
| 296 |
+
}
|
| 297 |
+
#convert-button {
|
| 298 |
+
margin-top: auto;
|
| 299 |
+
}
|
| 300 |
+
"""
|
| 301 |
+
|
| 302 |
+
with gr.Blocks(css=css) as demo:
|
| 303 |
+
gr.Markdown(
|
| 304 |
+
"""
|
| 305 |
+
# π¨ SDXL Model Converter
|
| 306 |
+
Convert SDXL checkpoints to Diffusers format (FP16, CPU-only).
|
| 307 |
+
|
| 308 |
+
### π₯ Input Sources Supported:
|
| 309 |
+
- Local model files (.safetensors, .ckpt)
|
| 310 |
+
- Direct URLs to model files
|
| 311 |
+
- Hugging Face model repositories (e.g., 'my-org/my-model' or 'my-org/my-model/file.safetensors')
|
| 312 |
+
|
| 313 |
+
### βΉοΈ Important Notes:
|
| 314 |
+
- This tool runs on **CPU**, conversion might be slower than on GPU.
|
| 315 |
+
- For Hugging Face uploads, you need a **WRITE** token (not a read token).
|
| 316 |
+
- Get your HF token here: [https://huggingface.co/settings/tokens](https://huggingface.co/settings/tokens)
|
| 317 |
+
|
| 318 |
+
### πΎ Memory Usage:
|
| 319 |
+
- This space is configured for **FP16** precision to reduce memory usage.
|
| 320 |
+
- Close other applications during conversion.
|
| 321 |
+
- For large models, ensure you have at least 16GB of RAM.
|
| 322 |
+
|
| 323 |
+
### π» Source Code:
|
| 324 |
+
- [GitHub Repository](https://github.com/Ktiseos-Nyx/Gradio-SDXL-Diffusers)
|
| 325 |
+
|
| 326 |
+
### π Support:
|
| 327 |
+
- If you're interested in funding more projects: [Ko-fi](https://ko-fi.com/duskfallcrew)
|
| 328 |
+
"""
|
| 329 |
+
)
|
| 330 |
+
|
| 331 |
+
with gr.Column(elem_id="main-container"): # Use a Column for layout
|
| 332 |
+
model_to_load = gr.Textbox(
|
| 333 |
+
label="SDXL Checkpoint (Path, URL, or HF Repo)",
|
| 334 |
+
placeholder="Path, URL, or Hugging Face Repo ID (e.g., my-org/my-model or my-org/my-model/file.safetensors)",
|
| 335 |
+
)
|
| 336 |
+
reference_model = gr.Textbox(
|
| 337 |
+
label="Reference Diffusers Model (Optional)",
|
| 338 |
+
placeholder="e.g., stabilityai/stable-diffusion-xl-base-1.0 (Leave blank for default)",
|
| 339 |
+
)
|
| 340 |
+
output_path = gr.Textbox(
|
| 341 |
+
label="Output Path (Diffusers Format)", value="output"
|
| 342 |
+
) # Default changed to "output"
|
| 343 |
+
hf_token = gr.Textbox(
|
| 344 |
+
label="Hugging Face Token", placeholder="Your Hugging Face write token"
|
| 345 |
+
)
|
| 346 |
+
orgs_name = gr.Textbox(
|
| 347 |
+
label="Organization Name (Optional)", placeholder="Your organization name"
|
| 348 |
+
)
|
| 349 |
+
model_name = gr.Textbox(
|
| 350 |
+
label="Model Name", placeholder="The name of your model on Hugging Face"
|
| 351 |
+
)
|
| 352 |
+
make_private = gr.Checkbox(label="Make Repository Private", value=False)
|
| 353 |
+
|
| 354 |
+
convert_button = gr.Button("Convert and Upload", elem_id="convert-button")
|
| 355 |
+
output = gr.Markdown()
|
| 356 |
+
|
| 357 |
+
convert_button.click(
|
| 358 |
+
fn=main,
|
| 359 |
+
inputs=[
|
| 360 |
+
model_to_load,
|
| 361 |
+
reference_model,
|
| 362 |
+
output_path,
|
| 363 |
+
hf_token,
|
| 364 |
+
orgs_name,
|
| 365 |
+
model_name,
|
| 366 |
+
make_private,
|
| 367 |
+
],
|
| 368 |
+
outputs=output,
|
| 369 |
+
)
|
| 370 |
|
| 371 |
demo.launch()
|