Spaces:
Sleeping
Sleeping
File size: 3,510 Bytes
7e5c5ea |
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 |
import os
import logging
from pathlib import Path
from typing import Optional
from dotenv import load_dotenv
logger = logging.getLogger(__name__)
def load_environment_variables(env_file: str = None) -> None:
"""
Load environment variables from .env file.
Args:
env_file: Path to the .env file (if None, will look in src/.env and .env)
"""
if env_file is None:
# Try multiple locations
possible_paths = [
Path("src/.env"),
Path(".env"),
Path(__file__).parent / ".env"
]
for env_path in possible_paths:
if env_path.exists():
load_dotenv(env_path)
logger.info(f"Environment variables loaded from {env_path}")
return
logger.warning("No .env file found in any of the expected locations")
else:
env_path = Path(env_file)
if env_path.exists():
load_dotenv(env_path)
logger.info(f"Environment variables loaded from {env_path}")
else:
logger.warning(f"Environment file not found: {env_path}")
def validate_api_keys() -> dict:
"""
Validate that required API keys are available.
Returns:
Dict with validation results
"""
required_keys = {
'GOOGLE_API_KEY': 'Google AI API key for Gemini model'
}
optional_keys = {
'OPENAI_API_KEY': 'OpenAI API key',
'HF_TOKEN': 'Hugging Face token',
'nvidia_api_key': 'NVIDIA API key'
}
validation_results = {
'valid': True,
'missing_required': [],
'missing_optional': [],
'available_keys': []
}
# Check required keys
for key, description in required_keys.items():
if os.getenv(key):
validation_results['available_keys'].append(key)
logger.info(f"✓ {key} is available")
else:
validation_results['missing_required'].append(key)
validation_results['valid'] = False
logger.error(f"✗ Missing required {key}: {description}")
# Check optional keys
for key, description in optional_keys.items():
if os.getenv(key):
validation_results['available_keys'].append(key)
logger.info(f"✓ {key} is available (optional)")
else:
validation_results['missing_optional'].append(key)
logger.debug(f"- {key} not found (optional): {description}")
return validation_results
def ensure_directory_exists(directory: str) -> Path:
"""
Ensure a directory exists, create if it doesn't.
Args:
directory: Directory path to create
Returns:
Path object of the directory
"""
dir_path = Path(directory)
dir_path.mkdir(parents=True, exist_ok=True)
logger.debug(f"Directory ensured: {dir_path}")
return dir_path
def get_project_root() -> Path:
"""Get the project root directory."""
return Path(__file__).parent.parent
def format_file_size(size_bytes: int) -> str:
"""
Format file size in human readable format.
Args:
size_bytes: Size in bytes
Returns:
Formatted size string
"""
if size_bytes == 0:
return "0B"
size_names = ["B", "KB", "MB", "GB", "TB"]
i = 0
while size_bytes >= 1024 and i < len(size_names) - 1:
size_bytes /= 1024.0
i += 1
return f"{size_bytes:.1f}{size_names[i]}" |