forked from Ksuriuri/index-tts-vllm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_server_modified.py
More file actions
243 lines (206 loc) · 7.47 KB
/
Copy pathapi_server_modified.py
File metadata and controls
243 lines (206 loc) · 7.47 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
""" This API server support IndexTTS-1/1.5 """
import os
import asyncio
import io
import traceback
import base64
import hashlib
from pathlib import Path
from fastapi import FastAPI, Request, Response
from fastapi.responses import JSONResponse, StreamingResponse
from contextlib import asynccontextmanager
from fastapi.middleware.cors import CORSMiddleware
import uvicorn
import argparse
import json
import asyncio
import time
import numpy as np
import soundfile as sf
from indextts.infer_vllm import IndexTTS
tts = None
TEMP_AUDIO_DIR = Path("temp_prompt_audio")
TEMP_AUDIO_DIR.mkdir(exist_ok=True)
with open(os.path.join(TEMP_AUDIO_DIR, ".gitignore"), "w") as f:
f.write("*\n")
def decode_base64_audio(base64_str: str, prefix: str = "audio") -> str:
if not isinstance(base64_str, str) or not base64_str.strip():
raise ValueError("空的 base64 音频数据")
payload = base64_str.strip()
if payload.startswith("data:") and "," in payload:
payload = payload.split(",", 1)[1]
audio_bytes = base64.b64decode(payload)
if not audio_bytes:
raise ValueError("无效的 base64 音频数据")
file_hash = hashlib.md5(audio_bytes).hexdigest()
file_path = TEMP_AUDIO_DIR / f"{prefix}_{file_hash}.wav"
if not file_path.exists():
with open(file_path, "wb") as f:
f.write(audio_bytes)
return str(file_path)
@asynccontextmanager
async def lifespan(app: FastAPI):
global tts
tts = IndexTTS(model_dir=args.model_dir, gpu_memory_utilization=args.gpu_memory_utilization)
current_file_path = os.path.abspath(__file__)
cur_dir = os.path.dirname(current_file_path)
speaker_path = os.path.join(cur_dir, "assets/speaker.json")
if os.path.exists(speaker_path):
speaker_dict = json.load(open(speaker_path, 'r'))
for speaker, audio_paths in speaker_dict.items():
audio_paths_ = []
for audio_path in audio_paths:
audio_paths_.append(os.path.join(cur_dir, audio_path))
tts.registry_speaker(speaker, audio_paths_)
yield
# Clean up resources to avoid NCCL/vLLM warnings on shutdown
if tts is not None:
tts.shutdown()
tts = None
app = FastAPI(lifespan=lifespan)
# 添加CORS中间件配置
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # 允许所有来源,生产环境建议改为具体域名
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/health")
async def health_check():
"""健康检查接口"""
try:
global tts
if tts is None:
return JSONResponse(
status_code=503,
content={
"status": "unhealthy",
"message": "TTS model not initialized"
}
)
return JSONResponse(
status_code=200,
content={
"status": "healthy",
"message": "Service is running",
"timestamp": time.time()
}
)
except Exception as ex:
return JSONResponse(
status_code=503,
content={
"status": "unhealthy",
"error": str(ex)
}
)
@app.post("/tts_url", responses={
200: {"content": {"application/octet-stream": {}}},
500: {"content": {"application/json": {}}}
})
async def tts_api_url(request: Request):
try:
data = await request.json()
text = data["text"]
audio_paths = data.get("audio_paths") or []
base64_source = data.get("spk_audio_base64") or data.get("speaker_audio_base64")
if base64_source:
decoded_path = decode_base64_audio(base64_source, prefix="speaker")
audio_paths = [decoded_path]
seed = data.get("seed", 8)
if not audio_paths:
return JSONResponse(
status_code=400,
content={
"status": "error",
"error": "audio_paths 或 spk_audio_base64 至少提供一种说话人参考",
}
)
global tts
sr, wav = await tts.infer(audio_paths, text, seed=seed)
with io.BytesIO() as wav_buffer:
sf.write(wav_buffer, wav, sr, format='WAV')
wav_bytes = wav_buffer.getvalue()
return Response(content=wav_bytes, media_type="audio/wav")
except Exception as ex:
tb_str = ''.join(traceback.format_exception(type(ex), ex, ex.__traceback__))
return JSONResponse(
status_code=500,
content={
"status": "error",
"error": str(tb_str)
}
)
@app.post("/tts", responses={
200: {"content": {"application/octet-stream": {}}},
500: {"content": {"application/json": {}}}
})
async def tts_api(request: Request):
try:
data = await request.json()
text = data["text"]
character = data["character"]
global tts
sr, wav = await tts.infer_with_ref_audio_embed(character, text)
with io.BytesIO() as wav_buffer:
sf.write(wav_buffer, wav, sr, format='WAV')
wav_bytes = wav_buffer.getvalue()
return Response(content=wav_bytes, media_type="audio/wav")
except Exception as ex:
tb_str = ''.join(traceback.format_exception(type(ex), ex, ex.__traceback__))
print(tb_str)
return JSONResponse(
status_code=500,
content={
"status": "error",
"error": str(tb_str)
}
)
@app.get("/audio/voices")
async def tts_voices():
""" additional function to provide the list of available voices, in the form of JSON """
current_file_path = os.path.abspath(__file__)
cur_dir = os.path.dirname(current_file_path)
speaker_path = os.path.join(cur_dir, "assets/speaker.json")
if os.path.exists(speaker_path):
speaker_dict = json.load(open(speaker_path, 'r'))
return speaker_dict
else:
return []
@app.post("/audio/speech", responses={
200: {"content": {"application/octet-stream": {}}},
500: {"content": {"application/json": {}}}
})
async def tts_api_openai(request: Request):
""" OpenAI competible API, see: https://api.openai.com/v1/audio/speech """
try:
data = await request.json()
text = data["input"]
character = data["voice"]
#model param is omitted
_model = data["model"]
global tts
sr, wav = await tts.infer_with_ref_audio_embed(character, text)
with io.BytesIO() as wav_buffer:
sf.write(wav_buffer, wav, sr, format='WAV')
wav_bytes = wav_buffer.getvalue()
return Response(content=wav_bytes, media_type="audio/wav")
except Exception as ex:
tb_str = ''.join(traceback.format_exception(type(ex), ex, ex.__traceback__))
print(tb_str)
return JSONResponse(
status_code=500,
content={
"status": "error",
"error": str(tb_str)
}
)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--host", type=str, default="0.0.0.0")
parser.add_argument("--port", type=int, default=7880)
parser.add_argument("--model_dir", type=str, default="/home/tangyan/proj/ckpts/indextts-1_5-vllm")
parser.add_argument("--gpu_memory_utilization", type=float, default=0.03)
args = parser.parse_args()
uvicorn.run(app=app, host=args.host, port=args.port)