response.py 3.8 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 15
from typing import List

W
WilliamZhang06 已提交
16 17
from pydantic import BaseModel

L
lym0302 已提交
18
__all__ = ['ASRResponse', 'TTSResponse', 'CLSResponse']
W
WilliamZhang06 已提交
19 20 21 22 23


class Message(BaseModel):
    description: str

L
lym0302 已提交
24

W
WilliamZhang06 已提交
25 26 27 28 29 30
#****************************************************************************************/
#************************************ ASR response **************************************/
#****************************************************************************************/
class AsrResult(BaseModel):
    transcription: str

L
lym0302 已提交
31

W
WilliamZhang06 已提交
32 33 34 35 36 37 38 39
class ASRResponse(BaseModel):
    """
    response example
    {
        "success": true,
        "code": 0,
        "message": {
            "description": "success" 
L
lym0302 已提交
40
        },
W
WilliamZhang06 已提交
41 42 43 44 45 46 47 48 49 50
        "result": {
            "transcription": "你好,飞桨"
        }
    }
    """
    success: bool
    code: int
    message: Message
    result: AsrResult

L
lym0302 已提交
51

W
WilliamZhang06 已提交
52 53 54
#****************************************************************************************/
#************************************ TTS response **************************************/
#****************************************************************************************/
L
lym0302 已提交
55 56 57 58 59
class TTSResult(BaseModel):
    lang: str = "zh"
    spk_id: int = 0
    speed: float = 1.0
    volume: float = 1.0
L
lym0302 已提交
60 61
    sample_rate: int
    duration: float
L
lym0302 已提交
62 63 64 65 66 67 68 69 70
    save_path: str = None
    audio: str


class TTSResponse(BaseModel):
    """
    response example
    {
        "success": true,
L
liangym 已提交
71
        "code": 200,
L
lym0302 已提交
72 73 74 75 76
        "message": {
            "description": "success" 
        },
        "result": {
            "lang": "zh",
L
lym0302 已提交
77
            "spk_id": 0,
L
lym0302 已提交
78 79
            "speed": 1.0,
            "volume": 1.0,
L
lym0302 已提交
80 81
            "sample_rate": 24000,
            "duration": 3.6125,
L
lym0302 已提交
82 83 84 85 86 87 88 89 90
            "audio": "LTI1OTIuNjI1OTUwMzQsOTk2OS41NDk4...",
            "save_path": "./tts.wav"
        }
    }
    """
    success: bool
    code: int
    message: Message
    result: TTSResult
91 92


L
lym0302 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
#****************************************************************************************/
#************************************ CLS response **************************************/
#****************************************************************************************/
class CLSResults(BaseModel):
    class_name: str
    prob: float


class CLSResult(BaseModel):
    topk: int
    results: List[CLSResults]


class CLSResponse(BaseModel):
    """
    response example
    {
        "success": true,
        "code": 0,
        "message": {
            "description": "success" 
        },
        "result": {
            topk: 1
            results: [
            {
                "class":"Speech",
                "prob": 0.9027184844017029
            }
            ]
        }
    }
    """
    success: bool
    code: int
    message: Message
    result: CLSResult


132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
#****************************************************************************************/
#********************************** Error response **************************************/
#****************************************************************************************/
class ErrorResponse(BaseModel):
    """
    response example
    {
        "success": false,
        "code": 0,
        "message": {
            "description": "Unknown error occurred."
        }
    }
    """
    success: bool
    code: int
    message: Message