mirror of
https://github.com/FunAudioLLM/CosyVoice.git
synced 2026-02-04 17:39:25 +08:00
update fastapi
This commit is contained in:
@@ -1,119 +1,77 @@
|
||||
# Set inference model
|
||||
# export MODEL_DIR=pretrained_models/CosyVoice-300M-Instruct
|
||||
# For development
|
||||
# fastapi dev --port 6006 fastapi_server.py
|
||||
# For production deployment
|
||||
# fastapi run --port 6006 fastapi_server.py
|
||||
|
||||
# Copyright (c) 2024 Alibaba Inc (authors: Xiang Lyu)
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
import os
|
||||
import sys
|
||||
import io,time
|
||||
from fastapi import FastAPI, Response, File, UploadFile, Form
|
||||
from fastapi.responses import HTMLResponse
|
||||
from fastapi.middleware.cors import CORSMiddleware #引入 CORS中间件模块
|
||||
from contextlib import asynccontextmanager
|
||||
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||
sys.path.append('{}/../../..'.format(ROOT_DIR))
|
||||
sys.path.append('{}/../../../third_party/Matcha-TTS'.format(ROOT_DIR))
|
||||
from cosyvoice.cli.cosyvoice import CosyVoice
|
||||
from cosyvoice.utils.file_utils import load_wav
|
||||
import numpy as np
|
||||
import torch
|
||||
import torchaudio
|
||||
import argparse
|
||||
import logging
|
||||
logging.getLogger('matplotlib').setLevel(logging.WARNING)
|
||||
from fastapi import FastAPI, UploadFile, Form, File
|
||||
from fastapi.responses import StreamingResponse
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
import uvicorn
|
||||
import numpy as np
|
||||
from cosyvoice.cli.cosyvoice import CosyVoice
|
||||
from cosyvoice.utils.file_utils import load_wav
|
||||
|
||||
class LaunchFailed(Exception):
|
||||
pass
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
model_dir = os.getenv("MODEL_DIR", "pretrained_models/CosyVoice-300M-SFT")
|
||||
if model_dir:
|
||||
logging.info("MODEL_DIR is {}", model_dir)
|
||||
app.cosyvoice = CosyVoice(model_dir)
|
||||
# sft usage
|
||||
logging.info("Avaliable speakers {}", app.cosyvoice.list_avaliable_spks())
|
||||
else:
|
||||
raise LaunchFailed("MODEL_DIR environment must set")
|
||||
yield
|
||||
|
||||
app = FastAPI(lifespan=lifespan)
|
||||
|
||||
#设置允许访问的域名
|
||||
origins = ["*"] #"*",即为所有,也可以改为允许的特定ip。
|
||||
app = FastAPI()
|
||||
# set cross region allowance
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=origins, #设置允许的origins来源
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"],
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"], # 设置允许跨域的http方法,比如 get、post、put等。
|
||||
allow_headers=["*"]) #允许跨域的headers,可以用来鉴别来源等作用。
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"])
|
||||
|
||||
def buildResponse(output):
|
||||
buffer = io.BytesIO()
|
||||
torchaudio.save(buffer, output, 22050, format="wav")
|
||||
buffer.seek(0)
|
||||
return Response(content=buffer.read(-1), media_type="audio/wav")
|
||||
def generate_data(model_output):
|
||||
for i in model_output:
|
||||
tts_audio = (i['tts_speech'].numpy() * (2 ** 15)).astype(np.int16).tobytes()
|
||||
yield tts_audio
|
||||
|
||||
@app.post("/api/inference/sft")
|
||||
@app.get("/api/inference/sft")
|
||||
async def sft(tts: str = Form(), role: str = Form()):
|
||||
start = time.process_time()
|
||||
output = app.cosyvoice.inference_sft(tts, role)
|
||||
end = time.process_time()
|
||||
logging.info("infer time is {} seconds", end-start)
|
||||
return buildResponse(output['tts_speech'])
|
||||
@app.get("/inference_sft")
|
||||
async def inference_sft(tts_text: str = Form(), spk_id: str = Form()):
|
||||
model_output = cosyvoice.inference_sft(tts_text, spk_id)
|
||||
return StreamingResponse(generate_data(model_output))
|
||||
|
||||
@app.post("/api/inference/zero-shot")
|
||||
async def zeroShot(tts: str = Form(), prompt: str = Form(), audio: UploadFile = File()):
|
||||
start = time.process_time()
|
||||
prompt_speech = load_wav(audio.file, 16000)
|
||||
prompt_audio = (prompt_speech.numpy() * (2**15)).astype(np.int16).tobytes()
|
||||
prompt_speech_16k = torch.from_numpy(np.array(np.frombuffer(prompt_audio, dtype=np.int16))).unsqueeze(dim=0)
|
||||
prompt_speech_16k = prompt_speech_16k.float() / (2**15)
|
||||
@app.get("/inference_zero_shot")
|
||||
async def inference_zero_shot(tts_text: str = Form(), prompt_text: str = Form(), prompt_wav: UploadFile = File()):
|
||||
prompt_speech_16k = load_wav(prompt_wav.file, 16000)
|
||||
model_output = cosyvoice.inference_zero_shot(tts_text, prompt_text, prompt_speech_16k)
|
||||
return StreamingResponse(generate_data(model_output))
|
||||
|
||||
output = app.cosyvoice.inference_zero_shot(tts, prompt, prompt_speech_16k)
|
||||
end = time.process_time()
|
||||
logging.info("infer time is {} seconds", end-start)
|
||||
return buildResponse(output['tts_speech'])
|
||||
@app.get("/inference_cross_lingual")
|
||||
async def inference_cross_lingual(tts_text: str = Form(), prompt_wav: UploadFile = File()):
|
||||
prompt_speech_16k = load_wav(prompt_wav.file, 16000)
|
||||
model_output = cosyvoice.inference_cross_lingual(tts_text, prompt_speech_16k)
|
||||
return StreamingResponse(generate_data(model_output))
|
||||
|
||||
@app.post("/api/inference/cross-lingual")
|
||||
async def crossLingual(tts: str = Form(), audio: UploadFile = File()):
|
||||
start = time.process_time()
|
||||
prompt_speech = load_wav(audio.file, 16000)
|
||||
prompt_audio = (prompt_speech.numpy() * (2**15)).astype(np.int16).tobytes()
|
||||
prompt_speech_16k = torch.from_numpy(np.array(np.frombuffer(prompt_audio, dtype=np.int16))).unsqueeze(dim=0)
|
||||
prompt_speech_16k = prompt_speech_16k.float() / (2**15)
|
||||
@app.get("/inference_instruct")
|
||||
async def inference_instruct(tts_text: str = Form(), spk_id: str = Form(), instruct_text: str = Form()):
|
||||
model_output = cosyvoice.inference_instruct(tts_text, spk_id, instruct_text)
|
||||
return StreamingResponse(generate_data(model_output))
|
||||
|
||||
output = app.cosyvoice.inference_cross_lingual(tts, prompt_speech_16k)
|
||||
end = time.process_time()
|
||||
logging.info("infer time is {} seconds", end-start)
|
||||
return buildResponse(output['tts_speech'])
|
||||
|
||||
@app.post("/api/inference/instruct")
|
||||
@app.get("/api/inference/instruct")
|
||||
async def instruct(tts: str = Form(), role: str = Form(), instruct: str = Form()):
|
||||
start = time.process_time()
|
||||
output = app.cosyvoice.inference_instruct(tts, role, instruct)
|
||||
end = time.process_time()
|
||||
logging.info("infer time is {} seconds", end-start)
|
||||
return buildResponse(output['tts_speech'])
|
||||
|
||||
@app.get("/api/roles")
|
||||
async def roles():
|
||||
return {"roles": app.cosyvoice.list_avaliable_spks()}
|
||||
|
||||
@app.get("/", response_class=HTMLResponse)
|
||||
async def root():
|
||||
return """
|
||||
<!DOCTYPE html>
|
||||
<html lang=zh-cn>
|
||||
<head>
|
||||
<meta charset=utf-8>
|
||||
<title>Api information</title>
|
||||
</head>
|
||||
<body>
|
||||
Get the supported tones from the Roles API first, then enter the tones and textual content in the TTS API for synthesis. <a href='./docs'>Documents of API</a>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
if __name__=='__main__':
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('--port',
|
||||
type=int,
|
||||
default=50000)
|
||||
parser.add_argument('--model_dir',
|
||||
type=str,
|
||||
default='iic/CosyVoice-300M',
|
||||
help='local path or modelscope repo id')
|
||||
args = parser.parse_args()
|
||||
cosyvoice = CosyVoice(args.model_dir)
|
||||
uvicorn.run(app, host="127.0.0.1", port=args.port)
|
||||
Reference in New Issue
Block a user