8
一开始以为是 fastapi-queue 的问题,但是现在发现,是在 fastapoi 上无法成功运行 tts.inter(),运行的时候也不报错,而是直接忽略掉,觉得不可思议,因为自己也有用其它模型进行推理,所以百思不得其解。
fastapi_demo.py
import uvicorn
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from my_pro import config
import time
from datetime import datetime, timedelta
import os
import uuid
from indextts.infer import IndexTTS
app = FastAPI()
# 添加 CORS 中间件
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # 允许所有来源,生产环境中建议指定具体的来源
allow_credentials=True,# 允许发送凭证(如 cookies)
allow_methods=["*"], # 允许所有 HTTP 方法
allow_headers=["*"], # 允许所有请求头
)
print("正在加载 index-tts 模型")
IndexTTSModel = IndexTTS(model_dir=os.path.join(config.root_dir, 'indextts/checkpoints'),
cfg_path=os.path.join(config.root_dir, 'indextts/checkpoints/config.yaml'))
print("index-tts 模型加载成功")
def index_tts_inference(prompt_path: str, tts_text: str, filename: str):
"""使用 idnex-tts 模型进行推理"""
file_path = config.temp_dir
os.makedirs(file_path, exist_ok=True)
if filename is None or filename == '':
filename = 'p{}0{}'.format(time.strftime('%y%m%d%H%M%S', time.localtime(time.time())), 0)
audio_full_path = os.path.join(file_path, filename + '.wav')
else:
filename += '-' + str(uuid.uuid1()) + '.wav'
audio_full_path = os.path.join(file_path, filename)
audio_path = os.path.join(datetime.now().strftime('%Y/%m'), filename)
print("正在生成语音...")
begin = time.time()
print('audio_full_path:', audio_full_path)
print(type(IndexTTSModel))
IndexTTSModel.infer(prompt_path, tts_text, audio_full_path)
print('audio_full_path 是否存在:', os.path.exists(audio_full_path))
print("推理文本:", tts_text)
print(f"音频生成结束,耗时: {time.time() - begin:.2f}s")
return audio_full_path
@app.get('/tts_inter', tags=['测试'], summary='语音合成')
async def tts_inter(request: Request):
prompt_path = './train_wav/test.mp3'
tts_text = '语音合成测试'
filename = ''
index_tts_inference(prompt_path, tts_text, filename)
if __name__ == "__main__":
uvicorn.run(app='fastapi_demo:app', host="0.0.0.0", port=50000, reload=True)
打印内容
正在生成语音...
audio_full_path: /temp/p25040118321200.wav
<class 'indextts.infer.IndexTTS'>
audio_full_path 是否存在: False
推理文本: 语音合成测试
音频生成结束,耗时: 0.00s