main.py 1.8 KB
Newer Older
W
WilliamZhang06 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# 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.
import argparse
L
lym0302 已提交
15

L
lym0302 已提交
16 17
import uvicorn
from fastapi import FastAPI
18

19
from paddlespeech.server.engine.engine_pool import init_engine_pool
L
lym0302 已提交
20 21
from paddlespeech.server.restful.api import setup_router
from paddlespeech.server.utils.config import get_config
L
lym0302 已提交
22 23 24

app = FastAPI(
    title="PaddleSpeech Serving API", description="Api", version="0.0.1")
W
WilliamZhang06 已提交
25 26


27
def init(config):
28 29 30 31 32 33 34
    """system initialization

    Args:
        config (CfgNode): config object

    Returns:
        bool: 
W
WilliamZhang06 已提交
35
    """
36 37 38
    # init api
    api_list = list(config.engine_backend)
    api_router = setup_router(api_list)
L
lym0302 已提交
39 40
    app.include_router(api_router)

41 42
    if not init_engine_pool(config):
        return False
L
lym0302 已提交
43 44

    return True
W
WilliamZhang06 已提交
45 46 47


def main(args):
48
    """main function"""
W
WilliamZhang06 已提交
49

50
    config = get_config(args.config_file)
W
WilliamZhang06 已提交
51

52
    if init(config):
L
lym0302 已提交
53
        uvicorn.run(app, host=config.host, port=config.port, debug=True)
W
WilliamZhang06 已提交
54 55 56 57


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
L
lym0302 已提交
58 59 60 61
    parser.add_argument(
        "--config_file",
        action="store",
        help="yaml file of the app",
62
        default="./conf/application.yaml")
L
liangym 已提交
63

L
lym0302 已提交
64 65 66 67 68
    parser.add_argument(
        "--log_file",
        action="store",
        help="log file",
        default="./log/paddlespeech.log")
W
WilliamZhang06 已提交
69 70 71
    args = parser.parse_args()

    main(args)