File size: 1,704 Bytes
0e39d80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""One-time helper to upload YOLO weights to a Hugging Face Hub model repo.

Usage (PowerShell):
    $env:HF_TOKEN="hf_xxx"            # a write token from https://huggingface.co/settings/tokens
    $env:HF_REPO_ID="<user>/docverify-weights"
    .\.venv\Scripts\python.exe scripts\upload_weights.py

The backend later downloads these files at startup via huggingface_hub.hf_hub_download
when the local weight paths are not present (e.g. on Render).
"""
import os
import sys
from pathlib import Path

BACKEND = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(BACKEND))

from config import AADHAAR_WEIGHT_FILE, AADHAAR_WEIGHTS, PAN_WEIGHT_FILE, PAN_WEIGHTS  # noqa: E402


def main() -> None:
    repo_id = os.getenv("HF_REPO_ID")
    token = os.getenv("HF_TOKEN")
    if not repo_id or not token:
        sys.exit("Set HF_REPO_ID and HF_TOKEN environment variables first.")

    from huggingface_hub import HfApi, create_repo

    uploads = [
        (Path(AADHAAR_WEIGHTS), AADHAAR_WEIGHT_FILE),
        (Path(PAN_WEIGHTS), PAN_WEIGHT_FILE),
    ]
    missing = [str(src) for src, _ in uploads if not src.exists()]
    if missing:
        sys.exit(f"Local weight file(s) not found: {missing}")

    create_repo(repo_id, repo_type="model", token=token, exist_ok=True, private=False)
    api = HfApi(token=token)

    for src, dest_name in uploads:
        print(f"Uploading {src} -> {repo_id}/{dest_name}")
        api.upload_file(
            path_or_fileobj=str(src),
            path_in_repo=dest_name,
            repo_id=repo_id,
            repo_type="model",
        )

    print(f"Done. Weights available at https://huggingface.co/{repo_id}")


if __name__ == "__main__":
    main()