You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
109 lines
3.3 KiB
109 lines
3.3 KiB
3 years ago
|
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
|
||
|
#
|
||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
# you may not use this file except in compliance with the License.
|
||
|
# You may obtain a copy of the License at
|
||
|
#
|
||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||
|
#
|
||
|
# Unless required by applicable law or agreed to in writing, software
|
||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
# See the License for the specific language governing permissions and
|
||
|
# limitations under the License.
|
||
3 years ago
|
import traceback
|
||
3 years ago
|
from typing import Union
|
||
3 years ago
|
from fastapi import APIRouter
|
||
|
|
||
3 years ago
|
#from paddlespeech.server.engine.tts.python.tts_engine import TTSEngine
|
||
|
from paddlespeech.server.engine.tts.paddleinference.tts_engine import TTSEngine
|
||
|
from paddlespeech.server.restful.request import TTSRequest
|
||
|
from paddlespeech.server.restful.response import ErrorResponse
|
||
|
from paddlespeech.server.restful.response import TTSResponse
|
||
|
from paddlespeech.server.utils.errors import ErrorCode
|
||
|
from paddlespeech.server.utils.errors import failed_response
|
||
|
from paddlespeech.server.utils.exception import ServerBaseException
|
||
3 years ago
|
|
||
3 years ago
|
router = APIRouter()
|
||
3 years ago
|
|
||
|
|
||
3 years ago
|
@router.get('/paddlespeech/tts/help')
|
||
|
def help():
|
||
|
"""help
|
||
3 years ago
|
|
||
3 years ago
|
Returns:
|
||
|
json: [description]
|
||
|
"""
|
||
3 years ago
|
response = {
|
||
3 years ago
|
"success": "True",
|
||
3 years ago
|
"code": 200,
|
||
3 years ago
|
"message": {
|
||
|
"global": "success"
|
||
|
},
|
||
|
"result": {
|
||
|
"description": "tts server",
|
||
|
"text": "sentence to be synthesized",
|
||
|
"audio": "the base64 of audio"
|
||
|
}
|
||
|
}
|
||
3 years ago
|
return response
|
||
3 years ago
|
|
||
|
|
||
3 years ago
|
@router.post(
|
||
|
"/paddlespeech/tts", response_model=Union[TTSResponse, ErrorResponse])
|
||
3 years ago
|
def tts(request_body: TTSRequest):
|
||
|
"""tts api
|
||
|
|
||
|
Args:
|
||
|
request_body (TTSRequest): [description]
|
||
|
|
||
|
Returns:
|
||
|
json: [description]
|
||
|
"""
|
||
|
# json to dict
|
||
|
item_dict = request_body.dict()
|
||
|
sentence = item_dict['text']
|
||
|
spk_id = item_dict['spk_id']
|
||
|
speed = item_dict['speed']
|
||
|
volume = item_dict['volume']
|
||
|
sample_rate = item_dict['sample_rate']
|
||
|
save_path = item_dict['save_path']
|
||
|
|
||
3 years ago
|
# Check parameters
|
||
|
if speed <=0 or speed > 3 or volume <=0 or volume > 3 or \
|
||
|
sample_rate not in [0, 16000, 8000] or \
|
||
3 years ago
|
(save_path is not None and save_path.endswith("pcm") == False and save_path.endswith("wav") == False):
|
||
3 years ago
|
return failed_response(ErrorCode.SERVER_PARAM_ERR)
|
||
|
|
||
3 years ago
|
# single
|
||
|
tts_engine = TTSEngine()
|
||
|
|
||
3 years ago
|
# run
|
||
|
try:
|
||
|
lang, target_sample_rate, wav_base64 = tts_engine.run(
|
||
3 years ago
|
sentence, spk_id, speed, volume, sample_rate, save_path)
|
||
|
|
||
|
response = {
|
||
|
"success": True,
|
||
|
"code": 200,
|
||
|
"message": {
|
||
|
"description": "success."
|
||
|
},
|
||
|
"result": {
|
||
|
"lang": lang,
|
||
|
"spk_id": spk_id,
|
||
|
"speed": speed,
|
||
|
"volume": volume,
|
||
|
"sample_rate": target_sample_rate,
|
||
|
"save_path": save_path,
|
||
|
"audio": wav_base64
|
||
|
}
|
||
|
}
|
||
3 years ago
|
except ServerBaseException as e:
|
||
|
response = failed_response(e.error_code, e.msg)
|
||
|
except:
|
||
|
response = failed_response(ErrorCode.SERVER_UNKOWN_ERR)
|
||
|
traceback.print_exc()
|
||
3 years ago
|
|
||
3 years ago
|
return response
|