File size: 5,172 Bytes
bd2c5ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26d5d83
 
 
bd2c5ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6fb37f2
 
bd2c5ca
6fb37f2
 
 
bd2c5ca
 
6fb37f2
bd2c5ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26d5d83
 
bd2c5ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
"""
Model Setup Script for KYC POC

This script downloads and sets up the required ML models:
1. AuraFace - Face recognition model from HuggingFace
2. Silent-Face-Anti-Spoofing - Liveness detection models from GitHub

Run this script before starting the application:
    python setup_models.py
"""

import os
import sys
import shutil
import subprocess
from pathlib import Path


def setup_auraface():
    """Download AuraFace model from HuggingFace."""
    print("=" * 50)
    print("Setting up AuraFace model...")
    print("=" * 50)

    try:
        from huggingface_hub import snapshot_download

        # InsightFace expects models at {root}/models/{name}/
        # So we download to insightface_models/models/auraface/
        model_dir = Path("insightface_models/models/auraface")
        model_dir.mkdir(parents=True, exist_ok=True)

        print("Downloading AuraFace-v1 from HuggingFace...")
        snapshot_download(
            repo_id="fal/AuraFace-v1",
            local_dir=str(model_dir),
            local_dir_use_symlinks=False
        )
        print(f"AuraFace model downloaded to: {model_dir}")
        return True

    except ImportError:
        print("ERROR: huggingface_hub not installed. Run: pip install huggingface-hub")
        return False
    except Exception as e:
        print(f"ERROR downloading AuraFace: {e}")
        return False


def setup_silent_face_anti_spoofing():
    """Clone Silent-Face-Anti-Spoofing repository and copy models."""
    print("\n" + "=" * 50)
    print("Setting up Silent-Face-Anti-Spoofing...")
    print("=" * 50)

    repo_dir = Path("Silent-Face-Anti-Spoofing")
    models_dir = Path("models/anti_spoof")

    # Clone repository if not exists or is empty
    if not repo_dir.exists() or not any(repo_dir.iterdir()):
        print("Cloning Silent-Face-Anti-Spoofing repository...")
        # Remove empty directory if it exists
        if repo_dir.exists():
            shutil.rmtree(repo_dir)
        try:
            result = subprocess.run(
                ["git", "clone", "--depth", "1", "https://github.com/minivision-ai/Silent-Face-Anti-Spoofing.git"],
                capture_output=True,
                text=True
            )
            if result.returncode != 0:
                print(f"ERROR cloning repository: {result.stderr}")
                return False
            print("Repository cloned successfully.")
        except FileNotFoundError:
            print("ERROR: git not found. Please install git and try again.")
            return False
    else:
        print("Repository already exists, skipping clone.")

    # Copy model files
    models_dir.mkdir(parents=True, exist_ok=True)

    # Copy anti_spoof_models
    src_anti_spoof = repo_dir / "resources" / "anti_spoof_models"
    dst_anti_spoof = models_dir / "anti_spoof_models"

    if src_anti_spoof.exists():
        if dst_anti_spoof.exists():
            shutil.rmtree(dst_anti_spoof)
        shutil.copytree(src_anti_spoof, dst_anti_spoof)
        print(f"Copied anti_spoof_models to: {dst_anti_spoof}")
    else:
        print(f"WARNING: {src_anti_spoof} not found")

    # Copy detection_model
    src_detection = repo_dir / "resources" / "detection_model"
    dst_detection = models_dir / "detection_model"

    if src_detection.exists():
        if dst_detection.exists():
            shutil.rmtree(dst_detection)
        shutil.copytree(src_detection, dst_detection)
        print(f"Copied detection_model to: {dst_detection}")
    else:
        print(f"WARNING: {src_detection} not found")

    return True


def verify_models():
    """Verify all required model files exist."""
    print("\n" + "=" * 50)
    print("Verifying model files...")
    print("=" * 50)

    required_files = [
        # AuraFace models (InsightFace expects {root}/models/{name}/)
        "insightface_models/models/auraface",
        # Anti-spoofing models
        "models/anti_spoof/anti_spoof_models",
        "models/anti_spoof/detection_model",
    ]

    all_exist = True
    for file_path in required_files:
        path = Path(file_path)
        exists = path.exists()
        status = "OK" if exists else "MISSING"
        print(f"  [{status}] {file_path}")
        if not exists:
            all_exist = False

    return all_exist


def main():
    """Main setup function."""
    print("\n" + "#" * 60)
    print("# KYC POC - Model Setup")
    print("#" * 60 + "\n")

    # Change to script directory
    script_dir = Path(__file__).parent
    os.chdir(script_dir)

    success = True

    # Setup AuraFace
    if not setup_auraface():
        success = False

    # Setup Silent-Face-Anti-Spoofing
    if not setup_silent_face_anti_spoofing():
        success = False

    # Verify all models
    if not verify_models():
        success = False

    print("\n" + "#" * 60)
    if success:
        print("# Setup completed successfully!")
        print("# You can now run the application with: uvicorn app.main:app --reload")
    else:
        print("# Setup completed with errors. Please check the messages above.")
    print("#" * 60 + "\n")

    return 0 if success else 1


if __name__ == "__main__":
    sys.exit(main())