Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
TensorSpeech
GitHub Repository: TensorSpeech/TensorFlowTTS
Path: blob/master/tensorflow_tts/inference/auto_processor.py
1558 views
1
# -*- coding: utf-8 -*-
2
# Copyright 2020 The TensorFlowTTS Team.
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 Processor modules."""
16
17
import logging
18
import json
19
import os
20
from collections import OrderedDict
21
22
from tensorflow_tts.processor import (
23
LJSpeechProcessor,
24
KSSProcessor,
25
BakerProcessor,
26
LibriTTSProcessor,
27
ThorstenProcessor,
28
LJSpeechUltimateProcessor,
29
SynpaflexProcessor,
30
JSUTProcessor,
31
)
32
33
from tensorflow_tts.utils import CACHE_DIRECTORY, PROCESSOR_FILE_NAME, LIBRARY_NAME
34
from tensorflow_tts import __version__ as VERSION
35
from huggingface_hub import hf_hub_url, cached_download
36
37
CONFIG_MAPPING = OrderedDict(
38
[
39
("LJSpeechProcessor", LJSpeechProcessor),
40
("KSSProcessor", KSSProcessor),
41
("BakerProcessor", BakerProcessor),
42
("LibriTTSProcessor", LibriTTSProcessor),
43
("ThorstenProcessor", ThorstenProcessor),
44
("LJSpeechUltimateProcessor", LJSpeechUltimateProcessor),
45
("SynpaflexProcessor", SynpaflexProcessor),
46
("JSUTProcessor", JSUTProcessor),
47
]
48
)
49
50
51
class AutoProcessor:
52
def __init__(self):
53
raise EnvironmentError(
54
"AutoProcessor is designed to be instantiated "
55
"using the `AutoProcessor.from_pretrained(pretrained_path)` method."
56
)
57
58
@classmethod
59
def from_pretrained(cls, pretrained_path, **kwargs):
60
# load weights from hf hub
61
if not os.path.isfile(pretrained_path):
62
# retrieve correct hub url
63
download_url = hf_hub_url(repo_id=pretrained_path, filename=PROCESSOR_FILE_NAME)
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
with open(pretrained_path, "r") as f:
74
config = json.load(f)
75
76
try:
77
processor_name = config["processor_name"]
78
processor_class = CONFIG_MAPPING[processor_name]
79
processor_class = processor_class(
80
data_dir=None, loaded_mapper_path=pretrained_path
81
)
82
return processor_class
83
except Exception:
84
raise ValueError(
85
"Unrecognized processor in {}. "
86
"Should have a `processor_name` key in its config.json, or contain one of the following strings "
87
"in its name: {}".format(
88
pretrained_path, ", ".join(CONFIG_MAPPING.keys())
89
)
90
)
91
92