File size: 10,273 Bytes
8b21729 cfeb3a6 8b21729 cfeb3a6 8b21729 cfeb3a6 8b21729 cfeb3a6 8b21729 90b0a17 8b21729 cfeb3a6 8b21729 cfeb3a6 8b21729 cfeb3a6 8b21729 90b0a17 8b21729 17e3d1d 8b21729 90b0a17 8b21729 90b0a17 8b21729 90b0a17 8b21729 90b0a17 8b21729 90b0a17 8b21729 90b0a17 8b21729 90b0a17 8b21729 90b0a17 8b21729 90b0a17 8b21729 90b0a17 8b21729 90b0a17 8b21729 90b0a17 cfeb3a6 8b21729 cfeb3a6 8b21729 |
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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
"""
Configuration settings for Data Extractor Using Gemini
Optimized for Gemini-only model usage with robust directory management
"""
import os
from pathlib import Path
from dotenv import load_dotenv
import logging
# Load environment variables
load_dotenv()
logger = logging.getLogger(__name__)
class Settings:
"""Configuration settings with Gemini-only model support and robust directory management."""
# === GEMINI MODEL CONFIGURATION ===
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
# Gemini model specifications - using gemini-2.5-flash (supports thinking budget)
DATA_EXTRACTOR_MODEL = os.getenv("DATA_EXTRACTOR_MODEL", "gemini-2.5-pro")
DATA_ARRANGER_MODEL = os.getenv("DATA_ARRANGER_MODEL", "gemini-2.5-pro")
CODE_GENERATOR_MODEL = os.getenv("CODE_GENERATOR_MODEL", "gemini-2.5-flash")
# Thinking budgets optimized for each task type
DATA_EXTRACTOR_MODEL_THINKING_BUDGET = int(os.getenv("DATA_EXTRACTOR_THINKING_BUDGET", "4096"))
DATA_ARRANGER_MODEL_THINKING_BUDGET = int(os.getenv("DATA_ARRANGER_THINKING_BUDGET", "4096"))
CODE_GENERATOR_MODEL_THINKING_BUDGET = int(os.getenv("CODE_GENERATOR_THINKING_BUDGET", "4096"))
# === FILE PROCESSING CONFIGURATION ===
MAX_FILE_SIZE_MB = int(os.getenv("MAX_FILE_SIZE_MB", "50"))
SUPPORTED_FILE_TYPES = [
"pdf", "txt", "docx", "xlsx", "csv", "md", "json", "xml", "html",
"png", "jpg", "jpeg", "doc", "xls", "ppt", "pptx"
]
# === DIRECTORY MANAGEMENT ===
# Centralized working directory - all operations happen within this directory
WORKING_DIR = Path(os.getenv("WORKING_DIR", "/tmp/data_extractor_gemini"))
# Subdirectories within working directory
TEMP_DIR = WORKING_DIR / "temp"
INPUT_DIR = WORKING_DIR / "input"
OUTPUT_DIR = WORKING_DIR / "output"
CACHE_DIR = WORKING_DIR / "cache"
LOGS_DIR = WORKING_DIR / "logs"
# === WORKFLOW CONFIGURATION ===
# Retry and timeout settings
MAX_RETRIES = int(os.getenv("MAX_RETRIES", "3"))
RETRY_DELAY_SECONDS = int(os.getenv("RETRY_DELAY_SECONDS", "5"))
AGENT_TIMEOUT_SECONDS = int(os.getenv("AGENT_TIMEOUT_SECONDS", "300"))
# Cache settings
ENABLE_CACHING = os.getenv("ENABLE_CACHING", "true").lower() == "true"
CACHE_TTL_HOURS = int(os.getenv("CACHE_TTL_HOURS", "24"))
@classmethod
def initialize_directories(cls):
"""Initialize all required directories with proper permissions."""
directories = [
cls.WORKING_DIR,
cls.TEMP_DIR,
cls.INPUT_DIR,
cls.OUTPUT_DIR,
cls.CACHE_DIR,
cls.LOGS_DIR
]
created_dirs = []
for directory in directories:
try:
directory.mkdir(parents=True, exist_ok=True)
# Test write permissions
test_file = directory / ".write_test"
test_file.write_text("test")
test_file.unlink()
created_dirs.append(str(directory))
logger.debug(f"Directory initialized: {directory}")
except Exception as e:
logger.error(f"Failed to initialize directory {directory}: {e}")
raise RuntimeError(f"Cannot create or write to directory {directory}: {e}")
logger.info(f"Successfully initialized {len(created_dirs)} directories")
return created_dirs
@classmethod
def validate_config(cls):
"""Comprehensive configuration validation with detailed error reporting."""
errors = []
warnings = []
# === CRITICAL VALIDATIONS ===
# Google API Key validation
if not cls.GOOGLE_API_KEY:
errors.append("GOOGLE_API_KEY is required. Get it from https://aistudio.google.com/app/apikey")
elif len(cls.GOOGLE_API_KEY) < 30:
warnings.append("GOOGLE_API_KEY appears to be too short - verify it's correct")
# Model name validation
gemini_models = [cls.DATA_EXTRACTOR_MODEL, cls.DATA_ARRANGER_MODEL, cls.CODE_GENERATOR_MODEL]
for i, model in enumerate(gemini_models):
model_names = ["DATA_EXTRACTOR_MODEL", "DATA_ARRANGER_MODEL", "CODE_GENERATOR_MODEL"]
if not model:
errors.append(f"{model_names[i]} cannot be empty")
elif not model.startswith("gemini-"):
errors.append(f"{model_names[i]} must be a Gemini model (starts with 'gemini-'), got: {model}")
# Directory validation
try:
cls.initialize_directories()
except Exception as e:
errors.append(f"Directory initialization failed: {e}")
# === MODERATE VALIDATIONS ===
# File size validation
if cls.MAX_FILE_SIZE_MB <= 0:
errors.append("MAX_FILE_SIZE_MB must be positive")
elif cls.MAX_FILE_SIZE_MB > 100:
warnings.append(f"MAX_FILE_SIZE_MB ({cls.MAX_FILE_SIZE_MB}) is very large - may cause memory issues")
# Supported file types validation
if not cls.SUPPORTED_FILE_TYPES:
errors.append("SUPPORTED_FILE_TYPES cannot be empty")
# Thinking budget validation
budgets = [
(cls.DATA_EXTRACTOR_MODEL_THINKING_BUDGET, "DATA_EXTRACTOR_MODEL_THINKING_BUDGET"),
(cls.DATA_ARRANGER_MODEL_THINKING_BUDGET, "DATA_ARRANGER_MODEL_THINKING_BUDGET"),
(cls.CODE_GENERATOR_MODEL_THINKING_BUDGET, "CODE_GENERATOR_MODEL_THINKING_BUDGET")
]
for budget, name in budgets:
if budget < 1024:
warnings.append(f"{name} ({budget}) is quite low - may affect model performance")
elif budget > 8192:
warnings.append(f"{name} ({budget}) is very high - may be unnecessary")
# Retry configuration validation
if cls.MAX_RETRIES < 1:
warnings.append("MAX_RETRIES should be at least 1")
elif cls.MAX_RETRIES > 10:
warnings.append("MAX_RETRIES is very high - may cause long delays")
# === RESULT PROCESSING ===
if errors:
error_msg = "❌ Configuration validation failed:\n"
error_msg += "\n".join(f" • {error}" for error in errors)
if warnings:
error_msg += "\n\n⚠️ Warnings:\n"
error_msg += "\n".join(f" • {warning}" for warning in warnings)
raise ValueError(error_msg)
if warnings:
logger.warning("Configuration warnings detected:")
for warning in warnings:
logger.warning(f" • {warning}")
logger.info("✅ Configuration validation successful")
return True
@classmethod
def get_session_directories(cls, session_id: str):
"""Get session-specific directory structure."""
session_base = cls.WORKING_DIR / session_id
return {
"base": session_base,
"input": session_base / "input",
"output": session_base / "output",
"temp": session_base / "temp",
"cache": session_base / "cache"
}
@classmethod
def create_session_directories(cls, session_id: str):
"""Create and validate session-specific directories."""
session_dirs = cls.get_session_directories(session_id)
created = []
for name, directory in session_dirs.items():
try:
directory.mkdir(parents=True, exist_ok=True)
# Test write permissions
test_file = directory / ".write_test"
test_file.write_text("test")
test_file.unlink()
created.append(str(directory))
except Exception as e:
logger.error(f"Failed to create session directory {name}: {e}")
raise RuntimeError(f"Cannot create session directory {directory}: {e}")
logger.info(f"Created {len(created)} session directories for {session_id}")
return session_dirs
@classmethod
def cleanup_session(cls, session_id: str, keep_output: bool = True):
"""Clean up session directories with option to preserve output."""
session_dirs = cls.get_session_directories(session_id)
import shutil
cleaned = []
for name, directory in session_dirs.items():
if keep_output and name == "output":
continue
if directory.exists():
try:
shutil.rmtree(directory)
cleaned.append(str(directory))
except Exception as e:
logger.warning(f"Could not clean {name} directory: {e}")
logger.info(f"Cleaned {len(cleaned)} session directories for {session_id}")
return cleaned
@classmethod
def get_debug_info(cls):
"""Get comprehensive debug information about current configuration."""
import platform
import sys
return {
"python_version": sys.version,
"platform": platform.platform(),
"temp_dir": str(cls.TEMP_DIR),
"temp_dir_exists": cls.TEMP_DIR.exists(),
"models": {
"data_extractor": cls.DATA_EXTRACTOR_MODEL,
"data_arranger": cls.DATA_ARRANGER_MODEL,
"code_generator": cls.CODE_GENERATOR_MODEL,
},
"api_keys": {
"google_api_key_present": bool(cls.GOOGLE_API_KEY),
"google_api_key_length": len(cls.GOOGLE_API_KEY) if cls.GOOGLE_API_KEY else 0
}
}
# Global settings instance
settings = Settings()
# Auto-initialize directories on import
try:
settings.initialize_directories()
logger.debug("Settings initialized successfully")
except Exception as e:
logger.error(f"Failed to initialize settings: {e}")
# Don't raise here to allow import to succeed
|