Create model_loader.py
Browse files- backend/model_loader.py +19 -0
backend/model_loader.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import shutil
|
| 3 |
+
from huggingface_hub import snapshot_download, HfApi
|
| 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 |
+
# Use cache, but ensure safetensors available
|
| 9 |
+
try:
|
| 10 |
+
path = snapshot_download(model_id, cache_dir="/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")
|
| 17 |
+
return download_civitai_model(model_id, civitai_key)
|
| 18 |
+
else:
|
| 19 |
+
raise ValueError(f"Unknown source: {source}")
|