mechark
commited on
Commit
·
34ab2b8
1
Parent(s):
8324ae8
Read secrets from /run/secrets/ directory (HF Spaces)
Browse files- src/core/config.py +40 -21
src/core/config.py
CHANGED
|
@@ -1,5 +1,16 @@
|
|
| 1 |
from pydantic_settings import BaseSettings
|
| 2 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
|
| 5 |
class Settings(BaseSettings):
|
|
@@ -17,41 +28,49 @@ class Settings(BaseSettings):
|
|
| 17 |
|
| 18 |
class Config:
|
| 19 |
env_file = ".env"
|
| 20 |
-
# Allow reading from environment variables with different prefixes
|
| 21 |
-
env_prefix = ""
|
| 22 |
-
case_sensitive = False
|
| 23 |
|
| 24 |
def __init__(self, **kwargs):
|
| 25 |
super().__init__(**kwargs)
|
| 26 |
-
# Try multiple environment variable names
|
| 27 |
-
# HuggingFace Spaces may use HF_TOKEN, HUGGINGFACE_TOKEN, or variations
|
| 28 |
import logging
|
| 29 |
|
| 30 |
if not self.HUGGINGFACE_TOKEN:
|
| 31 |
-
#
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
"HUGGINGFACE_TOKEN",
|
| 35 |
-
"HUGGING_FACE_HUB_TOKEN",
|
| 36 |
-
"HF_API_TOKEN"
|
| 37 |
-
]
|
| 38 |
|
| 39 |
-
for
|
| 40 |
-
token =
|
| 41 |
if token:
|
| 42 |
self.HUGGINGFACE_TOKEN = token
|
| 43 |
-
logging.info(f"Token loaded from {
|
| 44 |
break
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
# Debug: Log token status
|
| 47 |
if self.HUGGINGFACE_TOKEN:
|
| 48 |
-
logging.info(f"
|
| 49 |
else:
|
| 50 |
-
logging.error("HF Token is
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
| 55 |
|
| 56 |
|
| 57 |
settings = Settings()
|
|
|
|
| 1 |
from pydantic_settings import BaseSettings
|
| 2 |
import os
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def read_secret_from_file(secret_name: str) -> str:
|
| 7 |
+
"""Read a secret from HuggingFace Spaces secrets directory."""
|
| 8 |
+
secrets_dir = Path("/run/secrets")
|
| 9 |
+
secret_file = secrets_dir / secret_name
|
| 10 |
+
|
| 11 |
+
if secret_file.exists():
|
| 12 |
+
return secret_file.read_text().strip()
|
| 13 |
+
return ""
|
| 14 |
|
| 15 |
|
| 16 |
class Settings(BaseSettings):
|
|
|
|
| 28 |
|
| 29 |
class Config:
|
| 30 |
env_file = ".env"
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
def __init__(self, **kwargs):
|
| 33 |
super().__init__(**kwargs)
|
|
|
|
|
|
|
| 34 |
import logging
|
| 35 |
|
| 36 |
if not self.HUGGINGFACE_TOKEN:
|
| 37 |
+
# HuggingFace Spaces stores secrets in /run/secrets/ directory
|
| 38 |
+
# Try reading from secrets files first
|
| 39 |
+
secret_names = ["HF_TOKEN", "HUGGINGFACE_TOKEN"]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
for secret_name in secret_names:
|
| 42 |
+
token = read_secret_from_file(secret_name)
|
| 43 |
if token:
|
| 44 |
self.HUGGINGFACE_TOKEN = token
|
| 45 |
+
logging.info(f"Token loaded from secret file {secret_name}: {token[:10]}...")
|
| 46 |
break
|
| 47 |
+
|
| 48 |
+
# Fallback to environment variables
|
| 49 |
+
if not self.HUGGINGFACE_TOKEN:
|
| 50 |
+
token_env_vars = [
|
| 51 |
+
"HF_TOKEN",
|
| 52 |
+
"HUGGINGFACE_TOKEN",
|
| 53 |
+
"HUGGING_FACE_HUB_TOKEN",
|
| 54 |
+
]
|
| 55 |
+
|
| 56 |
+
for env_var in token_env_vars:
|
| 57 |
+
token = os.getenv(env_var, "").strip()
|
| 58 |
+
if token:
|
| 59 |
+
self.HUGGINGFACE_TOKEN = token
|
| 60 |
+
logging.info(f"Token loaded from env var {env_var}: {token[:10]}...")
|
| 61 |
+
break
|
| 62 |
|
| 63 |
# Debug: Log token status
|
| 64 |
if self.HUGGINGFACE_TOKEN:
|
| 65 |
+
logging.info(f"✓ HF Token configured ({len(self.HUGGINGFACE_TOKEN)} chars)")
|
| 66 |
else:
|
| 67 |
+
logging.error("✗ HF Token is NOT set!")
|
| 68 |
+
logging.error("Checked /run/secrets/ and environment variables")
|
| 69 |
+
# List what's actually available
|
| 70 |
+
secrets_path = Path("/run/secrets")
|
| 71 |
+
if secrets_path.exists():
|
| 72 |
+
logging.error(f"Files in /run/secrets/: {list(secrets_path.iterdir())}")
|
| 73 |
+
logging.error(f"Env vars with TOKEN/HF: {[k for k in os.environ if 'TOKEN' in k.upper() or 'HF' in k.upper()]}")
|
| 74 |
|
| 75 |
|
| 76 |
settings = Settings()
|