tts_api.py 3.3 KB
Newer Older
W
WilliamZhang06 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
# 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.
L
lym0302 已提交
14
import traceback
15
from typing import Union
W
WilliamZhang06 已提交
16 17
from fastapi import APIRouter

L
lym0302 已提交
18 19 20 21 22 23 24 25
#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
W
WilliamZhang06 已提交
26

W
WilliamZhang06 已提交
27
router = APIRouter()
W
WilliamZhang06 已提交
28 29


W
WilliamZhang06 已提交
30 31 32
@router.get('/paddlespeech/tts/help')
def help():
    """help
W
WilliamZhang06 已提交
33

W
WilliamZhang06 已提交
34 35 36
    Returns:
        json: [description]
    """
L
lym0302 已提交
37
    response = {
L
lym0302 已提交
38
        "success": "True",
L
lym0302 已提交
39
        "code": 200,
L
lym0302 已提交
40 41 42 43 44 45 46 47 48
        "message": {
            "global": "success"
        },
        "result": {
            "description": "tts server",
            "text": "sentence to be synthesized",
            "audio": "the base64 of audio"
        }
    }
L
lym0302 已提交
49
    return response
W
WilliamZhang06 已提交
50 51


52 53
@router.post(
    "/paddlespeech/tts", response_model=Union[TTSResponse, ErrorResponse])
L
lym0302 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
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']

L
lym0302 已提交
72 73 74
    # Check parameters
    if speed <=0 or speed > 3 or volume <=0 or volume > 3 or \
        sample_rate not in [0, 16000, 8000] or \
L
lym0302 已提交
75
        (save_path is not None and save_path.endswith("pcm") == False and save_path.endswith("wav") == False):
L
lym0302 已提交
76 77
        return failed_response(ErrorCode.SERVER_PARAM_ERR)

L
lym0302 已提交
78 79 80
    # single
    tts_engine = TTSEngine()

L
lym0302 已提交
81 82 83
    # run
    try:
        lang, target_sample_rate, wav_base64 = tts_engine.run(
L
lym0302 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
            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
            }
        }
L
lym0302 已提交
102 103 104 105 106
    except ServerBaseException as e:
        response = failed_response(e.error_code, e.msg)
    except:
        response = failed_response(ErrorCode.SERVER_UNKOWN_ERR)
        traceback.print_exc()
L
lym0302 已提交
107

L
lym0302 已提交
108
    return response