File size: 6,673 Bytes
379b63f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/env python3
"""Load KEY=VALUE settings from config.env (project root).

CLI flags always win over config.env values.
"""

from __future__ import annotations

import os
import shlex
from pathlib import Path

ROOT = Path(__file__).resolve().parent
DEFAULT_ENV_PATH = ROOT / "config.env"
# Alternate configs (e.g. config.en_pidgin.env) via CLI --config-env or XTTS_CONFIG_ENV.

# Always take these from config.env so a broken shell mirror/token cannot win.
_CONFIG_FORCE_KEYS = frozenset(
    {
        "HF_ENDPOINT",
        "HF_TOKEN",
        "HUGGINGFACE_ACCESS_TOKEN",
        "HUGGING_FACE_HUB_TOKEN",
        "HUGGINGFACE_TOKEN",
        "HF_HUB_DOWNLOAD_TIMEOUT",
        "HF_HUB_ETAG_TIMEOUT",
        "HF_DOWNLOAD_RETRIES",
    }
)


def load_config_env(path: Path | None = None, *, override: bool = False) -> Path | None:
    """Parse config.env into os.environ.

    Returns the path loaded, or None if missing.
    Does not override existing environment variables unless override=True,
    except for `_CONFIG_FORCE_KEYS` (HF endpoint/token/timeouts).
    """
    env_path = (path or DEFAULT_ENV_PATH).resolve()
    if not env_path.is_file():
        return None

    for raw in env_path.read_text(encoding="utf-8").splitlines():
        line = raw.strip()
        if not line or line.startswith("#"):
            continue
        if line.startswith("export "):
            line = line[len("export ") :].strip()
        if "=" not in line:
            continue
        key, value = line.split("=", 1)
        key = key.strip()
        value = value.strip()
        if not key:
            continue
        if len(value) >= 2 and value[0] == value[-1] and value[0] in "\"'":
            value = value[1:-1]
        # Skip blank assignments (e.g. FOO=) so they don't poison Hub clients
        if value == "":
            continue
        if override or key not in os.environ or key in _CONFIG_FORCE_KEYS:
            os.environ[key] = value
    return env_path


def env_str(name: str, default: str | None = None) -> str | None:
    v = os.getenv(name)
    if v is None or v == "":
        return default
    return v


def env_int(name: str, default: int) -> int:
    v = os.getenv(name)
    if v is None or v == "":
        return default
    return int(v)


def env_float(name: str, default: float) -> float:
    v = os.getenv(name)
    if v is None or v == "":
        return default
    return float(v)


def env_path(name: str, default: str | Path) -> Path:
    v = os.getenv(name)
    return Path(v) if v else Path(default)


def env_list(name: str, default: list[str] | None = None) -> list[str]:
    """Split a config value into a list (whitespace or comma separated)."""
    v = os.getenv(name)
    if v is None or v.strip() == "":
        return list(default or [])
    normalized = v.replace(",", " ")
    parts = shlex.split(normalized)
    return [p for p in parts if p]


def early_config_path_from_argv(argv: list[str] | None = None) -> Path | None:
    """Peek argv for --config-env / --config-env=PATH before full argparse."""
    import sys

    args = argv if argv is not None else sys.argv[1:]
    for i, a in enumerate(args):
        if a == "--config-env" and i + 1 < len(args):
            return Path(args[i + 1])
        if a.startswith("--config-env="):
            return Path(a.split("=", 1)[1])
    return None


def resolve_config_env_path(path: Path | None = None) -> Path:
    """Resolve config file: explicit path > XTTS_CONFIG_ENV > config.env."""
    if path is not None:
        return path.resolve()
    env_name = (os.getenv("XTTS_CONFIG_ENV") or "").strip()
    if env_name:
        return Path(env_name).expanduser().resolve()
    return DEFAULT_ENV_PATH.resolve()


def ensure_config_loaded(path: Path | None = None) -> None:
    """Load config.env (or alternate). If path is None, also checks argv --config-env."""
    if path is None:
        path = early_config_path_from_argv()
    env_path = resolve_config_env_path(path)
    loaded = load_config_env(env_path)
    if loaded:
        print(f"[config] loaded {loaded}")
    else:
        example = ROOT / "config.env.example"
        print(
            f"[config] no config file at {env_path}"
            + (f" (copy from {example.name})" if example.is_file() else "")
        )
    apply_hf_token()


def _sync_hf_hub_endpoint(endpoint: str) -> None:
    """Keep huggingface_hub / datasets in sync if they were already imported."""
    endpoint = endpoint.rstrip("/")
    try:
        import huggingface_hub.constants as hub_constants

        hub_constants.ENDPOINT = endpoint
        hub_constants.HUGGINGFACE_CO_URL_TEMPLATE = endpoint + "/{repo_id}/resolve/{revision}/{filename}"
    except Exception:
        pass
    try:
        import datasets.config as ds_config

        ds_config.HF_ENDPOINT = endpoint
        ds_config.HUB_DATASETS_URL = endpoint + "/datasets/{repo_id}/resolve/{revision}/{path}"
    except Exception:
        pass


def apply_hf_token() -> None:
    """Normalize HF token aliases and sync Hub endpoint.

    HF_ENDPOINT from config.env already wins over the shell via
    `_CONFIG_FORCE_KEYS` — no need to `unset HF_ENDPOINT` before runs.
    """
    endpoint = (os.environ.get("HF_ENDPOINT") or "").strip().rstrip("/")
    if endpoint and not endpoint.startswith(("http://", "https://")):
        del os.environ["HF_ENDPOINT"]
        endpoint = ""
    if not endpoint:
        endpoint = "https://huggingface.co"
        os.environ["HF_ENDPOINT"] = endpoint

    _sync_hf_hub_endpoint(endpoint)
    print(f"[config] HF_ENDPOINT={endpoint}")

    # Longer timeouts help IncompleteRead on slow/unstable links
    for key, default in (
        ("HF_HUB_DOWNLOAD_TIMEOUT", "120"),
        ("HF_HUB_ETAG_TIMEOUT", "60"),
        ("HF_DOWNLOAD_RETRIES", "8"),
    ):
        val = env_str(key, default)
        if val:
            os.environ[key] = val

    token = (
        env_str("HF_TOKEN")
        or env_str("HUGGINGFACE_ACCESS_TOKEN")
        or env_str("HUGGING_FACE_HUB_TOKEN")
        or env_str("HUGGINGFACE_TOKEN")
    )
    if not token:
        print("[config] HF token not set (optional for public repos)")
        return

    os.environ["HF_TOKEN"] = token
    os.environ["HUGGING_FACE_HUB_TOKEN"] = token
    if not os.getenv("HUGGINGFACE_ACCESS_TOKEN"):
        os.environ["HUGGINGFACE_ACCESS_TOKEN"] = token

    print(f"[config] HF token loaded ({token[:6]}…{token[-4:] if len(token) > 10 else '****'})")

    try:
        from huggingface_hub import login

        login(token=token, add_to_git_credential=False)
    except Exception as e:
        print(f"[config] huggingface_hub.login skipped: {e}")