Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,7 +8,7 @@ from collections import OrderedDict
|
|
| 8 |
import re
|
| 9 |
import json
|
| 10 |
import gdown
|
| 11 |
-
import requests
|
| 12 |
import subprocess
|
| 13 |
from urllib.parse import urlparse, unquote
|
| 14 |
from pathlib import Path
|
|
@@ -21,7 +21,8 @@ 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.
|
|
|
|
| 25 |
from huggingface_hub.errors import HfHubHTTPError
|
| 26 |
from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE # Import HUGGINGFACE_HUB_CACHE
|
| 27 |
|
|
@@ -80,52 +81,16 @@ def create_model_repo(api, user, orgs_name, model_name, make_private=False):
|
|
| 80 |
|
| 81 |
# ---------------------- MODEL LOADING AND CONVERSION ----------------------
|
| 82 |
def download_model(model_path_or_url):
|
| 83 |
-
"""Downloads a model
|
| 84 |
try:
|
| 85 |
-
#
|
| 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 |
-
|
| 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:
|
| 101 |
-
return cache_path
|
| 102 |
-
|
| 103 |
-
# It's a URL and not in cache: download manually and put into HF cache
|
| 104 |
-
response = requests.get(model_path_or_url, stream=True)
|
| 105 |
-
response.raise_for_status() # Raise HTTPError for bad requests (4xx or 5xx)
|
| 106 |
-
|
| 107 |
-
# Get filename from URL, or use a hash if we can't determine it
|
| 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:
|
| 119 |
-
for chunk in response.iter_content(chunk_size=8192):
|
| 120 |
-
f.write(chunk)
|
| 121 |
-
return local_path
|
| 122 |
-
|
| 123 |
-
# 3. Check if it's a local file
|
| 124 |
-
elif os.path.isfile(model_path_or_url):
|
| 125 |
-
return model_path_or_url
|
| 126 |
-
|
| 127 |
-
# 4. Handle Hugging Face repo with a specific file
|
| 128 |
-
else:
|
| 129 |
try:
|
| 130 |
parts = model_path_or_url.split("/", 1)
|
| 131 |
if len(parts) == 2:
|
|
@@ -134,13 +99,12 @@ def download_model(model_path_or_url):
|
|
| 134 |
local_path = hf_hub_download(repo_id=repo_id, filename=filename)
|
| 135 |
return local_path
|
| 136 |
else:
|
| 137 |
-
raise ValueError("Invalid
|
| 138 |
-
|
| 139 |
except HFValidationError:
|
| 140 |
-
raise ValueError(f"Invalid
|
| 141 |
|
| 142 |
except Exception as e:
|
| 143 |
-
raise ValueError(f"Error downloading
|
| 144 |
|
| 145 |
|
| 146 |
def load_sdxl_checkpoint(checkpoint_path):
|
|
@@ -306,9 +270,8 @@ with gr.Blocks(css=css) as demo:
|
|
| 306 |
Convert SDXL checkpoints to Diffusers format (FP16, CPU-only).
|
| 307 |
|
| 308 |
### 📥 Input Sources Supported:
|
| 309 |
-
-
|
| 310 |
-
-
|
| 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.
|
|
@@ -319,6 +282,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 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)
|
|
@@ -330,8 +294,8 @@ with gr.Blocks(css=css) as demo:
|
|
| 330 |
|
| 331 |
with gr.Column(elem_id="main-container"): # Use a Column for layout
|
| 332 |
model_to_load = gr.Textbox(
|
| 333 |
-
label="SDXL Checkpoint (
|
| 334 |
-
placeholder="
|
| 335 |
)
|
| 336 |
reference_model = gr.Textbox(
|
| 337 |
label="Reference Diffusers Model (Optional)",
|
|
|
|
| 8 |
import re
|
| 9 |
import json
|
| 10 |
import gdown
|
| 11 |
+
#import requests # Removed
|
| 12 |
import subprocess
|
| 13 |
from urllib.parse import urlparse, unquote
|
| 14 |
from pathlib import Path
|
|
|
|
| 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.file_download import get_from_cache # Corrected import
|
| 25 |
+
from huggingface_hub.utils import validate_repo_id, HFValidationError
|
| 26 |
from huggingface_hub.errors import HfHubHTTPError
|
| 27 |
from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE # Import HUGGINGFACE_HUB_CACHE
|
| 28 |
|
|
|
|
| 81 |
|
| 82 |
# ---------------------- MODEL LOADING AND CONVERSION ----------------------
|
| 83 |
def download_model(model_path_or_url):
|
| 84 |
+
"""Downloads a model from a Hugging Face Hub repository."""
|
| 85 |
try:
|
| 86 |
+
# Check if it's a valid Hugging Face repo ID (and potentially a file within)
|
| 87 |
try:
|
| 88 |
validate_repo_id(model_path_or_url)
|
| 89 |
# It's a valid repo ID; use hf_hub_download (it handles caching)
|
| 90 |
local_path = hf_hub_download(repo_id=model_path_or_url)
|
| 91 |
return local_path
|
| 92 |
except HFValidationError:
|
| 93 |
+
# Might be a repo ID + filename
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
try:
|
| 95 |
parts = model_path_or_url.split("/", 1)
|
| 96 |
if len(parts) == 2:
|
|
|
|
| 99 |
local_path = hf_hub_download(repo_id=repo_id, filename=filename)
|
| 100 |
return local_path
|
| 101 |
else:
|
| 102 |
+
raise ValueError("Invalid Hugging Face repository format.")
|
|
|
|
| 103 |
except HFValidationError:
|
| 104 |
+
raise ValueError(f"Invalid Hugging Face repository ID or path: {model_path_or_url}")
|
| 105 |
|
| 106 |
except Exception as e:
|
| 107 |
+
raise ValueError(f"Error downloading model: {e}")
|
| 108 |
|
| 109 |
|
| 110 |
def load_sdxl_checkpoint(checkpoint_path):
|
|
|
|
| 270 |
Convert SDXL checkpoints to Diffusers format (FP16, CPU-only).
|
| 271 |
|
| 272 |
### 📥 Input Sources Supported:
|
| 273 |
+
- Only Hugging Face model repositories (e.g., 'my-org/my-model' or 'my-org/my-model/file.safetensors')
|
| 274 |
+
- Non HF repository Gradio edition will eventually pop up on the Ktiseos Nyx github.
|
|
|
|
| 275 |
|
| 276 |
### ℹ️ Important Notes:
|
| 277 |
- This tool runs on **CPU**, conversion might be slower than on GPU.
|
|
|
|
| 282 |
- This space is configured for **FP16** precision to reduce memory usage.
|
| 283 |
- Close other applications during conversion.
|
| 284 |
- For large models, ensure you have at least 16GB of RAM.
|
| 285 |
+
|
| 286 |
|
| 287 |
### 💻 Source Code:
|
| 288 |
- [GitHub Repository](https://github.com/Ktiseos-Nyx/Gradio-SDXL-Diffusers)
|
|
|
|
| 294 |
|
| 295 |
with gr.Column(elem_id="main-container"): # Use a Column for layout
|
| 296 |
model_to_load = gr.Textbox(
|
| 297 |
+
label="SDXL Checkpoint (HF Repo)", # More specific label
|
| 298 |
+
placeholder="Hugging Face Repo ID (e.g., my-org/my-model or my-org/my-model/file.safetensors)",
|
| 299 |
)
|
| 300 |
reference_model = gr.Textbox(
|
| 301 |
label="Reference Diffusers Model (Optional)",
|