api_v2.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. """
  2. # WebAPI文档
  3. ` python api_v2.py -a 127.0.0.1 -p 9880 -c GPT_SoVITS/configs/tts_infer.yaml `
  4. ## 执行参数:
  5. `-a` - `绑定地址, 默认"127.0.0.1"`
  6. `-p` - `绑定端口, 默认9880`
  7. `-c` - `TTS配置文件路径, 默认"GPT_SoVITS/configs/tts_infer.yaml"`
  8. ## 调用:
  9. ### 推理
  10. endpoint: `/tts`
  11. GET:
  12. ```
  13. http://127.0.0.1:9880/tts?text=先帝创业未半而中道崩殂,今天下三分,益州疲弊,此诚危急存亡之秋也。&text_lang=zh&ref_audio_path=archive_jingyuan_1.wav&prompt_lang=zh&prompt_text=我是「罗浮」云骑将军景元。不必拘谨,「将军」只是一时的身份,你称呼我景元便可&text_split_method=cut5&batch_size=1&media_type=wav&streaming_mode=true
  14. ```
  15. POST:
  16. ```json
  17. {
  18. "text": "", # str.(required) text to be synthesized
  19. "text_lang: "", # str.(required) language of the text to be synthesized
  20. "ref_audio_path": "", # str.(required) reference audio path
  21. "aux_ref_audio_paths": [], # list.(optional) auxiliary reference audio paths for multi-speaker tone fusion
  22. "prompt_text": "", # str.(optional) prompt text for the reference audio
  23. "prompt_lang": "", # str.(required) language of the prompt text for the reference audio
  24. "top_k": 5, # int. top k sampling
  25. "top_p": 1, # float. top p sampling
  26. "temperature": 1, # float. temperature for sampling
  27. "text_split_method": "cut0", # str. text split method, see text_segmentation_method.py for details.
  28. "batch_size": 1, # int. batch size for inference
  29. "batch_threshold": 0.75, # float. threshold for batch splitting.
  30. "split_bucket: True, # bool. whether to split the batch into multiple buckets.
  31. "speed_factor":1.0, # float. control the speed of the synthesized audio.
  32. "streaming_mode": False, # bool. whether to return a streaming response.
  33. "seed": -1, # int. random seed for reproducibility.
  34. "parallel_infer": True, # bool. whether to use parallel inference.
  35. "repetition_penalty": 1.35 # float. repetition penalty for T2S model.
  36. }
  37. ```
  38. RESP:
  39. 成功: 直接返回 wav 音频流, http code 200
  40. 失败: 返回包含错误信息的 json, http code 400
  41. ### 命令控制
  42. endpoint: `/control`
  43. command:
  44. "restart": 重新运行
  45. "exit": 结束运行
  46. GET:
  47. ```
  48. http://127.0.0.1:9880/control?command=restart
  49. ```
  50. POST:
  51. ```json
  52. {
  53. "command": "restart"
  54. }
  55. ```
  56. RESP: 无
  57. ### 切换GPT模型
  58. endpoint: `/set_gpt_weights`
  59. GET:
  60. ```
  61. http://127.0.0.1:9880/set_gpt_weights?weights_path=GPT_SoVITS/pretrained_models/s1bert25hz-2kh-longer-epoch=68e-step=50232.ckpt
  62. ```
  63. RESP:
  64. 成功: 返回"success", http code 200
  65. 失败: 返回包含错误信息的 json, http code 400
  66. ### 切换Sovits模型
  67. endpoint: `/set_sovits_weights`
  68. GET:
  69. ```
  70. http://127.0.0.1:9880/set_sovits_weights?weights_path=GPT_SoVITS/pretrained_models/s2G488k.pth
  71. ```
  72. RESP:
  73. 成功: 返回"success", http code 200
  74. 失败: 返回包含错误信息的 json, http code 400
  75. """
  76. import os
  77. import sys
  78. import traceback
  79. from typing import Generator
  80. now_dir = os.getcwd()
  81. sys.path.append(now_dir)
  82. sys.path.append("%s/GPT_SoVITS" % (now_dir))
  83. import argparse
  84. import subprocess
  85. import wave
  86. import signal
  87. import numpy as np
  88. import soundfile as sf
  89. from fastapi import FastAPI, Request, HTTPException, Response
  90. from fastapi.responses import StreamingResponse, JSONResponse
  91. from fastapi import FastAPI, UploadFile, File
  92. import uvicorn
  93. from io import BytesIO
  94. from tools.i18n.i18n import I18nAuto
  95. from GPT_SoVITS.TTS_infer_pack.TTS import TTS, TTS_Config
  96. from GPT_SoVITS.TTS_infer_pack.text_segmentation_method import get_method_names as get_cut_method_names
  97. from fastapi.responses import StreamingResponse
  98. from pydantic import BaseModel
  99. # print(sys.path)
  100. i18n = I18nAuto()
  101. cut_method_names = get_cut_method_names()
  102. parser = argparse.ArgumentParser(description="GPT-SoVITS api")
  103. parser.add_argument("-c", "--tts_config", type=str, default="GPT_SoVITS/configs/tts_infer.yaml", help="tts_infer路径")
  104. parser.add_argument("-a", "--bind_addr", type=str, default="127.0.0.1", help="default: 127.0.0.1")
  105. parser.add_argument("-p", "--port", type=int, default="9880", help="default: 9880")
  106. args = parser.parse_args()
  107. config_path = args.tts_config
  108. # device = args.device
  109. port = args.port
  110. host = args.bind_addr
  111. argv = sys.argv
  112. if config_path in [None, ""]:
  113. config_path = "GPT-SoVITS/configs/tts_infer.yaml"
  114. tts_config = TTS_Config(config_path)
  115. print(tts_config)
  116. tts_pipeline = TTS(tts_config)
  117. APP = FastAPI()
  118. class TTS_Request(BaseModel):
  119. text: str = None
  120. text_lang: str = None
  121. ref_audio_path: str = None
  122. aux_ref_audio_paths: list = None
  123. prompt_lang: str = None
  124. prompt_text: str = ""
  125. top_k:int = 5
  126. top_p:float = 1
  127. temperature:float = 1
  128. text_split_method:str = "cut5"
  129. batch_size:int = 1
  130. batch_threshold:float = 0.75
  131. split_bucket:bool = True
  132. speed_factor:float = 1.0
  133. fragment_interval:float = 0.3
  134. seed:int = -1
  135. media_type:str = "wav"
  136. streaming_mode:bool = False
  137. parallel_infer:bool = True
  138. repetition_penalty:float = 1.35
  139. ### modify from https://github.com/RVC-Boss/GPT-SoVITS/pull/894/files
  140. def pack_ogg(io_buffer:BytesIO, data:np.ndarray, rate:int):
  141. with sf.SoundFile(io_buffer, mode='w', samplerate=rate, channels=1, format='ogg') as audio_file:
  142. audio_file.write(data)
  143. return io_buffer
  144. def pack_raw(io_buffer:BytesIO, data:np.ndarray, rate:int):
  145. io_buffer.write(data.tobytes())
  146. return io_buffer
  147. def pack_wav(io_buffer:BytesIO, data:np.ndarray, rate:int):
  148. io_buffer = BytesIO()
  149. sf.write(io_buffer, data, rate, format='wav')
  150. return io_buffer
  151. def pack_aac(io_buffer:BytesIO, data:np.ndarray, rate:int):
  152. process = subprocess.Popen([
  153. 'ffmpeg',
  154. '-f', 's16le', # 输入16位有符号小端整数PCM
  155. '-ar', str(rate), # 设置采样率
  156. '-ac', '1', # 单声道
  157. '-i', 'pipe:0', # 从管道读取输入
  158. '-c:a', 'aac', # 音频编码器为AAC
  159. '-b:a', '192k', # 比特率
  160. '-vn', # 不包含视频
  161. '-f', 'adts', # 输出AAC数据流格式
  162. 'pipe:1' # 将输出写入管道
  163. ], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  164. out, _ = process.communicate(input=data.tobytes())
  165. io_buffer.write(out)
  166. return io_buffer
  167. def pack_audio(io_buffer:BytesIO, data:np.ndarray, rate:int, media_type:str):
  168. if media_type == "ogg":
  169. io_buffer = pack_ogg(io_buffer, data, rate)
  170. elif media_type == "aac":
  171. io_buffer = pack_aac(io_buffer, data, rate)
  172. elif media_type == "wav":
  173. io_buffer = pack_wav(io_buffer, data, rate)
  174. else:
  175. io_buffer = pack_raw(io_buffer, data, rate)
  176. io_buffer.seek(0)
  177. return io_buffer
  178. # from https://huggingface.co/spaces/coqui/voice-chat-with-mistral/blob/main/app.py
  179. def wave_header_chunk(frame_input=b"", channels=1, sample_width=2, sample_rate=32000):
  180. # This will create a wave header then append the frame input
  181. # It should be first on a streaming wav file
  182. # Other frames better should not have it (else you will hear some artifacts each chunk start)
  183. wav_buf = BytesIO()
  184. with wave.open(wav_buf, "wb") as vfout:
  185. vfout.setnchannels(channels)
  186. vfout.setsampwidth(sample_width)
  187. vfout.setframerate(sample_rate)
  188. vfout.writeframes(frame_input)
  189. wav_buf.seek(0)
  190. return wav_buf.read()
  191. def handle_control(command:str):
  192. if command == "restart":
  193. os.execl(sys.executable, sys.executable, *argv)
  194. elif command == "exit":
  195. os.kill(os.getpid(), signal.SIGTERM)
  196. exit(0)
  197. def check_params(req:dict):
  198. text:str = req.get("text", "")
  199. text_lang:str = req.get("text_lang", "")
  200. ref_audio_path:str = req.get("ref_audio_path", "")
  201. streaming_mode:bool = req.get("streaming_mode", False)
  202. media_type:str = req.get("media_type", "wav")
  203. prompt_lang:str = req.get("prompt_lang", "")
  204. text_split_method:str = req.get("text_split_method", "cut5")
  205. if ref_audio_path in [None, ""]:
  206. return JSONResponse(status_code=400, content={"message": "ref_audio_path is required"})
  207. if text in [None, ""]:
  208. return JSONResponse(status_code=400, content={"message": "text is required"})
  209. if (text_lang in [None, ""]) :
  210. return JSONResponse(status_code=400, content={"message": "text_lang is required"})
  211. elif text_lang.lower() not in tts_config.languages:
  212. return JSONResponse(status_code=400, content={"message": f"text_lang: {text_lang} is not supported in version {tts_config.version}"})
  213. if (prompt_lang in [None, ""]) :
  214. return JSONResponse(status_code=400, content={"message": "prompt_lang is required"})
  215. elif prompt_lang.lower() not in tts_config.languages:
  216. return JSONResponse(status_code=400, content={"message": f"prompt_lang: {prompt_lang} is not supported in version {tts_config.version}"})
  217. if media_type not in ["wav", "raw", "ogg", "aac"]:
  218. return JSONResponse(status_code=400, content={"message": f"media_type: {media_type} is not supported"})
  219. elif media_type == "ogg" and not streaming_mode:
  220. return JSONResponse(status_code=400, content={"message": "ogg format is not supported in non-streaming mode"})
  221. if text_split_method not in cut_method_names:
  222. return JSONResponse(status_code=400, content={"message": f"text_split_method:{text_split_method} is not supported"})
  223. return None
  224. async def tts_handle(req:dict):
  225. """
  226. Text to speech handler.
  227. Args:
  228. req (dict):
  229. {
  230. "text": "", # str.(required) text to be synthesized
  231. "text_lang: "", # str.(required) language of the text to be synthesized
  232. "ref_audio_path": "", # str.(required) reference audio path
  233. "aux_ref_audio_paths": [], # list.(optional) auxiliary reference audio paths for multi-speaker synthesis
  234. "prompt_text": "", # str.(optional) prompt text for the reference audio
  235. "prompt_lang": "", # str.(required) language of the prompt text for the reference audio
  236. "top_k": 5, # int. top k sampling
  237. "top_p": 1, # float. top p sampling
  238. "temperature": 1, # float. temperature for sampling
  239. "text_split_method": "cut5", # str. text split method, see text_segmentation_method.py for details.
  240. "batch_size": 1, # int. batch size for inference
  241. "batch_threshold": 0.75, # float. threshold for batch splitting.
  242. "split_bucket: True, # bool. whether to split the batch into multiple buckets.
  243. "speed_factor":1.0, # float. control the speed of the synthesized audio.
  244. "fragment_interval":0.3, # float. to control the interval of the audio fragment.
  245. "seed": -1, # int. random seed for reproducibility.
  246. "media_type": "wav", # str. media type of the output audio, support "wav", "raw", "ogg", "aac".
  247. "streaming_mode": False, # bool. whether to return a streaming response.
  248. "parallel_infer": True, # bool.(optional) whether to use parallel inference.
  249. "repetition_penalty": 1.35 # float.(optional) repetition penalty for T2S model.
  250. }
  251. returns:
  252. StreamingResponse: audio stream response.
  253. """
  254. streaming_mode = req.get("streaming_mode", False)
  255. return_fragment = req.get("return_fragment", False)
  256. media_type = req.get("media_type", "wav")
  257. check_res = check_params(req)
  258. if check_res is not None:
  259. return check_res
  260. if streaming_mode or return_fragment:
  261. req["return_fragment"] = True
  262. try:
  263. tts_generator=tts_pipeline.run(req)
  264. if streaming_mode:
  265. def streaming_generator(tts_generator:Generator, media_type:str):
  266. if media_type == "wav":
  267. yield wave_header_chunk()
  268. media_type = "raw"
  269. for sr, chunk in tts_generator:
  270. yield pack_audio(BytesIO(), chunk, sr, media_type).getvalue()
  271. # _media_type = f"audio/{media_type}" if not (streaming_mode and media_type in ["wav", "raw"]) else f"audio/x-{media_type}"
  272. return StreamingResponse(streaming_generator(tts_generator, media_type, ), media_type=f"audio/{media_type}")
  273. else:
  274. sr, audio_data = next(tts_generator)
  275. audio_data = pack_audio(BytesIO(), audio_data, sr, media_type).getvalue()
  276. return Response(audio_data, media_type=f"audio/{media_type}")
  277. except Exception as e:
  278. return JSONResponse(status_code=400, content={"message": f"tts failed", "Exception": str(e)})
  279. @APP.get("/control")
  280. async def control(command: str = None):
  281. if command is None:
  282. return JSONResponse(status_code=400, content={"message": "command is required"})
  283. handle_control(command)
  284. @APP.get("/tts")
  285. async def tts_get_endpoint(
  286. text: str = None,
  287. text_lang: str = None,
  288. ref_audio_path: str = None,
  289. aux_ref_audio_paths:list = None,
  290. prompt_lang: str = None,
  291. prompt_text: str = "",
  292. top_k:int = 5,
  293. top_p:float = 1,
  294. temperature:float = 1,
  295. text_split_method:str = "cut0",
  296. batch_size:int = 1,
  297. batch_threshold:float = 0.75,
  298. split_bucket:bool = True,
  299. speed_factor:float = 1.0,
  300. fragment_interval:float = 0.3,
  301. seed:int = -1,
  302. media_type:str = "wav",
  303. streaming_mode:bool = False,
  304. parallel_infer:bool = True,
  305. repetition_penalty:float = 1.35
  306. ):
  307. req = {
  308. "text": text,
  309. "text_lang": text_lang.lower(),
  310. "ref_audio_path": ref_audio_path,
  311. "aux_ref_audio_paths": aux_ref_audio_paths,
  312. "prompt_text": prompt_text,
  313. "prompt_lang": prompt_lang.lower(),
  314. "top_k": top_k,
  315. "top_p": top_p,
  316. "temperature": temperature,
  317. "text_split_method": text_split_method,
  318. "batch_size":int(batch_size),
  319. "batch_threshold":float(batch_threshold),
  320. "speed_factor":float(speed_factor),
  321. "split_bucket":split_bucket,
  322. "fragment_interval":fragment_interval,
  323. "seed":seed,
  324. "media_type":media_type,
  325. "streaming_mode":streaming_mode,
  326. "parallel_infer":parallel_infer,
  327. "repetition_penalty":float(repetition_penalty)
  328. }
  329. return await tts_handle(req)
  330. @APP.post("/tts")
  331. async def tts_post_endpoint(request: TTS_Request):
  332. req = request.dict()
  333. return await tts_handle(req)
  334. @APP.get("/set_refer_audio")
  335. async def set_refer_aduio(refer_audio_path: str = None):
  336. try:
  337. tts_pipeline.set_ref_audio(refer_audio_path)
  338. except Exception as e:
  339. return JSONResponse(status_code=400, content={"message": f"set refer audio failed", "Exception": str(e)})
  340. return JSONResponse(status_code=200, content={"message": "success"})
  341. # @APP.post("/set_refer_audio")
  342. # async def set_refer_aduio_post(audio_file: UploadFile = File(...)):
  343. # try:
  344. # # 检查文件类型,确保是音频文件
  345. # if not audio_file.content_type.startswith("audio/"):
  346. # return JSONResponse(status_code=400, content={"message": "file type is not supported"})
  347. # os.makedirs("uploaded_audio", exist_ok=True)
  348. # save_path = os.path.join("uploaded_audio", audio_file.filename)
  349. # # 保存音频文件到服务器上的一个目录
  350. # with open(save_path , "wb") as buffer:
  351. # buffer.write(await audio_file.read())
  352. # tts_pipeline.set_ref_audio(save_path)
  353. # except Exception as e:
  354. # return JSONResponse(status_code=400, content={"message": f"set refer audio failed", "Exception": str(e)})
  355. # return JSONResponse(status_code=200, content={"message": "success"})
  356. @APP.get("/set_gpt_weights")
  357. async def set_gpt_weights(weights_path: str = None):
  358. try:
  359. if weights_path in ["", None]:
  360. return JSONResponse(status_code=400, content={"message": "gpt weight path is required"})
  361. tts_pipeline.init_t2s_weights(weights_path)
  362. except Exception as e:
  363. return JSONResponse(status_code=400, content={"message": f"change gpt weight failed", "Exception": str(e)})
  364. return JSONResponse(status_code=200, content={"message": "success"})
  365. @APP.get("/set_sovits_weights")
  366. async def set_sovits_weights(weights_path: str = None):
  367. try:
  368. if weights_path in ["", None]:
  369. return JSONResponse(status_code=400, content={"message": "sovits weight path is required"})
  370. tts_pipeline.init_vits_weights(weights_path)
  371. except Exception as e:
  372. return JSONResponse(status_code=400, content={"message": f"change sovits weight failed", "Exception": str(e)})
  373. return JSONResponse(status_code=200, content={"message": "success"})
  374. if __name__ == "__main__":
  375. try:
  376. if host == 'None': # 在调用时使用 -a None 参数,可以让api监听双栈
  377. host = None
  378. uvicorn.run(app=APP, host=host, port=port, workers=1)
  379. except Exception as e:
  380. traceback.print_exc()
  381. os.kill(os.getpid(), signal.SIGTERM)
  382. exit(0)