Skip to content

Commit fd242ec

Browse files
committed
Add cors middleware, remove semicolons and other symbols
1 parent e77d616 commit fd242ec

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

tts_server.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,39 @@
11
import uvicorn
22
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
46
from melo.api import TTS
5-
7+
import string
68

79
speed = 1.0
810
device = "cuda:0"
911
model = TTS(language="KR", device=device)
12+
speaker_ids = model.hps.data.spk2id
1013

1114
app = 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")
2953
async 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

Comments
 (0)