primerz commited on
Commit
a6caf7b
·
verified ·
1 Parent(s): cc13ccc

Update models.py

Browse files
Files changed (1) hide show
  1. models.py +39 -3
models.py CHANGED
@@ -4,6 +4,7 @@ FIXED VERSION with proper IP-Adapter and BLIP-2 support
4
  """
5
  import torch
6
  import time
 
7
  from diffusers import (
8
  StableDiffusionXLControlNetImg2ImgPipeline,
9
  ControlNetModel,
@@ -14,7 +15,7 @@ from diffusers.models.attention_processor import AttnProcessor2_0
14
  from transformers import CLIPVisionModelWithProjection
15
  from insightface.app import FaceAnalysis
16
  from controlnet_aux import ZoeDetector, OpenposeDetector, LeresDetector, MidasDetector, MediapipeFaceDetector
17
- from huggingface_hub import hf_hub_download
18
  from compel import Compel, ReturnedEmbeddingsType
19
 
20
  # Use reference implementation's attention processor
@@ -64,10 +65,44 @@ def download_model_with_retry(repo_id, filename, max_retries=None):
64
  def load_face_analysis():
65
  """Load face analysis model with proper error handling."""
66
  print("Loading face analysis model...")
 
 
 
 
 
67
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
  face_app = FaceAnalysis(
69
- name=FACE_DETECTION_CONFIG['model_name'],
70
- root='./models/insightface',
71
  providers=['CUDAExecutionProvider', 'CPUExecutionProvider']
72
  )
73
  face_app.prepare(
@@ -76,6 +111,7 @@ def load_face_analysis():
76
  )
77
  print(" [OK] Face analysis model loaded successfully")
78
  return face_app, True
 
79
  except Exception as e:
80
  print(f" [WARNING] Face detection not available: {e}")
81
  return None, False
 
4
  """
5
  import torch
6
  import time
7
+ import os
8
  from diffusers import (
9
  StableDiffusionXLControlNetImg2ImgPipeline,
10
  ControlNetModel,
 
15
  from transformers import CLIPVisionModelWithProjection
16
  from insightface.app import FaceAnalysis
17
  from controlnet_aux import ZoeDetector, OpenposeDetector, LeresDetector, MidasDetector, MediapipeFaceDetector
18
+ from huggingface_hub import hf_hub_download, snapshot_download
19
  from compel import Compel, ReturnedEmbeddingsType
20
 
21
  # Use reference implementation's attention processor
 
65
  def load_face_analysis():
66
  """Load face analysis model with proper error handling."""
67
  print("Loading face analysis model...")
68
+
69
+ model_name = FACE_DETECTION_CONFIG['model_name'] # "antelopev2"
70
+ local_model_root = '.' # We want files to be in ./antelopev2
71
+ local_model_path = os.path.join(local_model_root, model_name)
72
+
73
  try:
74
+ # --- NEW: Download models from HF Hub ---
75
+ print(f" Ensuring insightface models are downloaded to {local_model_path}...")
76
+
77
+ # Check if files exist before downloading
78
+ required_files = ["1k3d68.onnx", "2d106det.onnx", "genderage.onnx", "glintr100.onnx", "scrfd_10g_bnkps.onnx"]
79
+ files_exist = all(os.path.exists(os.path.join(local_model_path, f)) for f in required_files)
80
+
81
+ if not files_exist:
82
+ print(f" Models not found locally, downloading from {MODEL_REPO}...")
83
+ try:
84
+ snapshot_kwargs = {
85
+ "repo_id": MODEL_REPO,
86
+ "allow_patterns": [f"{model_name}/*.onnx"],
87
+ "local_dir": local_model_root,
88
+ "local_dir_use_symlinks": False,
89
+ "repo_type": "model"
90
+ }
91
+ if HUGGINGFACE_TOKEN:
92
+ snapshot_kwargs["token"] = HUGGINGFACE_TOKEN
93
+
94
+ snapshot_download(**snapshot_kwargs)
95
+ print(" [OK] Downloaded insightface models.")
96
+ except Exception as e:
97
+ print(f" [ERROR] Failed to download insightface models: {e}")
98
+ raise # Re-raise exception to be caught by the outer try-except
99
+ else:
100
+ print(" [OK] Insightface models found locally.")
101
+ # --- END NEW ---
102
+
103
  face_app = FaceAnalysis(
104
+ name=model_name,
105
+ root=local_model_root, # This will look in ./antelopev2
106
  providers=['CUDAExecutionProvider', 'CPUExecutionProvider']
107
  )
108
  face_app.prepare(
 
111
  )
112
  print(" [OK] Face analysis model loaded successfully")
113
  return face_app, True
114
+
115
  except Exception as e:
116
  print(f" [WARNING] Face detection not available: {e}")
117
  return None, False