Path: blob/master/tensorflow_tts/inference/auto_processor.py
1558 views
# -*- coding: utf-8 -*-1# Copyright 2020 The TensorFlowTTS Team.2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.14"""Tensorflow Auto Processor modules."""1516import logging17import json18import os19from collections import OrderedDict2021from tensorflow_tts.processor import (22LJSpeechProcessor,23KSSProcessor,24BakerProcessor,25LibriTTSProcessor,26ThorstenProcessor,27LJSpeechUltimateProcessor,28SynpaflexProcessor,29JSUTProcessor,30)3132from tensorflow_tts.utils import CACHE_DIRECTORY, PROCESSOR_FILE_NAME, LIBRARY_NAME33from tensorflow_tts import __version__ as VERSION34from huggingface_hub import hf_hub_url, cached_download3536CONFIG_MAPPING = OrderedDict(37[38("LJSpeechProcessor", LJSpeechProcessor),39("KSSProcessor", KSSProcessor),40("BakerProcessor", BakerProcessor),41("LibriTTSProcessor", LibriTTSProcessor),42("ThorstenProcessor", ThorstenProcessor),43("LJSpeechUltimateProcessor", LJSpeechUltimateProcessor),44("SynpaflexProcessor", SynpaflexProcessor),45("JSUTProcessor", JSUTProcessor),46]47)484950class AutoProcessor:51def __init__(self):52raise EnvironmentError(53"AutoProcessor is designed to be instantiated "54"using the `AutoProcessor.from_pretrained(pretrained_path)` method."55)5657@classmethod58def from_pretrained(cls, pretrained_path, **kwargs):59# load weights from hf hub60if not os.path.isfile(pretrained_path):61# retrieve correct hub url62download_url = hf_hub_url(repo_id=pretrained_path, filename=PROCESSOR_FILE_NAME)6364pretrained_path = str(65cached_download(66url=download_url,67library_name=LIBRARY_NAME,68library_version=VERSION,69cache_dir=CACHE_DIRECTORY,70)71)72with open(pretrained_path, "r") as f:73config = json.load(f)7475try:76processor_name = config["processor_name"]77processor_class = CONFIG_MAPPING[processor_name]78processor_class = processor_class(79data_dir=None, loaded_mapper_path=pretrained_path80)81return processor_class82except Exception:83raise ValueError(84"Unrecognized processor in {}. "85"Should have a `processor_name` key in its config.json, or contain one of the following strings "86"in its name: {}".format(87pretrained_path, ", ".join(CONFIG_MAPPING.keys())88)89)909192