server.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # Copyright (c) 2024 Alibaba Inc (authors: Xiang Lyu)
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import os
  15. import sys
  16. ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
  17. sys.path.append('{}/../../..'.format(ROOT_DIR))
  18. sys.path.append('{}/../../../third_party/Matcha-TTS'.format(ROOT_DIR))
  19. import argparse
  20. import logging
  21. logging.getLogger('matplotlib').setLevel(logging.WARNING)
  22. from fastapi import FastAPI, UploadFile, Form, File
  23. from fastapi.responses import StreamingResponse
  24. from fastapi.middleware.cors import CORSMiddleware
  25. import uvicorn
  26. import numpy as np
  27. from cosyvoice.cli.cosyvoice import CosyVoice
  28. from cosyvoice.utils.file_utils import load_wav
  29. app = FastAPI()
  30. # set cross region allowance
  31. app.add_middleware(
  32. CORSMiddleware,
  33. allow_origins=["*"],
  34. allow_credentials=True,
  35. allow_methods=["*"],
  36. allow_headers=["*"])
  37. def generate_data(model_output):
  38. for i in model_output:
  39. tts_audio = (i['tts_speech'].numpy() * (2 ** 15)).astype(np.int16).tobytes()
  40. yield tts_audio
  41. @app.get("/inference_sft")
  42. async def inference_sft(tts_text: str = Form(), spk_id: str = Form()):
  43. model_output = cosyvoice.inference_sft(tts_text, spk_id)
  44. return StreamingResponse(generate_data(model_output))
  45. @app.get("/inference_zero_shot")
  46. async def inference_zero_shot(tts_text: str = Form(), prompt_text: str = Form(), prompt_wav: UploadFile = File()):
  47. prompt_speech_16k = load_wav(prompt_wav.file, 16000)
  48. model_output = cosyvoice.inference_zero_shot(tts_text, prompt_text, prompt_speech_16k)
  49. return StreamingResponse(generate_data(model_output))
  50. @app.get("/inference_cross_lingual")
  51. async def inference_cross_lingual(tts_text: str = Form(), prompt_wav: UploadFile = File()):
  52. prompt_speech_16k = load_wav(prompt_wav.file, 16000)
  53. model_output = cosyvoice.inference_cross_lingual(tts_text, prompt_speech_16k)
  54. return StreamingResponse(generate_data(model_output))
  55. @app.get("/inference_instruct")
  56. async def inference_instruct(tts_text: str = Form(), spk_id: str = Form(), instruct_text: str = Form()):
  57. model_output = cosyvoice.inference_instruct(tts_text, spk_id, instruct_text)
  58. return StreamingResponse(generate_data(model_output))
  59. if __name__=='__main__':
  60. parser = argparse.ArgumentParser()
  61. parser.add_argument('--port',
  62. type=int,
  63. default=50000)
  64. parser.add_argument('--model_dir',
  65. type=str,
  66. default='iic/CosyVoice-300M',
  67. help='local path or modelscope repo id')
  68. args = parser.parse_args()
  69. cosyvoice = CosyVoice(args.model_dir)
  70. uvicorn.run(app, host="127.0.0.1", port=args.port)