crash10155 commited on
Commit
761cb3c
Β·
verified Β·
1 Parent(s): e977387

Update SwitcherAI/processors/frame/modules/face_swapper.py

Browse files
SwitcherAI/processors/frame/modules/face_swapper.py CHANGED
@@ -79,43 +79,67 @@ def get_frame_processor() -> Any:
79
 
80
  with THREAD_LOCK:
81
  if FRAME_PROCESSOR is None:
82
- try:
83
- config = get_current_model_config()
84
- model_path = resolve_relative_path(config['path'])
85
-
86
- # Convert to Path object if it's a string for validation
87
- if isinstance(model_path, str):
88
- model_path_obj = Path(model_path)
89
- else:
90
- model_path_obj = model_path
91
-
92
- # Check if model exists
93
- if not model_path_obj.exists():
94
- print(f"⚠️ Face swap model not found at: {model_path_obj}")
95
- print("πŸ”„ Attempting to download model...")
96
- if not pre_check():
97
- print("❌ Failed to download face swap model")
98
- return None
99
-
100
- # Verify model file size
101
- if model_path_obj.stat().st_size < 1024: # Less than 1KB indicates corruption
102
- print(f"⚠️ Face swap model appears corrupted: {model_path_obj}")
103
- print("πŸ”„ Attempting to re-download model...")
104
- model_path_obj.unlink() # Delete corrupted file
105
- if not pre_check():
106
- print("❌ Failed to re-download face swap model")
107
- return None
108
-
109
- # Load model with string path (insightface expects string)
110
- FRAME_PROCESSOR = insightface.model_zoo.get_model(
111
- str(model_path_obj),
112
- providers=SwitcherAI.globals.execution_providers
113
- )
114
- print("βœ… Face swap processor initialized")
115
-
116
- except Exception as e:
117
- print(f"⚠️ Failed to initialize face swap processor: {e}")
118
- FRAME_PROCESSOR = None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
 
120
  return FRAME_PROCESSOR
121
 
 
79
 
80
  with THREAD_LOCK:
81
  if FRAME_PROCESSOR is None:
82
+ # Try models in order of preference
83
+ model_priority = ['inswapper_128_fp16', 'inswapper_128', 'simswap_256']
84
+
85
+ # If user set a specific model, try it first
86
+ current_model = getattr(SwitcherAI.globals, 'face_swapper_model', None)
87
+ if current_model and current_model in MODEL_CONFIGS:
88
+ model_priority.insert(0, current_model)
89
+ # Remove duplicate if it exists later in the list
90
+ if current_model in model_priority[1:]:
91
+ model_priority.remove(current_model)
92
+
93
+ for model_name in model_priority:
94
+ try:
95
+ print(f"πŸ”„ Trying to load face swap model: {model_name}")
96
+
97
+ # Update config to current model being tested
98
+ temp_config = MODEL_CONFIGS[model_name]
99
+ model_path = resolve_relative_path(temp_config['path'])
100
+
101
+ # Convert to Path object if it's a string for validation
102
+ if isinstance(model_path, str):
103
+ model_path_obj = Path(model_path)
104
+ else:
105
+ model_path_obj = model_path
106
+
107
+ # Check if model exists, download if needed
108
+ if not model_path_obj.exists():
109
+ print(f"πŸ“₯ Downloading {model_name}...")
110
+ download_directory_path = resolve_relative_path('../.assets/models')
111
+ if isinstance(download_directory_path, str):
112
+ download_directory_path = Path(download_directory_path)
113
+ download_directory_path.mkdir(parents=True, exist_ok=True)
114
+
115
+ download_urls = [temp_config['url']]
116
+ if temp_config.get('requires_converter', False):
117
+ download_urls.append(temp_config['converter_url'])
118
+
119
+ conditional_download(str(download_directory_path), download_urls)
120
+
121
+ # Verify model file size
122
+ if model_path_obj.stat().st_size < 1024: # Less than 1KB indicates corruption
123
+ print(f"⚠️ {model_name} appears corrupted, skipping...")
124
+ continue
125
+
126
+ # Try to load the model
127
+ FRAME_PROCESSOR = insightface.model_zoo.get_model(
128
+ str(model_path_obj),
129
+ providers=SwitcherAI.globals.execution_providers
130
+ )
131
+
132
+ # If successful, update the global setting and break
133
+ SwitcherAI.globals.face_swapper_model = model_name
134
+ print(f"βœ… Successfully loaded face swap model: {model_name}")
135
+ break
136
+
137
+ except Exception as e:
138
+ print(f"❌ Failed to load {model_name}: {e}")
139
+ continue
140
+
141
+ if FRAME_PROCESSOR is None:
142
+ print("❌ All face swap models failed to load")
143
 
144
  return FRAME_PROCESSOR
145