Spaces:
Running
Running
Commit Β·
79d56d4
1
Parent(s): cddf7b4
Fix: use public mirror for inswapper model (gated HF repo fallback)
Browse files- processors/face_swap.py +57 -14
processors/face_swap.py
CHANGED
|
@@ -7,6 +7,7 @@ in the local `models/` directory.
|
|
| 7 |
"""
|
| 8 |
|
| 9 |
import os
|
|
|
|
| 10 |
import cv2
|
| 11 |
import numpy as np
|
| 12 |
import requests
|
|
@@ -17,24 +18,66 @@ MODELS_DIR = Path(__file__).parent.parent / "models"
|
|
| 17 |
MODELS_DIR.mkdir(exist_ok=True)
|
| 18 |
|
| 19 |
INSWAPPER_PATH = MODELS_DIR / "inswapper_128.onnx"
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
|
| 25 |
# ββ Helpers βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 26 |
|
| 27 |
-
def
|
| 28 |
-
"""
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
return
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
|
| 40 |
# ββ Main class ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
@@ -69,7 +112,7 @@ class FaceSwapper:
|
|
| 69 |
self._app.prepare(ctx_id=-1, det_size=(640, 640))
|
| 70 |
|
| 71 |
# inswapper model
|
| 72 |
-
|
| 73 |
self._swapper = insightface.model_zoo.get_model(
|
| 74 |
str(INSWAPPER_PATH),
|
| 75 |
providers=["CPUExecutionProvider"],
|
|
|
|
| 7 |
"""
|
| 8 |
|
| 9 |
import os
|
| 10 |
+
import shutil
|
| 11 |
import cv2
|
| 12 |
import numpy as np
|
| 13 |
import requests
|
|
|
|
| 18 |
MODELS_DIR.mkdir(exist_ok=True)
|
| 19 |
|
| 20 |
INSWAPPER_PATH = MODELS_DIR / "inswapper_128.onnx"
|
| 21 |
+
|
| 22 |
+
# Public mirrors β tried in order until one succeeds
|
| 23 |
+
_INSWAPPER_URLS = [
|
| 24 |
+
# Public HF mirror (no auth required)
|
| 25 |
+
"https://huggingface.co/ezioruan/inswapper_128.onnx/resolve/main/inswapper_128.onnx",
|
| 26 |
+
# Fallback mirror
|
| 27 |
+
"https://huggingface.co/theNeofr/inswapper/resolve/main/inswapper_128.onnx",
|
| 28 |
+
]
|
| 29 |
|
| 30 |
|
| 31 |
# ββ Helpers βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 32 |
|
| 33 |
+
def _download_inswapper() -> None:
|
| 34 |
+
"""Download inswapper_128.onnx.
|
| 35 |
+
|
| 36 |
+
Strategy:
|
| 37 |
+
1. huggingface_hub.hf_hub_download (uses HF_TOKEN env var automatically
|
| 38 |
+
on HF Spaces β works if user has accepted gated-model terms).
|
| 39 |
+
2. Plain HTTP fallback from public mirrors.
|
| 40 |
+
"""
|
| 41 |
+
if INSWAPPER_PATH.exists() and INSWAPPER_PATH.stat().st_size > 100_000:
|
| 42 |
+
return
|
| 43 |
+
|
| 44 |
+
# ββ Strategy 1: huggingface_hub ββββββββββββββββββββββββββββββββββββββββββ
|
| 45 |
+
try:
|
| 46 |
+
from huggingface_hub import hf_hub_download
|
| 47 |
+
print("[FaceSwapper] Downloading inswapper_128.onnx via HF Hub β¦")
|
| 48 |
+
cached = hf_hub_download(
|
| 49 |
+
repo_id="deepinsight/inswapper",
|
| 50 |
+
filename="inswapper_128.onnx",
|
| 51 |
+
token=os.environ.get("HF_TOKEN"),
|
| 52 |
+
)
|
| 53 |
+
shutil.copy(cached, INSWAPPER_PATH)
|
| 54 |
+
print(f"[FaceSwapper] Saved to {INSWAPPER_PATH}")
|
| 55 |
return
|
| 56 |
+
except Exception as e:
|
| 57 |
+
print(f"[FaceSwapper] HF Hub download failed ({e}), trying mirrors β¦")
|
| 58 |
+
|
| 59 |
+
# ββ Strategy 2: public mirrors βββββββββββββββββββββββββββββββββββββββββββ
|
| 60 |
+
for url in _INSWAPPER_URLS:
|
| 61 |
+
try:
|
| 62 |
+
print(f"[FaceSwapper] Trying {url} β¦")
|
| 63 |
+
resp = requests.get(url, stream=True, timeout=180)
|
| 64 |
+
resp.raise_for_status()
|
| 65 |
+
with open(INSWAPPER_PATH, "wb") as f:
|
| 66 |
+
for chunk in resp.iter_content(chunk_size=65536):
|
| 67 |
+
f.write(chunk)
|
| 68 |
+
if INSWAPPER_PATH.stat().st_size > 100_000:
|
| 69 |
+
print(f"[FaceSwapper] Saved to {INSWAPPER_PATH}")
|
| 70 |
+
return
|
| 71 |
+
INSWAPPER_PATH.unlink(missing_ok=True)
|
| 72 |
+
except Exception as e:
|
| 73 |
+
print(f"[FaceSwapper] Mirror failed ({e})")
|
| 74 |
+
INSWAPPER_PATH.unlink(missing_ok=True)
|
| 75 |
+
|
| 76 |
+
raise RuntimeError(
|
| 77 |
+
"Could not download inswapper_128.onnx. "
|
| 78 |
+
"Accept the model terms at https://huggingface.co/deepinsight/inswapper "
|
| 79 |
+
"then add your HF token as a Space secret named HF_TOKEN."
|
| 80 |
+
)
|
| 81 |
|
| 82 |
|
| 83 |
# ββ Main class ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 112 |
self._app.prepare(ctx_id=-1, det_size=(640, 640))
|
| 113 |
|
| 114 |
# inswapper model
|
| 115 |
+
_download_inswapper()
|
| 116 |
self._swapper = insightface.model_zoo.get_model(
|
| 117 |
str(INSWAPPER_PATH),
|
| 118 |
providers=["CPUExecutionProvider"],
|