Spaces:
Sleeping
Sleeping
File size: 4,303 Bytes
7dfe46c |
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 |
import yaml
import os
import logging
from pathlib import Path
from typing import Optional
from dotenv import load_dotenv
import yaml
import sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
try:
from logger.custom_logger import CustomLoggerTracker
custom_log = CustomLoggerTracker()
logger = custom_log.get_logger("utilites")
except ImportError:
# Fallback to standard logging if custom logger not available
logger = logging.getLogger("utilites")
logger = logging.getLogger(__name__)
def load_yaml_config(config_path: str):
with open(config_path, "r") as f:
config = yaml.safe_load(f)
return config
def load_environment_variables(env_file: str = None) -> None:
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',
'GROQ_API_KEY': 'Groq API key for RAG system',
'SILICONFLOW_API_KEY': 'Silicon Flow API key for embeddings and reranking',
'QDRANT_URL': 'Qdrant vector database URL',
'QDRANT_API_KEY': 'Qdrant 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]}"
def load_config_yaml(config_path: str):
with open(config_path, 'r') as file:
config = yaml.safe_load(file)
return config
if __name__=="__main__":
print(f"config ...") |