Update config.py
Browse files
config.py
CHANGED
|
@@ -1,46 +1,46 @@
|
|
| 1 |
-
"""Configuration management for Helium server."""
|
| 2 |
-
|
| 3 |
-
import os
|
| 4 |
-
from pathlib import Path
|
| 5 |
-
from dotenv import load_dotenv
|
| 6 |
-
import logging
|
| 7 |
-
|
| 8 |
-
# Load environment variables from .env file
|
| 9 |
-
env_path = Path(__file__).parent / '.env'
|
| 10 |
-
load_dotenv(dotenv_path=env_path)
|
| 11 |
-
|
| 12 |
-
# Database configuration
|
| 13 |
-
DB_URL = os.getenv('HELIUM_DB_URL', 'hf://datasets/Fred808/helium/storage.json')
|
| 14 |
-
|
| 15 |
-
# HuggingFace token configuration
|
| 16 |
-
def get_hf_token() -> str:
|
| 17 |
-
"""Get the HuggingFace token from environment variables."""
|
| 18 |
-
token = os.getenv('HF_TOKEN')
|
| 19 |
-
if not token:
|
| 20 |
-
logging.error("HF_TOKEN environment variable not set")
|
| 21 |
-
raise ValueError(
|
| 22 |
-
"HF_TOKEN environment variable not set. "
|
| 23 |
-
"Please set it in .env file or environment. "
|
| 24 |
-
"Create a .env file in the project root with: HF_TOKEN=your_token_here"
|
| 25 |
-
)
|
| 26 |
-
return token
|
| 27 |
-
|
| 28 |
-
def get_db_url() -> str:
|
| 29 |
-
"""Get the database URL from environment variables."""
|
| 30 |
-
if not DB_URL or not DB_URL.startswith('hf://datasets/'):
|
| 31 |
-
logging.error(f"Invalid database URL: {DB_URL}")
|
| 32 |
-
raise ValueError(
|
| 33 |
-
f"Invalid database URL: {DB_URL}. "
|
| 34 |
-
"URL must start with 'hf://datasets/' and be properly formatted"
|
| 35 |
-
)
|
| 36 |
-
return DB_URL
|
| 37 |
-
|
| 38 |
-
# Singleton instance for HF token to avoid reading env multiple times
|
| 39 |
-
_hf_token = None
|
| 40 |
-
|
| 41 |
-
def get_hf_token_cached() -> str:
|
| 42 |
-
"""Get the HuggingFace token with caching."""
|
| 43 |
-
global _hf_token
|
| 44 |
-
if _hf_token is None:
|
| 45 |
-
_hf_token = get_hf_token()
|
| 46 |
-
return _hf_token
|
|
|
|
| 1 |
+
"""Configuration management for Helium server."""
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
import logging
|
| 7 |
+
|
| 8 |
+
# Load environment variables from .env file
|
| 9 |
+
env_path = Path(__file__).parent / '.env'
|
| 10 |
+
load_dotenv(dotenv_path=env_path)
|
| 11 |
+
|
| 12 |
+
# Database configuration
|
| 13 |
+
DB_URL = os.getenv('HELIUM_DB_URL', 'hf://datasets/Fred808/helium/storage.json')
|
| 14 |
+
|
| 15 |
+
# HuggingFace token configuration
|
| 16 |
+
def get_hf_token() -> str:
|
| 17 |
+
"""Get the HuggingFace token from environment variables."""
|
| 18 |
+
token = os.getenv('HF_TOKEN')
|
| 19 |
+
if not token:
|
| 20 |
+
logging.error("HF_TOKEN environment variable not set")
|
| 21 |
+
raise ValueError(
|
| 22 |
+
"HF_TOKEN environment variable not set. "
|
| 23 |
+
"Please set it in .env file or environment. "
|
| 24 |
+
"Create a .env file in the project root with: HF_TOKEN=your_token_here"
|
| 25 |
+
)
|
| 26 |
+
return token
|
| 27 |
+
|
| 28 |
+
def get_db_url() -> str:
|
| 29 |
+
"""Get the database URL from environment variables."""
|
| 30 |
+
if not DB_URL or not DB_URL.startswith('hf://datasets/'):
|
| 31 |
+
logging.error(f"Invalid database URL: {DB_URL}")
|
| 32 |
+
raise ValueError(
|
| 33 |
+
f"Invalid database URL: {DB_URL}. "
|
| 34 |
+
"URL must start with 'hf://datasets/' and be properly formatted"
|
| 35 |
+
)
|
| 36 |
+
return DB_URL
|
| 37 |
+
|
| 38 |
+
# Singleton instance for HF token to avoid reading env multiple times
|
| 39 |
+
_hf_token = None
|
| 40 |
+
|
| 41 |
+
def get_hf_token_cached() -> str:
|
| 42 |
+
"""Get the HuggingFace token with caching."""
|
| 43 |
+
global _hf_token
|
| 44 |
+
if _hf_token is None:
|
| 45 |
+
_hf_token = get_hf_token()
|
| 46 |
+
return _hf_token
|