Skip to content

Commit acea198

Browse files
committed
Create fastapi rest api
1 parent ec51738 commit acea198

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

tts_server.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import uvicorn
2+
# from typing import Any
3+
from fastapi import FastAPI, Request
4+
5+
from typing import Any, Dict, AnyStr, List, Union
6+
from melo.api import TTS
7+
from pydantic import BaseModel
8+
9+
10+
from typing import Dict
11+
12+
import json
13+
14+
15+
speed = 1.0
16+
device = 'cuda:0'
17+
18+
# text = "안녕하세요! 오늘은 날씨가 정말 좋네요."
19+
model = TTS(language='KR', device=device)
20+
21+
app = FastAPI()
22+
23+
24+
# from apitally.flask import ApitallyMiddleware
25+
#
26+
# app.wsgi_app = ApitallyMiddleware(
27+
# app,
28+
# client_id="65bd9a71-da0f-4bbc-8f07-5a64cac1e548",
29+
# env="prod", # or "dev"
30+
# )
31+
32+
33+
class TtsPostRequest(BaseModel):
34+
languageCode: str | None = None
35+
name: str| None = None
36+
text: str
37+
38+
# voice: dict[str, str]
39+
# input: dict[str, str]
40+
# audioconfig: dict[str, str]
41+
42+
43+
@app.get("/")
44+
async def root():
45+
return {"message": "Hello World"}
46+
47+
48+
@app.post("/api/synthesize")
49+
async def apifunction_generate_tts(request: Request):
50+
_json = await request.json()
51+
# => ('{"languageCode":"ko-KR","name":"ko-KR-Standard-B","text":"1번. 또 파이썬은 순수한 ''프로그램 언어로서의 기능 외에도 다른 언어로 쓰인 모듈들을 연결하는 접착제 언어로써 자주 이용된다. 실제 파이썬은 많은 상용 응용 ''프로그램에서 스크립트 언어로 채용되고 있다. 도움말 문서도 정리가 잘 되어 있으며, 유니코 드 문자열을 지원해서 다양한 언어의 문자 ' '처리에도 능하다."}')
52+
# get text
53+
text = _json['text']
54+
55+
print(text)
56+
57+
speaker_ids = model.hps.data.spk2id
58+
# # output_path = 'kr.wav'
59+
# # model.tts_to_file(data, speaker_ids['KR'], output_path, speed=speed)
60+
61+
response_data = model.tts_to_base64(text, speaker_ids['KR'], speed=speed)
62+
63+
64+
65+
66+
return response_data
67+
68+
69+
70+
71+
if __name__ == "__main__":
72+
uvicorn.run("main:app", host="127.0.0.1", port=5000, log_level="info")

0 commit comments

Comments
 (0)