1
1
import uvicorn
2
2
from fastapi import FastAPI , Request
3
-
3
+ # from fastapi.middleware.cors import CORSMiddleware
4
+ from starlette .middleware .cors import CORSMiddleware
5
+ from pprint import pprint
4
6
from melo .api import TTS
5
-
7
+ import string
6
8
7
9
speed = 1.0
8
10
device = "cuda:0"
9
11
model = TTS (language = "KR" , device = device )
12
+ speaker_ids = model .hps .data .spk2id
10
13
11
14
app = FastAPI ()
12
15
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
+
13
37
14
38
# from apitally.flask import ApitallyMiddleware
15
39
#
@@ -28,9 +52,16 @@ async def root():
28
52
@app .post ("/api/synthesize" )
29
53
async def apifunction_generate_tts (request : Request ):
30
54
_json = await request .json ()
55
+ pprint (_json )
31
56
text = _json ["text" ]
57
+ print (text )
32
58
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
+
34
65
response_data = model .tts_to_base64 (text , speaker_ids ["KR" ], speed = speed )
35
66
return response_data
36
67
0 commit comments