File size: 1,113 Bytes
8c04aa9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""HF API client configuration for arca-processor"""
import os
from dataclasses import dataclass
from typing import Dict, Optional

from huggingface_hub import HfApi


@dataclass(frozen=True)
class HFConfig:
    token: Optional[str]
    dataset_repo: str


def _is_dev_mode() -> bool:
    value = str(os.environ.get("NODE_ENV") or os.environ.get("ENV") or "").strip().lower()
    return value in {"dev", "development", "local"}


def get_hf_config() -> HFConfig:
    token = str(os.environ.get("HF_TOKEN") or "").strip()
    dataset_repo = str(os.environ.get("DATASET_REPO") or "ArcaThread/arca-thread-priors").strip()
    if not token and not _is_dev_mode():
        raise RuntimeError("HF_TOKEN is required in non-dev environments")
    return HFConfig(token=token or None, dataset_repo=dataset_repo)


def get_hf_api() -> HfApi:
    cfg = get_hf_config()
    return HfApi(token=cfg.token)


def get_hf_headers() -> Dict[str, str]:
    cfg = get_hf_config()
    if not cfg.token:
        return {}
    return {"Authorization": f"Bearer {cfg.token}"}