File size: 1,237 Bytes
1a698ed
 
 
 
 
 
c1ca456
1a698ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from pathlib import Path

from huggingface_hub import hf_hub_download
from huggingface_hub.errors import EntryNotFoundError

HF_REPO_ID = os.getenv("HF_REPO_ID", "Swaraj2003/test_model_2")
HF_REPO_TYPE = os.getenv("HF_REPO_TYPE", "model")
HF_REVISION = os.getenv("HF_REVISION")
HF_CACHE_DIR = os.getenv("HF_HOME")


def get_artifact_path(relative_path: str) -> Path:
    candidates = [relative_path]

    if relative_path.startswith("model_files/"):
        candidates.append(relative_path.removeprefix("model_files/"))
    else:
        candidates.append(f"model_files/{relative_path}")

    last_error = None
    for candidate in dict.fromkeys(candidates):
        try:
            return Path(
                hf_hub_download(
                    repo_id=HF_REPO_ID,
                    filename=candidate,
                    repo_type=HF_REPO_TYPE,
                    revision=HF_REVISION,
                    cache_dir=HF_CACHE_DIR,
                )
            )
        except EntryNotFoundError as exc:
            last_error = exc

    raise FileNotFoundError(
        f"Artifact not found in '{HF_REPO_ID}'. Tried paths: {candidates}. "
        "Verify repo folder structure and file names."
    ) from last_error