Spaces:
Sleeping
Sleeping
File size: 4,881 Bytes
4e37375 | 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 | """配置管理模块
提供应用程序配置的加载和管理功能。
支持多环境配置和环境变量覆盖。
"""
import os
from pathlib import Path
from typing import Any, Dict, Optional
import yaml
from pydantic import Field
from pydantic_settings import BaseSettings
from pydantic_settings import SettingsConfigDict
class AppConfig(BaseSettings):
"""应用程序配置"""
model_config = SettingsConfigDict(
env_prefix="APP_",
env_file=".env",
env_file_encoding="utf-8",
case_sensitive=False,
extra="ignore" # 忽略额外字段
)
name: str = "音频转文字服务"
version: str = "1.0.0"
debug: bool = False
host: str = "127.0.0.1"
port: int = 7860
max_file_size: int = 2147483648 # 2GB
max_files_count: int = 100
concurrent_tasks: int = 5
class OSSConfig(BaseSettings):
"""OSS配置"""
model_config = SettingsConfigDict(
env_prefix="OSS_",
env_file=".env",
env_file_encoding="utf-8",
case_sensitive=False,
extra="ignore"
)
endpoint: str = Field(..., description="OSS服务端点")
access_key_id: str = Field(..., description="访问密钥ID")
access_key_secret: str = Field(..., description="访问密钥密码")
bucket_name: str = Field(..., description="存储桶名称")
upload_timeout: int = 300
url_expire_hours: int = 24
temp_prefix: str = "temp/audio"
auto_cleanup_days: int = 7
class DashScopeConfig(BaseSettings):
"""阿里云百炼API配置"""
model_config = SettingsConfigDict(
env_prefix="DASHSCOPE_",
env_file=".env",
env_file_encoding="utf-8",
case_sensitive=False,
extra="ignore"
)
api_key: str = Field(..., description="API密钥")
base_url: str = "https://dashscope.aliyuncs.com/api/v1"
model: str = "paraformer-v2"
timeout: int = 300
max_retries: int = 3
retry_delay: int = 5
language_hints: list[str] = ["zh", "en"]
class TaskConfig(BaseSettings):
"""任务配置"""
model_config = SettingsConfigDict(
env_prefix="TASK_",
env_file=".env",
env_file_encoding="utf-8",
case_sensitive=False,
extra="ignore"
)
status_check_interval: int = 2
max_processing_time: int = 3600 # 1小时
queue_size: int = 1000
class LoggingConfig(BaseSettings):
"""日志配置"""
model_config = SettingsConfigDict(
env_prefix="LOGGING_",
env_file=".env",
env_file_encoding="utf-8",
case_sensitive=False,
extra="ignore"
)
level: str = "INFO"
format: str = "structured"
file_max_size: str = "10MB"
backup_count: int = 5
class Config:
"""配置管理器"""
def __init__(self, environment: Optional[str] = None):
"""初始化配置管理器
Args:
environment: 环境名称(development/production)
"""
self.environment = environment or os.getenv("ENVIRONMENT", "development")
self._config_data = self._load_config()
# 初始化各个配置模块
self.app = AppConfig(**self._config_data.get("app", {}))
# OSS配置 - 直接创建实例以支持环境变量覆盖
self.oss = OSSConfig()
# DashScope配置 - 直接创建实例以支持环境变量覆盖
self.dashscope = DashScopeConfig()
self.task = TaskConfig(**self._config_data.get("task", {}))
self.logging = LoggingConfig(**self._config_data.get("logging", {}))
def _load_config(self) -> Dict[str, Any]:
"""加载配置文件"""
config_dir = Path(__file__).parent.parent.parent / "config" / "environments"
config_file = config_dir / f"{self.environment}.yaml"
if not config_file.exists():
raise FileNotFoundError(f"配置文件不存在: {config_file}")
with open(config_file, 'r', encoding='utf-8') as file:
return yaml.safe_load(file)
def get_project_root(self) -> Path:
"""获取项目根目录"""
return Path(__file__).parent.parent.parent
def get_logs_dir(self) -> Path:
"""获取日志目录"""
logs_dir = self.get_project_root() / "logs"
logs_dir.mkdir(exist_ok=True)
return logs_dir
def get_temp_dir(self) -> Path:
"""获取临时文件目录"""
temp_dir = self.get_project_root() / "temp"
temp_dir.mkdir(exist_ok=True)
return temp_dir
# 全局配置实例
config = Config()
def get_config() -> Config:
"""获取配置实例"""
return config
def reload_config(environment: Optional[str] = None) -> Config:
"""重新加载配置"""
global config
config = Config(environment)
return config |