File size: 1,471 Bytes
881357e
 
ea9bdf0
 
c7a8a09
ea9bdf0
efa5772
 
 
 
 
ea9bdf0
 
 
 
 
 
 
 
 
 
 
 
d0a592f
 
 
 
 
 
 
 
 
 
 
 
 
 
ea9bdf0
 
 
 
 
 
 
 
 
 
6d68931
ea9bdf0
d0a592f
ea9bdf0
 
d0a592f
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
import os

from huggingface_hub import get_token

PROJECT_ROOT = os.path.dirname(os.path.dirname(__file__))
RESULTS_DIR = os.environ.get("RESULTS_DIR", os.path.join(PROJECT_ROOT, "results"))
SUBMISSIONS_DIR = os.environ.get(
    "SUBMISSIONS_DIR",
    os.path.join(RESULTS_DIR, "submissions"),
)
SUBMISSIONS_REPO_PREFIX = "submissions"


def _resolve_space_repo() -> str | None:
    space_id = os.environ.get("SPACE_ID")
    if space_id:
        return space_id

    author = os.environ.get("SPACE_AUTHOR_NAME")
    repo_name = os.environ.get("SPACE_REPO_NAME")
    if author and repo_name:
        return f"{author}/{repo_name}"

    return None


def _resolve_results_dataset_repo() -> str | None:
    explicit = os.environ.get("RESULTS_DATASET_REPO") or os.environ.get("RESULTS_REPO")
    if explicit:
        return explicit

    space_id = _resolve_space_repo()
    if space_id:
        owner, repo_name = space_id.split("/", 1)
        return f"{owner}/{repo_name}_results"

    return None


def _resolve_token() -> str | None:
    return (
        get_token()
        or os.environ.get("HF_TOKEN")
        or os.environ.get("HUGGING_FACE_HUB_TOKEN")
        or os.environ.get("HUGGINGFACEHUB_API_TOKEN")
    )


SPACE_REPO = _resolve_space_repo()
RESULTS_DATASET_REPO = _resolve_results_dataset_repo()
TOKEN = _resolve_token()
ON_SPACE = bool(os.environ.get("SPACE_ID") or os.environ.get("SPACE_REPO_NAME"))
USE_HUB_STORAGE = bool(RESULTS_DATASET_REPO and TOKEN)