File size: 1,506 Bytes
9a9309b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Fetch only public DINOv2 tensor/config assets into a separate local directory."""
import json
from urllib.request import urlopen
from huggingface_hub import HfApi,snapshot_download
from backend.config import ROOT
from backend.vision import sha256

if __name__=='__main__':
    destination=ROOT/'models/encoder_dinov2'
    destination.mkdir(parents=True,exist_ok=True)
    license_path=destination/'LICENSE'
    if not license_path.exists():
        with urlopen('https://raw.githubusercontent.com/facebookresearch/dinov2/main/LICENSE',timeout=30) as response:
            license_path.write_bytes(response.read())
    receipt=destination/'ASSET_RECEIPT.json'
    if receipt.exists():
        saved=json.loads(receipt.read_text())
        assert sha256(destination/'model.safetensors')==saved['model_sha256']
        print('Preserved existing verified DINOv2 asset.');raise SystemExit(0)
    info=HfApi().model_info('facebook/dinov2-small')
    snapshot_download('facebook/dinov2-small',revision=info.sha,local_dir=destination,
        cache_dir=ROOT/'runtime/model-download-cache',
        allow_patterns=['config.json','model.safetensors','preprocessor_config.json','README.md','LICENSE'])
    receipt.write_text(json.dumps({'repo':'facebook/dinov2-small','revision':info.sha,
        'model_sha256':sha256(destination/'model.safetensors'),'license':'Apache-2.0',
        'source':'https://huggingface.co/facebook/dinov2-small','remote_code':False},indent=2))
    print(receipt.read_text(),flush=True)