Spaces:
Paused
Paused
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 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
print("π
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
|