# cosyvoice的fastapi服务器 **Repository Path**: mudk/fastapi-server-for-cosyvoice ## Basic Information - **Project Name**: cosyvoice的fastapi服务器 - **Description**: 一个可以在其他python项目里通过http请求调用cosyvoice/cosyvoice2功能的本地网页服务器 - **Primary Language**: Python - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2025-01-30 - **Last Updated**: 2025-04-04 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # cosyvoice的fastapi服务器 #### 介绍 一个可以在其他python项目里通过http请求调用cosyvoice/cosyvoice2功能的本地网页服务器 #### 使用说明 在cosyvoice配置好后,直接将tts_http.py拷贝到项目根目录下 注意:cosyvoice2模型不支持预训练音声,如果要用预训练音色请用其他模型 服务端启动: ```python python tts_http.py --model_dir /path/to/model --port 8000 ``` --model_dir: 模型路径,端口自己定义 客户端示例: ```python # client.py import requests import os def test_api(): url = "http://localhost:8000/generate" # 基本参数 data = { "text": "欢迎使用语音[laughter]合成服务", "mode": "预训练音色", "speaker": "中文女", "speed": 1.0, "seed": 42 } # 文件上传示例 files = {} """ # 带音频的测试示例 files = {"prompt_audio": open("prompt.wav", "rb")} data.update({ "mode": "3s极速复刻", "prompt_text": "这是参考文本" }) """ try: response = requests.post(url, data=data, files=files) response.raise_for_status() with open("output.wav", "wb") as f: f.write(response.content) print(f"生成成功!文件大小:{os.path.getsize('output.wav')}字节") except requests.HTTPError as e: print(f"HTTP错误: {e.response.status_code}") if response.content: with open("error.wav", "wb") as f: f.write(response.content) print("保存错误音频到error.wav") except Exception as e: print(f"请求失败: {str(e)}") if __name__ == "__main__": test_api() ``` 启动客户端: ```python python client.py ```