File size: 1,084 Bytes
e340a84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from dataclasses import dataclass
from typing import Optional


@dataclass
class HFSpec:
    repo_id: str
    filename: str
    revision: Optional[str] = None
    local_dir: str = "checkpoints"


def _is_nonempty_str(x) -> bool:
    return isinstance(x, str) and len(x) > 0


def resolve_checkpoint_path(
    checkpoint: Optional[str], hf: Optional[dict]
) -> Optional[str]:
    if _is_nonempty_str(checkpoint):
        return checkpoint
    if not isinstance(hf, dict):
        return None

    repo_id = hf.get("repo_id")
    filename = hf.get("filename")
    revision = hf.get("revision", None)
    local_dir = hf.get("local_dir", "checkpoints")

    if not _is_nonempty_str(repo_id) or not _is_nonempty_str(filename):
        return None

    try:
        from huggingface_hub import hf_hub_download
    except Exception as e:
        raise RuntimeError("huggingface_hub is required for auto-download") from e

    os.makedirs(local_dir, exist_ok=True)
    return hf_hub_download(
        repo_id=repo_id, filename=filename, revision=revision, local_dir=local_dir
    )