File size: 1,299 Bytes
22b25b9
 
 
 
 
 
 
 
 
6ab2f07
c2aa97a
22b25b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Shared file naming and directory layout for dataset readers and writers."""
from pathlib import Path
import re

SHARD_PREFIX_LENGTH = 3
DATASET_FILE_PATTERN = re.compile(
    r"(?P<id>.+?)_(?:(?:source|intermediate|edited|result|perfect)"
    r"(?:_[A-Za-z0-9][A-Za-z0-9_-]*)?\.(?:png|jpe?g|webp)"
    r"|(?:prompt|best|perfect|all_rejected)\.txt"
    r"|(?:author|decode)\.json"
    r"|(?:meat|semantic)\.(?:png|json)"
    r"|result(?:_[A-Za-z0-9][A-Za-z0-9_-]*)?\.uv_review\.json)"
)


def generation_directory(root: Path, model_version: str, generation_id: str) -> Path:
    """Use the first three ID characters for every model; short IDs stay intact."""
    for value in (model_version, generation_id):
        if not value or value in {".", ".."} or any(c in value for c in "/\\\0"):
            raise ValueError("Model version and generation ID must be single path components")
    return Path(root) / model_version / generation_id[:SHARD_PREFIX_LENGTH]


def model_directory(sample_directory: Path, generation_id: str) -> Path:
    """Recognize current shards and the previous one-character layout for readers."""
    directory = Path(sample_directory)
    if directory.name in {generation_id[:SHARD_PREFIX_LENGTH], generation_id[:1]}:
        return directory.parent
    return directory