11import uvicorn
22from fastapi import FastAPI , Request
3-
3+ # from fastapi.middleware.cors import CORSMiddleware
4+ from starlette .middleware .cors import CORSMiddleware
5+ from pprint import pprint
46from melo .api import TTS
5-
7+ import string
68
79speed = 1.0
810device = "cuda:0"
911model = TTS (language = "KR" , device = device )
12+ speaker_ids = model .hps .data .spk2id
1013
1114app = FastAPI ()
1215
16+ punc = ''';:@#$%^&*_-~※○●□■△▲◇◆▽▼→←↑↓↔↕↗↘↙↖↙↗↘↖↔↕─│┌┐└┘├┤┬┴┼━┃┏┓┗'''
17+
18+ # allowed_origins = [
19+ # "http://localhost.tiangolo.com",
20+ # "https://localhost.tiangolo.com",
21+ # "http://localhost",
22+ # "http://localhost:8080",
23+ # "https://localhost:44345/",
24+ # "https://localhost:44345",
25+ # ]
26+
27+ allowed_origins = ["*" ]
28+
29+ app .add_middleware (
30+ CORSMiddleware ,
31+ allow_credentials = True ,
32+ allow_methods = ["GET" , "POST" , "PUT" , "DELETE" ],
33+ allow_headers = ["X-Requested-With" , "Content-Type" ],
34+ allow_origins = allowed_origins ,
35+ )
36+
1337
1438# from apitally.flask import ApitallyMiddleware
1539#
@@ -28,9 +52,16 @@ async def root():
2852@app .post ("/api/synthesize" )
2953async def apifunction_generate_tts (request : Request ):
3054 _json = await request .json ()
55+ pprint (_json )
3156 text = _json ["text" ]
57+ print (text )
3258
33- speaker_ids = model .hps .data .spk2id
59+ for c in text :
60+ if c in punc :
61+ text = text .replace (c , "" )
62+
63+ text = text .replace ('\n ' , ' ' )
64+
3465 response_data = model .tts_to_base64 (text , speaker_ids ["KR" ], speed = speed )
3566 return response_data
3667
0 commit comments