primerz commited on
Commit
e09a80f
·
verified ·
1 Parent(s): e15ddfd

Update models.py

Browse files
Files changed (1) hide show
  1. models.py +31 -41
models.py CHANGED
@@ -63,70 +63,60 @@ def download_model_with_retry(repo_id, filename, max_retries=None, **kwargs):
63
 
64
  def load_face_analysis():
65
  """
66
- Load face analysis model with proper error handling.
67
- This version pre-downloads models to the exact path
68
- insightface expects, to bypass its internal downloader.
69
  """
70
  print("Loading face analysis model...")
71
 
72
- # The library expects models to be at: [root]/[name]
73
- # We will set root = './models' and name = 'antelopev2'
74
- # The final path it will check is './models/antelopev2'
75
 
76
- local_model_root = './models'
77
- model_name = FACE_DETECTION_CONFIG['model_name'] # 'antelopev2'
78
-
79
- # This is the directory where the .onnx files must be:
80
- local_model_path = os.path.join(local_model_root, model_name) # ./models/antelopev2
81
 
82
  try:
83
- # --- NEW: Download logic based on user's snippet ---
84
- print(f" Ensuring insightface models are present in {local_model_path}...")
85
 
86
- # Check if models are already downloaded
87
- test_file_path = os.path.join(local_model_path, "scrfd_10g_bnkps.onnx")
88
 
89
- if not os.path.exists(test_file_path):
90
- print(f" Models not found locally, downloading from {MODEL_REPO}...")
91
-
92
- snapshot_kwargs = {
93
- "repo_id": MODEL_REPO,
94
- "repo_type": "model",
95
- "allow_patterns": f"{model_name}/*.onnx", # Download only files from antelopev2 folder
96
- "local_dir": local_model_root, # Download *into* ./models
97
- "local_dir_use_symlinks": False
98
- }
99
- if HUGGINGFACE_TOKEN:
100
- snapshot_kwargs["token"] = HUGGINGFACE_TOKEN
101
-
102
- # This will download files, preserving the repo folder structure,
103
- # creating ./models/antelopev2/[...].onnx
104
- snapshot_download(**snapshot_kwargs)
105
- print(" [OK] Downloaded insightface models.")
106
- else:
107
- print(" [OK] Insightface models found locally.")
108
- # --- END NEW ---
109
-
110
- # Now, initialize FaceAnalysis
111
- # It will look for models in root + name = ./models/antelopev2
112
  face_app = FaceAnalysis(
113
  name=model_name,
114
- root=local_model_root,
115
  providers=['CUDAExecutionProvider', 'CPUExecutionProvider']
116
  )
 
117
  face_app.prepare(
118
  ctx_id=FACE_DETECTION_CONFIG['ctx_id'],
119
  det_size=FACE_DETECTION_CONFIG['det_size']
120
  )
 
121
  print(" [OK] Face analysis model loaded successfully")
 
122
  return face_app, True
123
 
124
  except Exception as e:
125
- print(f" [WARNING] Face detection not available: {e}")
126
  import traceback
127
  traceback.print_exc()
128
  return None, False
129
-
130
 
131
  def load_depth_detector():
132
  """
 
63
 
64
  def load_face_analysis():
65
  """
66
+ Load face analysis model with proper model downloading from HuggingFace.
67
+ Downloads from DIAMONIK7777/antelopev2 which has the correct model structure.
 
68
  """
69
  print("Loading face analysis model...")
70
 
71
+ # Use a persistent location for models
72
+ local_model_root = '/data/models' if os.path.exists('/data') else './models'
73
+ model_name = 'antelopev2'
74
 
75
+ # This is the directory where the .onnx files must be
76
+ local_model_path = os.path.join(local_model_root, model_name)
 
 
 
77
 
78
  try:
79
+ # Download from DIAMONIK7777/antelopev2 repo (correct structure)
80
+ print(f" Downloading antelopev2 models to {local_model_path}...")
81
 
82
+ from huggingface_hub import snapshot_download
 
83
 
84
+ # Download entire repo
85
+ snapshot_kwargs = {
86
+ "repo_id": "DIAMONIK7777/antelopev2",
87
+ "local_dir": local_model_path,
88
+ "local_dir_use_symlinks": False,
89
+ "repo_type": "model"
90
+ }
91
+ if HUGGINGFACE_TOKEN:
92
+ snapshot_kwargs["token"] = HUGGINGFACE_TOKEN
93
+
94
+ antelope_download = snapshot_download(**snapshot_kwargs)
95
+ print(f" [OK] Models downloaded to: {antelope_download}")
96
+
97
+ # Initialize FaceAnalysis with correct paths
98
+ # Note: root should be the PARENT of the model directory
 
 
 
 
 
 
 
 
99
  face_app = FaceAnalysis(
100
  name=model_name,
101
+ root=local_model_root, # Parent directory
102
  providers=['CUDAExecutionProvider', 'CPUExecutionProvider']
103
  )
104
+
105
  face_app.prepare(
106
  ctx_id=FACE_DETECTION_CONFIG['ctx_id'],
107
  det_size=FACE_DETECTION_CONFIG['det_size']
108
  )
109
+
110
  print(" [OK] Face analysis model loaded successfully")
111
+ print(f" [OK] Using providers: {face_app.det_model.providers}")
112
  return face_app, True
113
 
114
  except Exception as e:
115
+ print(f" [ERROR] Face detection not available: {e}")
116
  import traceback
117
  traceback.print_exc()
118
  return None, False
119
+
120
 
121
  def load_depth_detector():
122
  """