Spaces:
Running
Running
File size: 3,359 Bytes
01e655b 02e90e4 01e655b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
from fastapi import FastAPI, APIRouter
from fastapi.middleware.cors import CORSMiddleware
import logging
from fastapi.staticfiles import StaticFiles
import fnmatch
def is_excluded(path, exclude_patterns):
"""
检查路径是否被排除
:param path: 需要检查的路径
:param exclude_patterns: 包含通配符的排除路径列表
:return: 如果路径被排除,返回 True;否则返回 False
"""
for pattern in exclude_patterns:
if fnmatch.fnmatch(path, pattern):
print(path, pattern)
return True
return False
class APIManager:
def __init__(self, no_docs=False, exclude_patterns=[]):
self.app = FastAPI(
title="ChatTTS Forge API",
description="""
ChatTTS-Forge 是一个功能强大的文本转语音生成工具,支持通过类 SSML 语法生成丰富的音频长文本,并提供全面的 API 服务,适用于各种场景。<br/>
ChatTTS-Forge is a powerful text-to-speech generation tool that supports generating rich audio long texts through class SSML syntax
项目地址: [https://github.com/lenML/ChatTTS-Forge](https://github.com/lenML/ChatTTS-Forge)
> 所有生成音频的 POST api都无法在此页面调试,调试建议使用 playground <br/>
> All audio generation POST APIs cannot be debugged on this page, it is recommended to use playground for debugging
> 如果你不熟悉本系统,建议从这个一键脚本开始,在colab中尝试一下:<br/>
> [https://colab.research.google.com/github/lenML/ChatTTS-Forge/blob/main/colab.ipynb](https://colab.research.google.com/github/lenML/ChatTTS-Forge/blob/main/colab.ipynb)
""",
version="0.1.0",
redoc_url=None if no_docs else "/redoc",
docs_url=None if no_docs else "/docs",
)
self.registered_apis = {}
self.logger = logging.getLogger(__name__)
self.exclude = exclude_patterns
def is_excluded(self, path):
return is_excluded(path, self.exclude)
def set_cors(
self,
allow_origins: list = ["*"],
allow_credentials: bool = True,
allow_methods: list = ["*"],
allow_headers: list = ["*"],
):
self.app.add_middleware(
CORSMiddleware,
allow_origins=allow_origins,
allow_credentials=allow_credentials,
allow_methods=allow_methods,
allow_headers=allow_headers,
)
def setup_playground(self):
app = self.app
app.mount(
"/playground",
StaticFiles(directory="playground", html=True),
name="playground",
)
def get(self, path: str, **kwargs):
def decorator(func):
if self.is_excluded(path):
return func
self.app.get(path, **kwargs)(func)
self.registered_apis[path] = func
self.logger.info(f"Registered API: GET {path}")
return func
return decorator
def post(self, path: str, **kwargs):
def decorator(func):
if self.is_excluded(path):
return func
self.app.post(path, **kwargs)(func)
self.registered_apis[path] = func
self.logger.info(f"Registered API: POST {path}")
return func
return decorator
|