JotunnBurton commited on
Commit
6f67c88
·
verified ·
1 Parent(s): 53d08de

Upload config.py

Browse files
Files changed (1) hide show
  1. config.py +250 -0
config.py ADDED
@@ -0,0 +1,250 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ @Desc: 全局配置文件读取
3
+ """
4
+ import argparse
5
+ import yaml
6
+ from typing import Dict, List
7
+ import os
8
+ import shutil
9
+ import sys
10
+
11
+
12
+ class Resample_config:
13
+ """重采样配置"""
14
+
15
+ def __init__(self, in_dir: str, out_dir: str, sampling_rate: int = 44100):
16
+ self.sampling_rate: int = sampling_rate # 目标采样率
17
+ self.in_dir: str = in_dir # 待处理音频目录路径
18
+ self.out_dir: str = out_dir # 重采样输出路径
19
+
20
+ @classmethod
21
+ def from_dict(cls, dataset_path: str, data: Dict[str, any]):
22
+ """从字典中生成实例"""
23
+
24
+ # 不检查路径是否有效,此逻辑在resample.py中处理
25
+ data["in_dir"] = os.path.join(dataset_path, data["in_dir"])
26
+ data["out_dir"] = os.path.join(dataset_path, data["out_dir"])
27
+
28
+ return cls(**data)
29
+
30
+
31
+ class Preprocess_text_config:
32
+ """数据预处理配置"""
33
+
34
+ def __init__(
35
+ self,
36
+ transcription_path: str,
37
+ cleaned_path: str,
38
+ train_path: str,
39
+ val_path: str,
40
+ config_path: str,
41
+ val_per_lang: int = 5,
42
+ max_val_total: int = 10000,
43
+ clean: bool = True,
44
+ ):
45
+ self.transcription_path: str = transcription_path # 原始文本文件路径,文本格式应为{wav_path}|{speaker_name}|{language}|{text}。
46
+ self.cleaned_path: str = cleaned_path # 数据清洗后文本路径,可以不填。不填则将在原始文本目录生成
47
+ self.train_path: str = train_path # 训练集路径,可以不填。不填则将在原始文本目录生成
48
+ self.val_path: str = val_path # 验证集路径,可以不填。不填则将在原始文本目录生成
49
+ self.config_path: str = config_path # 配置文件路径
50
+ self.val_per_lang: int = val_per_lang # 每个speaker的验证集条数
51
+ self.max_val_total: int = max_val_total # 验证集最大条数,多于的会被截断并放到训练集中
52
+ self.clean: bool = clean # 是否进行数据清洗
53
+
54
+ @classmethod
55
+ def from_dict(cls, dataset_path: str, data: Dict[str, any]):
56
+ """从字典中生成实例"""
57
+
58
+ data["transcription_path"] = os.path.join(
59
+ dataset_path, data["transcription_path"]
60
+ )
61
+ if data["cleaned_path"] == "" or data["cleaned_path"] is None:
62
+ data["cleaned_path"] = None
63
+ else:
64
+ data["cleaned_path"] = os.path.join(dataset_path, data["cleaned_path"])
65
+ data["train_path"] = os.path.join(dataset_path, data["train_path"])
66
+ data["val_path"] = os.path.join(dataset_path, data["val_path"])
67
+ data["config_path"] = os.path.join(dataset_path, data["config_path"])
68
+
69
+ return cls(**data)
70
+
71
+
72
+ class Bert_gen_config:
73
+ """bert_gen 配置"""
74
+
75
+ def __init__(
76
+ self,
77
+ config_path: str,
78
+ num_processes: int = 2,
79
+ device: str = "cuda",
80
+ use_multi_device: bool = False,
81
+ ):
82
+ self.config_path = config_path
83
+ self.num_processes = num_processes
84
+ self.device = device
85
+ self.use_multi_device = use_multi_device
86
+
87
+ @classmethod
88
+ def from_dict(cls, dataset_path: str, data: Dict[str, any]):
89
+ data["config_path"] = os.path.join(dataset_path, data["config_path"])
90
+
91
+ return cls(**data)
92
+
93
+
94
+ class Emo_gen_config:
95
+ """emo_gen 配置"""
96
+
97
+ def __init__(
98
+ self,
99
+ config_path: str,
100
+ num_processes: int = 2,
101
+ device: str = "cuda",
102
+ use_multi_device: bool = False,
103
+ ):
104
+ self.config_path = config_path
105
+ self.num_processes = num_processes
106
+ self.device = device
107
+ self.use_multi_device = use_multi_device
108
+
109
+ @classmethod
110
+ def from_dict(cls, dataset_path: str, data: Dict[str, any]):
111
+ data["config_path"] = os.path.join(dataset_path, data["config_path"])
112
+
113
+ return cls(**data)
114
+
115
+
116
+ class Train_ms_config:
117
+ """训练配置"""
118
+
119
+ def __init__(
120
+ self,
121
+ config_path: str,
122
+ env: Dict[str, any],
123
+ base: Dict[str, any],
124
+ model: str,
125
+ num_workers: int,
126
+ spec_cache: bool,
127
+ keep_ckpts: int,
128
+ ):
129
+ self.env = env # 需要加载的环境变量
130
+ self.base = base # 底模配置
131
+ self.model = model # 训练模型存储目录,该路径为相对于dataset_path的路径,而非项目根目录
132
+ self.config_path = config_path # 配置文件路径
133
+ self.num_workers = num_workers # worker数量
134
+ self.spec_cache = spec_cache # 是否启用spec缓存
135
+ self.keep_ckpts = keep_ckpts # ckpt数量
136
+
137
+ @classmethod
138
+ def from_dict(cls, dataset_path: str, data: Dict[str, any]):
139
+ # data["model"] = os.path.join(dataset_path, data["model"])
140
+ data["config_path"] = os.path.join(dataset_path, data["config_path"])
141
+
142
+ return cls(**data)
143
+
144
+
145
+ class Webui_config:
146
+ """webui 配置"""
147
+
148
+ def __init__(
149
+ self,
150
+ device: str,
151
+ model: str,
152
+ config_path: str,
153
+ language_identification_library: str,
154
+ port: int = 7860,
155
+ share: bool = False,
156
+ debug: bool = False,
157
+ fp16_run: bool = False,
158
+ ):
159
+ self.device: str = device
160
+ self.model: str = model # 端口号
161
+ self.config_path: str = config_path # 是否公开部署,对外网开放
162
+ self.port: int = port # 是否开启debug模式
163
+ self.share: bool = share # 模型路径
164
+ self.debug: bool = debug # 配置文件路径
165
+ self.fp16_run: bool = fp16_run # 16bit加载bert和clap
166
+ self.language_identification_library: str = (
167
+ language_identification_library # 语种识别库
168
+ )
169
+
170
+ @classmethod
171
+ def from_dict(cls, dataset_path: str, data: Dict[str, any]):
172
+ data["config_path"] = os.path.join(dataset_path, data["config_path"])
173
+ data["model"] = os.path.join(dataset_path, data["model"])
174
+ return cls(**data)
175
+
176
+
177
+ class Server_config:
178
+ def __init__(
179
+ self, models: List[Dict[str, any]], port: int = 5000, device: str = "cuda"
180
+ ):
181
+ self.models: List[Dict[str, any]] = models # 需要加载的所有模型的配置
182
+ self.port: int = port # 端口号
183
+ self.device: str = device # 模型默认使用设备
184
+
185
+ @classmethod
186
+ def from_dict(cls, data: Dict[str, any]):
187
+ return cls(**data)
188
+
189
+
190
+ class Translate_config:
191
+ """翻译api配置"""
192
+
193
+ def __init__(self, app_key: str, secret_key: str):
194
+ self.app_key = app_key
195
+ self.secret_key = secret_key
196
+
197
+ @classmethod
198
+ def from_dict(cls, data: Dict[str, any]):
199
+ return cls(**data)
200
+
201
+
202
+ class Config:
203
+ def __init__(self, config_path: str):
204
+ if not os.path.isfile(config_path) and os.path.isfile("default_config.yml"):
205
+ shutil.copy(src="default_config.yml", dst=config_path)
206
+ print(
207
+ f"已根据默认配置文件default_config.yml生成配置文件{config_path}。请按该配置文件的说明进行配置后重新运行。"
208
+ )
209
+ print("如无特殊需求,请勿修改default_config.yml或备份该文件。")
210
+ sys.exit(0)
211
+ with open(file=config_path, mode="r", encoding="utf-8") as file:
212
+ yaml_config: Dict[str, any] = yaml.safe_load(file.read())
213
+ dataset_path: str = yaml_config["dataset_path"]
214
+ openi_token: str = yaml_config["openi_token"]
215
+ self.dataset_path: str = dataset_path
216
+ self.mirror: str = yaml_config["mirror"]
217
+ self.openi_token: str = openi_token
218
+ self.resample_config: Resample_config = Resample_config.from_dict(
219
+ dataset_path, yaml_config["resample"]
220
+ )
221
+ self.preprocess_text_config: Preprocess_text_config = (
222
+ Preprocess_text_config.from_dict(
223
+ dataset_path, yaml_config["preprocess_text"]
224
+ )
225
+ )
226
+ self.bert_gen_config: Bert_gen_config = Bert_gen_config.from_dict(
227
+ dataset_path, yaml_config["bert_gen"]
228
+ )
229
+ self.emo_gen_config: Emo_gen_config = Emo_gen_config.from_dict(
230
+ dataset_path, yaml_config["emo_gen"]
231
+ )
232
+ self.train_ms_config: Train_ms_config = Train_ms_config.from_dict(
233
+ dataset_path, yaml_config["train_ms"]
234
+ )
235
+ self.webui_config: Webui_config = Webui_config.from_dict(
236
+ dataset_path, yaml_config["webui"]
237
+ )
238
+ self.server_config: Server_config = Server_config.from_dict(
239
+ yaml_config["server"]
240
+ )
241
+ self.translate_config: Translate_config = Translate_config.from_dict(
242
+ yaml_config["translate"]
243
+ )
244
+
245
+
246
+ parser = argparse.ArgumentParser()
247
+ # 为避免与以前的config.json起冲突,将其更名如下
248
+ parser.add_argument("-y", "--yml_config", type=str, default="config.yml")
249
+ args, _ = parser.parse_known_args()
250
+ config = Config(args.yml_config)