Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
TensorSpeech
GitHub Repository: TensorSpeech/TensorFlowTTS
Path: blob/master/tensorflow_tts/inference/auto_config.py
1558 views
1
# -*- coding: utf-8 -*-
2
# Copyright 2020 The HuggingFace Inc. team and Minh Nguyen (@dathudeptrai)
3
#
4
# Licensed under the Apache License, Version 2.0 (the "License");
5
# you may not use this file except in compliance with the License.
6
# You may obtain a copy of the License at
7
#
8
# http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
"""Tensorflow Auto Config modules."""
16
17
import logging
18
import yaml
19
import os
20
from collections import OrderedDict
21
22
from tensorflow_tts.configs import (
23
FastSpeechConfig,
24
FastSpeech2Config,
25
MelGANGeneratorConfig,
26
MultiBandMelGANGeneratorConfig,
27
HifiGANGeneratorConfig,
28
Tacotron2Config,
29
ParallelWaveGANGeneratorConfig,
30
)
31
32
from tensorflow_tts.utils import CACHE_DIRECTORY, CONFIG_FILE_NAME, LIBRARY_NAME
33
from tensorflow_tts import __version__ as VERSION
34
from huggingface_hub import hf_hub_url, cached_download
35
36
CONFIG_MAPPING = OrderedDict(
37
[
38
("fastspeech", FastSpeechConfig),
39
("fastspeech2", FastSpeech2Config),
40
("multiband_melgan_generator", MultiBandMelGANGeneratorConfig),
41
("melgan_generator", MelGANGeneratorConfig),
42
("hifigan_generator", HifiGANGeneratorConfig),
43
("tacotron2", Tacotron2Config),
44
("parallel_wavegan_generator", ParallelWaveGANGeneratorConfig),
45
]
46
)
47
48
49
class AutoConfig:
50
def __init__(self):
51
raise EnvironmentError(
52
"AutoConfig is designed to be instantiated "
53
"using the `AutoConfig.from_pretrained(pretrained_path)` method."
54
)
55
56
@classmethod
57
def from_pretrained(cls, pretrained_path, **kwargs):
58
# load weights from hf hub
59
if not os.path.isfile(pretrained_path):
60
# retrieve correct hub url
61
download_url = hf_hub_url(
62
repo_id=pretrained_path, filename=CONFIG_FILE_NAME
63
)
64
65
pretrained_path = str(
66
cached_download(
67
url=download_url,
68
library_name=LIBRARY_NAME,
69
library_version=VERSION,
70
cache_dir=CACHE_DIRECTORY,
71
)
72
)
73
74
with open(pretrained_path) as f:
75
config = yaml.load(f, Loader=yaml.Loader)
76
77
try:
78
model_type = config["model_type"]
79
config_class = CONFIG_MAPPING[model_type]
80
config_class = config_class(**config[model_type + "_params"], **kwargs)
81
config_class.set_config_params(config)
82
return config_class
83
except Exception:
84
raise ValueError(
85
"Unrecognized config in {}. "
86
"Should have a `model_type` key in its config.yaml, or contain one of the following strings "
87
"in its name: {}".format(
88
pretrained_path, ", ".join(CONFIG_MAPPING.keys())
89
)
90
)
91
92