File size: 1,029 Bytes
cc6274a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Token discovery + user-friendly auth error messages."""

from __future__ import annotations

import os


def get_hf_token() -> str | None:
    """Read HF token from standard env vars.

    `HF_TOKEN` wins over `HUGGING_FACE_HUB_TOKEN` for consistency with the
    huggingface-cli default.
    """
    return os.environ.get("HF_TOKEN") or os.environ.get("HUGGING_FACE_HUB_TOKEN")


def get_modelscope_token() -> str | None:
    return os.environ.get("MODELSCOPE_API_TOKEN") or os.environ.get("MODELSCOPE_TOKEN")


def hf_auth_error_message(model_id: str) -> str:
    return (
        f"Model '{model_id}' requires authentication (gated or private).\n"
        "Set HF_TOKEN env var or run: huggingface-cli login"
    )


def modelscope_auth_error_message(model_id: str) -> str:
    # Chinese user-facing message — full-width punctuation is intentional.
    return (
        f"模型 '{model_id}' 需要登录(gated 或 私有)。\n"
        "设置 MODELSCOPE_API_TOKEN 环境变量,或执行:modelscope login"
    )