Spaces:
Sleeping
Sleeping
| 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) | |