File size: 2,307 Bytes
7dcbd04
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Publie le code (Space Docker) ou un checkpoint (modèle) sur le Hub via huggingface_hub.

  HF_TOKEN=... python scripts/push_to_hub.py --repo-id RDS777/mouh --kind space
  HF_TOKEN=... python scripts/push_to_hub.py --repo-id RDS777/mouh-1.5b --kind model --folder checkpoints/final
  python scripts/push_to_hub.py --repo-id x/y --kind space --dry-run     # liste les fichiers sans rien envoyer
"""
import argparse
import os
import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
IGNORE = ["__pycache__", "*.pyc", ".git", ".git/*", ".pytest_cache", "checkpoints", "checkpoints/*", "*.egg-info", ".env", "audit"]


def main() -> None:
    ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
    ap.add_argument("--repo-id", required=True)
    ap.add_argument("--kind", choices=["space", "model"], default="space")
    ap.add_argument("--folder", default=str(ROOT))
    ap.add_argument("--private", action="store_true")
    ap.add_argument("--dry-run", action="store_true")
    ap.add_argument("--message", default="M.O.U.H. — mise à jour")
    a = ap.parse_args()

    folder = Path(a.folder)
    files = [p for p in folder.rglob("*") if p.is_file() and not any(
        part in {"__pycache__", ".git", ".pytest_cache", "checkpoints", "audit"} or part.endswith(".egg-info")
        for part in p.relative_to(folder).parts) and p.suffix != ".pyc" and p.name != ".env"]
    if a.dry_run:
        for p in sorted(files):
            print(p.relative_to(folder))
        print(f"{len(files)} fichier(s) seraient envoyés vers {a.kind} {a.repo_id}")
        return

    token = os.environ.get("HF_TOKEN")
    if not token:
        sys.exit("HF_TOKEN absent : définissez un jeton avec droit d'écriture")
    from huggingface_hub import HfApi

    api = HfApi(token=token)
    if a.kind == "space":
        api.create_repo(a.repo_id, repo_type="space", space_sdk="docker", exist_ok=True, private=a.private)
    else:
        api.create_repo(a.repo_id, repo_type="model", exist_ok=True, private=a.private)
    info = api.upload_folder(folder_path=str(folder), repo_id=a.repo_id, repo_type=a.kind,
                             ignore_patterns=IGNORE, commit_message=a.message)
    print(info)


if __name__ == "__main__":
    main()