Spaces:
Running on T4

File size: 2,776 Bytes
ff80158
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python3
"""Copy TCGA cached results between HF dataset caches (e.g. staging -> production).

    uv run python scripts/copy_tcga_cache.py \\
        --src raylim/mosaic-tcga-cache-staging --dst PDM-Group/mosaic-tcga-cache \\
        TCGA-86-8074-01Z-00-DX1 ...      # barcodes or file UUIDs (default: UI examples)

Only adds files that are missing in the destination; never overwrites. Result
folders are keyed by a hash of the settings, cache version and pinned model
revisions, so results computed by the same code on staging are exactly what
production looks up. metadata.json is skipped when the destination already
has one (the app refreshes stale metadata itself).
"""

from __future__ import annotations

import argparse
import os
import sys
import tempfile
from pathlib import Path

from huggingface_hub import CommitOperationAdd, HfApi, hf_hub_download

sys.path.insert(0, str(Path(__file__).resolve().parent))
from warm_tcga_examples import EXAMPLE_SLIDES  # noqa: E402


def main(argv=None) -> int:
    parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0])
    parser.add_argument("--src", required=True)
    parser.add_argument("--dst", required=True)
    parser.add_argument("--dry-run", action="store_true")
    parser.add_argument("slides", nargs="*", default=EXAMPLE_SLIDES)
    args = parser.parse_args(argv)

    from mosaic.tcga import is_valid_uuid, query_slide_uuid

    api = HfApi(token=os.environ.get("HF_TOKEN"))
    src_files = set(api.list_repo_files(args.src, repo_type="dataset"))
    dst_files = set(api.list_repo_files(args.dst, repo_type="dataset"))

    to_copy = []
    for slide in args.slides:
        uuid = slide if is_valid_uuid(slide) else query_slide_uuid(slide)
        mine = sorted(f for f in src_files if f.startswith(f"{uuid}/"))
        new = [f for f in mine if f not in dst_files]  # never overwrite
        print(f"{slide} ({uuid}): {len(mine)} files on src, {len(new)} new")
        to_copy += new

    if not to_copy:
        print("Nothing to copy.")
        return 0
    for f in to_copy:
        print("  +", f)
    if args.dry_run:
        return 0

    with tempfile.TemporaryDirectory() as tmp:
        ops = [
            CommitOperationAdd(
                path_in_repo=f,
                path_or_fileobj=hf_hub_download(
                    args.src, f, repo_type="dataset", local_dir=tmp
                ),
            )
            for f in to_copy
        ]
        api.create_commit(
            args.dst,
            repo_type="dataset",
            operations=ops,
            commit_message=f"Copy {len(ops)} cached result files from {args.src}",
        )
    print(f"Copied {len(to_copy)} files to {args.dst}")
    return 0


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