Spaces:
Sleeping
Sleeping
Update backend/model_loader.py
Browse files- backend/model_loader.py +16 -4
backend/model_loader.py
CHANGED
|
@@ -1,16 +1,28 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
-
from huggingface_hub import snapshot_download
|
| 4 |
from civitai import download_civitai_model
|
| 5 |
|
| 6 |
def prepare_model(source: str, model_id: str, civitai_key: str = None) -> str:
|
| 7 |
if source == "hf":
|
| 8 |
-
#
|
|
|
|
|
|
|
| 9 |
try:
|
| 10 |
path = snapshot_download(model_id, cache_dir="/app/data/hf_cache")
|
| 11 |
-
return path
|
| 12 |
except Exception as e:
|
| 13 |
raise RuntimeError(f"Failed to download HF model {model_id}: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
elif source == "civitai":
|
| 15 |
if not civitai_key:
|
| 16 |
raise ValueError("Civitai API key required")
|
|
|
|
| 1 |
import os
|
| 2 |
+
import glob
|
| 3 |
+
from huggingface_hub import snapshot_download
|
| 4 |
from civitai import download_civitai_model
|
| 5 |
|
| 6 |
def prepare_model(source: str, model_id: str, civitai_key: str = None) -> str:
|
| 7 |
if source == "hf":
|
| 8 |
+
# 簡易バリデーション(念のため)
|
| 9 |
+
if model_id.startswith("http"):
|
| 10 |
+
raise ValueError("HuggingFace model_id should be 'repo_name' or 'namespace/repo_name', not a URL.")
|
| 11 |
try:
|
| 12 |
path = snapshot_download(model_id, cache_dir="/app/data/hf_cache")
|
|
|
|
| 13 |
except Exception as e:
|
| 14 |
raise RuntimeError(f"Failed to download HF model {model_id}: {e}")
|
| 15 |
+
|
| 16 |
+
# safetensorsが存在するかチェック(GGUFなどのみのリポジトリ対策)
|
| 17 |
+
safetensors_files = glob.glob(os.path.join(path, "**", "*.safetensors"), recursive=True)
|
| 18 |
+
bin_files = glob.glob(os.path.join(path, "**", "*.bin"), recursive=True) # pytorch_model.bin も一応許容
|
| 19 |
+
if not safetensors_files and not bin_files:
|
| 20 |
+
raise RuntimeError(
|
| 21 |
+
f"No safetensors or pytorch_model.bin files found in {model_id}. "
|
| 22 |
+
"This repository may only contain GGUF or other non-mergeable formats."
|
| 23 |
+
)
|
| 24 |
+
return path
|
| 25 |
+
|
| 26 |
elif source == "civitai":
|
| 27 |
if not civitai_key:
|
| 28 |
raise ValueError("Civitai API key required")
|