lenML/ChatTTS-Forge
301
1import argparse2import logging3 4from fastapi import FastAPI5 6from modules import config7from modules.api.Api import APIManager8from modules.api.impl import (9 google_api,10 models_api,11 openai_api,12 ping_api,13 refiner_api,14 speaker_api,15 ssml_api,16 style_api,17 tts_api,18 xtts_v2_api,19)20from modules.utils import env21 22logger = logging.getLogger(__name__)23 24 25def create_api(app: FastAPI, exclude=[]):26 app_mgr = APIManager(app=app, exclude_patterns=exclude)27 28 ping_api.setup(app_mgr)29 models_api.setup(app_mgr)30 style_api.setup(app_mgr)31 speaker_api.setup(app_mgr)32 tts_api.setup(app_mgr)33 ssml_api.setup(app_mgr)34 google_api.setup(app_mgr)35 openai_api.setup(app_mgr)36 refiner_api.setup(app_mgr)37 xtts_v2_api.setup(app_mgr)38 39 return app_mgr40 41 42def setup_api_args(parser: argparse.ArgumentParser):43 parser.add_argument(44 "--cors_origin",45 type=str,46 help="Allowed CORS origins. Use '*' to allow all origins.",47 )48 parser.add_argument(49 "--no_playground",50 action="store_true",51 help="Disable the playground entry",52 )53 parser.add_argument(54 "--no_docs",55 action="store_true",56 help="Disable the documentation entry",57 )58 # 配置哪些api要跳过 比如 exclude="/v1/speakers/*,/v1/tts/*"59 parser.add_argument(60 "--exclude",61 type=str,62 help="Exclude the specified API from the server",63 )64 65 66def process_api_args(args: argparse.Namespace, app: FastAPI):67 cors_origin = env.get_and_update_env(args, "cors_origin", "*", str)68 no_playground = env.get_and_update_env(args, "no_playground", False, bool)69 no_docs = env.get_and_update_env(args, "no_docs", False, bool)70 exclude = env.get_and_update_env(args, "exclude", "", str)71 72 api = create_api(app=app, exclude=exclude.split(","))73 config.api = api74 75 if cors_origin:76 api.set_cors(allow_origins=[cors_origin])77 78 if not no_playground:79 api.setup_playground()80 81 if compile:82 logger.info("Model compile is enabled")83 