File size: 3,009 Bytes
a02f72f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
DeepGuard — Model Download Script
Downloads the ViT-based deepfake detection ONNX model from Hugging Face Hub.

Usage:
    python download_model.py

Model: onnx-community/Deep-Fake-Detector-v2-Model-ONNX
  - Architecture: google/vit-base-patch16-224-in21k (fine-tuned)
  - Task:         Binary classification — Realism vs. Deepfake
  - Labels:       {0: "Realism", 1: "Deepfake"}
"""

import os
import sys
import hashlib

MODELS_DIR = os.path.join(os.path.dirname(__file__), "models")
MODEL_DEST = os.path.join(MODELS_DIR, "deepfake_vit.onnx")

REPO_ID = "onnx-community/Deep-Fake-Detector-v2-Model-ONNX"
FILENAME = "onnx/model.onnx"  # Path inside the HF repo


def sha256(path: str) -> str:
    h = hashlib.sha256()
    with open(path, "rb") as f:
        for chunk in iter(lambda: f.read(65536), b""):
            h.update(chunk)
    return h.hexdigest()


def download():
    os.makedirs(MODELS_DIR, exist_ok=True)

    if os.path.exists(MODEL_DEST):
        size_mb = os.path.getsize(MODEL_DEST) / (1024 * 1024)
        print(f"[DeepGuard] Model already exists ({size_mb:.1f} MB): {MODEL_DEST}")
        print(f"[DeepGuard] SHA-256: {sha256(MODEL_DEST)}")
        print("[DeepGuard] Delete the file and re-run this script to force re-download.")
        return

    print(f"[DeepGuard] Downloading model from Hugging Face Hub...")
    print(f"  Repo:    {REPO_ID}")
    print(f"  File:    {FILENAME}")
    print(f"  Target:  {MODEL_DEST}")
    print()

    try:
        from huggingface_hub import hf_hub_download

        tmp_path = hf_hub_download(
            repo_id=REPO_ID,
            filename=FILENAME,
            cache_dir=MODELS_DIR,
            local_dir=MODELS_DIR,
            local_dir_use_symlinks=False,
        )

        # hf_hub_download writes to local_dir/<filename>
        # Move if needed
        expected_local = os.path.join(MODELS_DIR, "onnx", "model.onnx")
        if os.path.exists(expected_local) and not os.path.exists(MODEL_DEST):
            import shutil
            shutil.move(expected_local, MODEL_DEST)

        if not os.path.exists(MODEL_DEST):
            # Try symlink/copy from tmp_path
            import shutil
            shutil.copy2(tmp_path, MODEL_DEST)

        size_mb = os.path.getsize(MODEL_DEST) / (1024 * 1024)
        checksum = sha256(MODEL_DEST)
        print(f"\n[DeepGuard] [OK] Download complete!")
        print(f"  Size:    {size_mb:.1f} MB")
        print(f"  SHA-256: {checksum}")
        print(f"  Path:    {MODEL_DEST}")

    except ImportError:
        print("[ERROR] huggingface_hub not installed. Run: pip install huggingface_hub")
        sys.exit(1)
    except Exception as e:
        print(f"[ERROR] Download failed: {e}")
        print()
        print("Manual download instructions:")
        print(f"  1. Visit: https://huggingface.co/{REPO_ID}/tree/main/onnx")
        print(f"  2. Download 'model.onnx'")
        print(f"  3. Place it at: {MODEL_DEST}")
        sys.exit(1)


if __name__ == "__main__":
    download()