AndrewKapok commited on
Commit
7c5d00f
·
verified ·
1 Parent(s): bd0b304

Upload config/config_loader.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. config/config_loader.py +142 -0
config/config_loader.py ADDED
@@ -0,0 +1,142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import yaml
3
+ import logging
4
+ from typing import Dict, Any, Optional
5
+ from pydantic_settings import BaseSettings
6
+ from dotenv import load_dotenv
7
+
8
+ logger = logging.getLogger(__name__)
9
+
10
+ # 加载环境变量
11
+ load_dotenv()
12
+
13
+ class AppSettings(BaseSettings):
14
+ """
15
+ 应用配置类
16
+ """
17
+ # 模型配置
18
+ model_name: str = os.getenv("MODEL_NAME", "qwen/qwen-image-2512")
19
+ model_revision: str = os.getenv("MODEL_REVISION", "main")
20
+ model_dir: str = os.getenv("MODEL_DIR", "./models")
21
+ cache_dir: Optional[str] = os.getenv("CACHE_DIR")
22
+
23
+ # 推理配置
24
+ device: str = os.getenv("DEVICE", "cpu")
25
+ max_memory_cpu: str = os.getenv("MAX_MEMORY_CPU", "16GB")
26
+ num_threads: int = int(os.getenv("NUM_THREADS", "4"))
27
+ num_interop_threads: int = int(os.getenv("NUM_INTEROP_THREADS", "2"))
28
+
29
+ # API配置
30
+ request_limit: int = int(os.getenv("REQUEST_LIMIT", "100"))
31
+ request_window: int = int(os.getenv("REQUEST_WINDOW", "60"))
32
+
33
+ # 日志配置
34
+ log_level: str = os.getenv("LOG_LEVEL", "info")
35
+ log_format: str = os.getenv("LOG_FORMAT", "%(asctime)s - %(name)s - %(levelname)s - %(message)s")
36
+
37
+ class Config:
38
+ case_sensitive = False
39
+ env_file = ".env"
40
+
41
+ def load_config(config_path: str = "config.yaml") -> Dict[str, Any]:
42
+ """
43
+ 加载YAML配置文件
44
+
45
+ Args:
46
+ config_path: 配置文件路径
47
+
48
+ Returns:
49
+ Dict[str, Any]: 配置字典
50
+ """
51
+ if not os.path.exists(config_path):
52
+ logger.warning(f"配置文件不存在: {config_path},使用默认配置")
53
+ return {}
54
+
55
+ try:
56
+ with open(config_path, "r", encoding="utf-8") as f:
57
+ config = yaml.safe_load(f)
58
+
59
+ logger.info(f"配置文件加载成功: {config_path}")
60
+ return config
61
+
62
+ except Exception as e:
63
+ logger.error(f"配置文件加载失败: {str(e)},使用默认配置")
64
+ return {}
65
+
66
+ def get_settings() -> AppSettings:
67
+ """
68
+ 获取应用设置
69
+
70
+ Returns:
71
+ AppSettings: 应用设置实例
72
+ """
73
+ return AppSettings()
74
+
75
+ def merge_configs(default_config: Dict[str, Any], user_config: Dict[str, Any]) -> Dict[str, Any]:
76
+ """
77
+ 合并配置
78
+
79
+ Args:
80
+ default_config: 默认配置
81
+ user_config: 用户配置
82
+
83
+ Returns:
84
+ Dict[str, Any]: 合并后的配置
85
+ """
86
+ merged = default_config.copy()
87
+
88
+ for key, value in user_config.items():
89
+ if key in merged and isinstance(merged[key], dict) and isinstance(value, dict):
90
+ merged[key] = merge_configs(merged[key], value)
91
+ else:
92
+ merged[key] = value
93
+
94
+ return merged
95
+
96
+ # 默认配置
97
+ DEFAULT_CONFIG = {
98
+ "model": {
99
+ "name": "qwen/qwen-image-2512",
100
+ "revision": "main",
101
+ "dir": "./models",
102
+ "cache_dir": "~/.cache/huggingface/hub",
103
+ "verify_integrity": True
104
+ },
105
+ "inference": {
106
+ "device": "cpu",
107
+ "dtype": "float32",
108
+ "max_memory": {
109
+ "cpu": "16GB"
110
+ },
111
+ "num_threads": 4,
112
+ "num_interop_threads": 2,
113
+ "enable_attention_slicing": True,
114
+ "enable_sequential_cpu_offload": True,
115
+ "low_cpu_mem_usage": True
116
+ },
117
+ "api": {
118
+ "host": "0.0.0.0",
119
+ "port": 7860,
120
+ "workers": 1,
121
+ "docs_url": "/docs",
122
+ "redoc_url": "/redoc",
123
+ "request_limit": 100,
124
+ "request_window": 60,
125
+ "cors_origins": ["*"]
126
+ },
127
+ "ui": {
128
+ "title": "Qwen-Image-2512 文本到图像生成",
129
+ "theme": "soft",
130
+ "max_images": 4
131
+ },
132
+ "logging": {
133
+ "level": "info",
134
+ "format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
135
+ }
136
+ }
137
+
138
+ # 加载配置
139
+ CONFIG = merge_configs(DEFAULT_CONFIG, load_config())
140
+
141
+ # 获取设置实例
142
+ SETTINGS = get_settings()