File size: 1,670 Bytes
9668975
787b0b5
759bf41
 
 
 
f25fee8
759bf41
 
 
9668975
 
 
 
 
 
 
 
 
759bf41
787b0b5
 
 
 
 
 
 
 
759bf41
 
 
 
 
 
f25fee8
 
759bf41
 
 
f25fee8
759bf41
 
 
 
 
 
 
 
 
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
import re
from datetime import datetime
from pathlib import Path

from huggingface_hub import HfApi, hf_hub_download

from .config import MODEL_SEARCH, ORG, TOKEN, stamp

api = HfApi(token=TOKEN)

SHA = re.compile(r"[0-9a-f]{40}")


def resolve(repo: str, revision: str, repo_type: str = "model") -> str:
    if revision and SHA.fullmatch(revision):
        return revision
    info = api.repo_info(repo, revision=revision or None, repo_type=repo_type, token=TOKEN)
    return info.sha


def model_repos() -> dict[str, datetime]:
    """Every backbone with the date of its last push, keyed alphabetically."""
    models = api.list_models(author=ORG, search=MODEL_SEARCH, sort="last_modified")
    return {model.id: model.last_modified for model in sorted(models, key=lambda m: m.id)}


def newest(pushes: dict[str, datetime]) -> str | None:
    return max(pushes, key=lambda repo: pushes[repo], default=None)


def revisions(repo: str) -> list[tuple[str, str]]:
    if not repo:
        return []
    commits = api.list_repo_commits(repo, token=TOKEN)
    tip = f" 路 {stamp(commits[0].created_at)}" if commits else ""
    return [(f"main (latest){tip}", "main"), *(_commit_choice(c) for c in commits)]


def _commit_choice(commit) -> tuple[str, str]:
    label = f"{commit.commit_id[:7]}{commit.title[:44]}{stamp(commit.created_at)}"
    return label, commit.commit_id


def download(repo: str, filename: str, revision: str) -> Path:
    return Path(hf_hub_download(repo, filename, revision=revision, token=TOKEN))


def has_file(repo: str, filename: str, revision: str) -> bool:
    return filename in api.list_repo_files(repo, revision=revision, token=TOKEN)