Fix env file loading
Browse files
src/synthetic_dataset_generator/load_env.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
from pathlib import Path
|
| 3 |
from typing import Dict
|
|
|
|
| 4 |
|
| 5 |
def load_env_file(env_path: Path | str) -> Dict[str, str]:
|
| 6 |
"""
|
|
@@ -30,23 +31,18 @@ def load_env_file(env_path: Path | str) -> Dict[str, str]:
|
|
| 30 |
return env_vars
|
| 31 |
|
| 32 |
|
| 33 |
-
def init_environment()
|
| 34 |
-
"""
|
| 35 |
-
Initialize environment variables from .env file in project root.
|
| 36 |
-
|
| 37 |
-
Returns:
|
| 38 |
-
Dict of environment variables loaded
|
| 39 |
-
|
| 40 |
-
Raises:
|
| 41 |
-
FileNotFoundError: If the .env file doesn't exist
|
| 42 |
-
"""
|
| 43 |
root_dir = Path(__file__).parent.parent.parent
|
| 44 |
env_file = root_dir / ".env"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
"
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
return load_env_file(env_file)
|
|
|
|
| 1 |
import os
|
| 2 |
from pathlib import Path
|
| 3 |
from typing import Dict
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
|
| 6 |
def load_env_file(env_path: Path | str) -> Dict[str, str]:
|
| 7 |
"""
|
|
|
|
| 31 |
return env_vars
|
| 32 |
|
| 33 |
|
| 34 |
+
def init_environment():
|
| 35 |
+
"""Initialize environment variables."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
root_dir = Path(__file__).parent.parent.parent
|
| 37 |
env_file = root_dir / ".env"
|
| 38 |
+
|
| 39 |
+
# Load .env if it exists, but do not fail if it does not exist
|
| 40 |
+
if env_file.exists():
|
| 41 |
+
load_dotenv(env_file)
|
| 42 |
|
| 43 |
+
# Verify required variables are set
|
| 44 |
+
if not os.getenv("HF_TOKEN"):
|
| 45 |
+
raise ValueError(
|
| 46 |
+
"HF_TOKEN environment variable is required. "
|
| 47 |
+
"Please set it in your .env file or environment."
|
| 48 |
+
)
|
|
|