from fastapi import FastAPI, HTTPException from pydantic import BaseModel from paddlespeech.cli.tts.infer import TTSExecutor app = FastAPI() tts_executor = TTSExecutor() class TextRequest(BaseModel): text: str def warm_up_tts(): default_text = "初始化文本" # 你可以使用任何你喜欢的文本 output_file = "warm_up_output.wav" # 暖身合成结果的输出文件名 # 调用合成功能 try: tts_executor(text=default_text, output=output_file) except Exception as e: print(f"Error during warm-up: {e}") # 在应用启动时进行暖身合成 warm_up_tts() @app.post("/synthesize/") async def synthesize_text(text_request: TextRequest): text = text_request.text output_file = "output.wav" # You can customize the output file name if needed try: tts_executor(text=text, output=output_file) return {"message": "语音合成成功", "output_file": output_file} except Exception as e: raise HTTPException(status_code=500, detail=str(e)) if __name__ == "__main__": import uvicorn uvicorn.run(app, host="0.0.0.0", port=8000)