devkunalnaik commited on
Commit
79d56d4
Β·
1 Parent(s): cddf7b4

Fix: use public mirror for inswapper model (gated HF repo fallback)

Browse files
Files changed (1) hide show
  1. 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
- INSWAPPER_URL = (
21
- "https://huggingface.co/deepinsight/inswapper/resolve/main/inswapper_128.onnx"
22
- )
 
 
 
 
 
23
 
24
 
25
  # ── Helpers ───────────────────────────────────────────────────────────────────
26
 
27
- def _download_file(url: str, dest: Path) -> None:
28
- """Stream-download a file, showing a simple progress indicator."""
29
- if dest.exists():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  return
31
- print(f"[FaceSwapper] Downloading {dest.name} …")
32
- response = requests.get(url, stream=True, timeout=120)
33
- response.raise_for_status()
34
- with open(dest, "wb") as f:
35
- for chunk in response.iter_content(chunk_size=65536):
36
- f.write(chunk)
37
- print(f"[FaceSwapper] Saved to {dest}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- _download_file(INSWAPPER_URL, INSWAPPER_PATH)
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"],