File size: 3,465 Bytes
f5a2109
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
675bd91
 
 
 
 
 
f5a2109
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
da80f2e
f5a2109
 
 
 
 
 
 
 
 
 
 
 
 
 
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python
"""Create the dataset repo and upload `dist/` (the publish step).

    HF_TOKEN=hf_xxx python publish.py --repo mahalisyarifuddin/emotweetid-ekman7

The token is read from --token or $HF_TOKEN and is never written to disk.  Uploads are resumable:
`HfApi.upload_folder` skips files whose SHA already matches what the repo holds.
"""
import argparse
import json
import os
import sys


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--repo", required=True, help="namespace/name")
    ap.add_argument("--dist", default="dist")
    ap.add_argument("--token", default=os.environ.get("HF_TOKEN", ""))
    ap.add_argument("--private", action="store_true")
    ap.add_argument("--commit-message",
                    default="EmoTweetID unified under Ekman's 7 universal emotions: human labels "
                            "kept, anger pool split into anger/contempt by laya")
    args = ap.parse_args()

    if not args.token:
        sys.exit("no token: pass --token or set HF_TOKEN (a fine-grained token with `write` scope "
                 "on the target namespace). Nothing was uploaded.")
    for need in ("README.md",):
        if not os.path.exists(os.path.join(args.dist, need)):
            sys.exit("run make_dataset.py first: %s is missing from %s" % (need, args.dist))
    # working notes, scratch dirs and the local model cache must never be uploaded
    junk = [f for f in sorted(os.listdir(args.dist))
            if f.upper().startswith(("REVISION_NOTES", "NOTES", "TODO", "CHANGELOG"))
            or f in ("build", "models", "data", "dist", ".git")]
    if junk:
        sys.exit("refusing to publish: %s/%s" % (args.dist, ", ".join(junk)))

    from huggingface_hub import HfApi
    api = HfApi(token=args.token)
    who = api.whoami()
    print("[publish] authenticated as %s (write scopes: %s)" % (
        who.get("name"), sorted({s.get("scope", {}).get("value", "*") for s in who.get("accessToken", {}).get("fineGrained", {}).get("scopes", [])})))
    ns = args.repo.split("/")[0]
    if who.get("name") != ns and not any(a.get("name") == ns for a in who.get("orgs", []) or []):
        sys.exit("[publish] this token cannot write to %r (it belongs to %s). "
                 "Pass --repo <name>/... or a token for that namespace." % (ns, who.get("name")))

    url = api.create_repo(args.repo, repo_type="dataset", private=args.private, exist_ok=True).url
    print("[publish] repo ready:", url)
    info = api.upload_folder(folder_path=args.dist, repo_id=args.repo, repo_type="dataset",
                             commit_message=args.commit_message)
    print("[publish] committed:", info.commit_url if hasattr(info, "commit_url") else info)
    files = sorted(os.listdir(args.dist))
    print("[publish] uploaded %d top-level entries: %s" % (len(files), ", ".join(files)))
    remote = {f.rfilename for f in api.dataset_info(args.repo, files_metadata=True).siblings}
    local = set()
    for root, _, fs in os.walk(args.dist):
        for f in fs:
            local.add(os.path.relpath(os.path.join(root, f), args.dist).replace(os.sep, "/"))
    missing = sorted(local - remote)
    print("[publish] %d files in the repo, %d still missing upstream" % (len(remote), len(missing)))
    if missing:
        sys.exit("upload did not land everything: %s" % ", ".join(missing[:8]))
    print("https://huggingface.co/datasets/%s" % args.repo)
    return 0


if __name__ == "__main__":
    sys.exit(main())