Spaces:
Sleeping
Sleeping
| """ | |
| Configuration Loader | |
| Loads and manages configuration from YAML files. | |
| """ | |
| import yaml | |
| import os | |
| from typing import Dict, Any, Optional | |
| from dataclasses import dataclass | |
| class AppConfig: | |
| """Application configuration.""" | |
| title: str | |
| description: str | |
| theme: str | |
| server_host: str | |
| server_port: int | |
| server_share: bool | |
| class LeaderboardConfig: | |
| """Leaderboard configuration.""" | |
| path: str | |
| columns: list | |
| top_results: int | |
| results_table_headers: list | |
| class MetricsConfig: | |
| """Metrics configuration.""" | |
| weights: Dict[str, float] | |
| descriptions: Dict[str, str] | |
| thresholds: Dict[str, float] | |
| formatting: Dict[str, str] | |
| class PromptsConfig: | |
| """Prompts configuration.""" | |
| files: Dict[str, str] | |
| fallback: str | |
| placeholders: Dict[str, str] | |
| sections: Dict[str, str] | |
| class ConfigLoader: | |
| """Loads and manages configuration from YAML files.""" | |
| def __init__(self, config_dir: str = "config"): | |
| self.config_dir = config_dir | |
| self._app_config = None | |
| self._leaderboard_config = None | |
| self._metrics_config = None | |
| self._prompts_config = None | |
| def _load_yaml(self, filename: str) -> Dict[str, Any]: | |
| """Load a YAML configuration file.""" | |
| filepath = os.path.join(self.config_dir, filename) | |
| if not os.path.exists(filepath): | |
| raise FileNotFoundError(f"Configuration file not found: {filepath}") | |
| with open(filepath, 'r') as f: | |
| return yaml.safe_load(f) | |
| def get_app_config(self) -> AppConfig: | |
| """Get application configuration.""" | |
| if self._app_config is None: | |
| config = self._load_yaml("app.yaml") | |
| app = config["app"] | |
| server = app["server"] | |
| self._app_config = AppConfig( | |
| title=app["title"], | |
| description=app["description"], | |
| theme=app["theme"], | |
| server_host=server["host"], | |
| server_port=server["port"], | |
| server_share=server["share"] | |
| ) | |
| return self._app_config | |
| def get_leaderboard_config(self) -> LeaderboardConfig: | |
| """Get leaderboard configuration.""" | |
| if self._leaderboard_config is None: | |
| config = self._load_yaml("app.yaml") | |
| leaderboard = config["leaderboard"] | |
| display = leaderboard["display"] | |
| self._leaderboard_config = LeaderboardConfig( | |
| path=leaderboard["path"], | |
| columns=leaderboard["columns"], | |
| top_results=display["top_results"], | |
| results_table_headers=display["results_table_headers"] | |
| ) | |
| return self._leaderboard_config | |
| def get_metrics_config(self) -> MetricsConfig: | |
| """Get metrics configuration.""" | |
| if self._metrics_config is None: | |
| config = self._load_yaml("metrics.yaml") | |
| metrics = config["metrics"] | |
| self._metrics_config = MetricsConfig( | |
| weights=metrics["weights"], | |
| descriptions=metrics["descriptions"], | |
| thresholds=metrics["thresholds"], | |
| formatting=metrics["formatting"] | |
| ) | |
| return self._metrics_config | |
| def get_prompts_config(self) -> PromptsConfig: | |
| """Get prompts configuration.""" | |
| if self._prompts_config is None: | |
| config = self._load_yaml("prompts.yaml") | |
| prompts = config["prompts"] | |
| self._prompts_config = PromptsConfig( | |
| files=prompts["files"], | |
| fallback=prompts["fallback"], | |
| placeholders=prompts["placeholders"], | |
| sections=prompts["sections"] | |
| ) | |
| return self._prompts_config | |
| def get_dialects(self) -> list: | |
| """Get available SQL dialects.""" | |
| config = self._load_yaml("app.yaml") | |
| return config["dialects"] | |
| def get_ui_config(self) -> Dict[str, Any]: | |
| """Get UI configuration.""" | |
| config = self._load_yaml("app.yaml") | |
| return config["ui"] | |
| def get_environment_config(self) -> Dict[str, Any]: | |
| """Get environment configuration.""" | |
| config = self._load_yaml("app.yaml") | |
| return config["environment"] | |
| def get_mock_sql_config(self) -> Dict[str, Any]: | |
| """Get mock SQL configuration.""" | |
| config = self._load_yaml("metrics.yaml") | |
| return config["mock_sql"] | |
| def get_visible_datasets(self) -> list: | |
| """Get list of visible datasets from configuration.""" | |
| config = self._load_yaml("app.yaml") | |
| return config.get("visible_datasets", []) | |
| # Global configuration loader instance | |
| config_loader = ConfigLoader() | |