d6edc69c65
- Flask backend with MiniMax and CosyVoice TTS engines - Frontend UI with emotion tags, speed/pitch/volume controls - Nginx reverse proxy configuration - Dual engine support (MiniMax speech-2.8-hd, CosyVoice v3.5-flash)
56 lines
1.7 KiB
Python
56 lines
1.7 KiB
Python
from flask import Blueprint, request, jsonify
|
|
import dashscope
|
|
from dashscope.audio.tts_v2 import SpeechSynthesizer
|
|
import os
|
|
|
|
cosyvoice_bp = Blueprint('cosyvoice', __name__)
|
|
|
|
# CosyVoice配置
|
|
COSYVOICE_API_KEY = os.environ.get('COSYVOICE_API_KEY', 'sk-3c386742861b4d2da421030fa6254ef6')
|
|
COSYVOICE_VOICE_ID = os.environ.get('COSYVOICE_VOICE_ID', 'cosyvoice-v3.5-flash-bailian-e859a6e87823419f9c6b3eefb1a97dc3')
|
|
COSYVOICE_MODEL = 'cosyvoice-v3.5-flash' # 注意:克隆音色必须用这个模型
|
|
|
|
def synthesize_cosyvoice(text):
|
|
"""使用CosyVoice WebSocket SDK合成语音"""
|
|
try:
|
|
# 设置API Key
|
|
dashscope.api_key = COSYVOICE_API_KEY
|
|
|
|
# 创建合成器
|
|
synthesizer = SpeechSynthesizer(
|
|
model=COSYVOICE_MODEL,
|
|
voice=COSYVOICE_VOICE_ID
|
|
)
|
|
|
|
# 合成音频
|
|
audio = synthesizer.call(text)
|
|
|
|
if audio:
|
|
return {
|
|
"success": True,
|
|
"audio_data": audio.hex() if isinstance(audio, bytes) else audio,
|
|
"model": "cosyvoice",
|
|
"voice_id": COSYVOICE_VOICE_ID
|
|
}
|
|
else:
|
|
return {"success": False, "error": "No audio returned"}
|
|
|
|
except Exception as e:
|
|
return {"success": False, "error": str(e)}
|
|
|
|
@cosyvoice_bp.route('/cosyvoice', methods=['POST'])
|
|
def cosyvoice_synthesize():
|
|
"""CosyVoice合成接口"""
|
|
data = request.get_json()
|
|
text = data.get('text', '')
|
|
|
|
if not text:
|
|
return jsonify({"success": False, "error": "Text is required"}), 400
|
|
|
|
result = synthesize_cosyvoice(text)
|
|
|
|
if result.get("success"):
|
|
return jsonify(result)
|
|
else:
|
|
return jsonify(result), 500
|