Files
military-tech-voice3.0/backend/app/routes/tts_synthesize.py
T
simonkoson d6edc69c65 Initial commit: 军事科技AI配音系统 v1.0
- 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)
2026-06-30 21:00:20 +08:00

58 lines
1.7 KiB
Python

from flask import Blueprint, request, jsonify
import base64
import os
import requests
from app.services.minimax_service import synthesize_minimax
from app.services.cosyvoice_service import synthesize_cosyvoice
bp = Blueprint('tts', __name__, url_prefix='/api')
MINIMAX_API_KEY = os.getenv('MINIMAX_API_KEY', '')
MINIMAX_API_URL = 'https://api.minimax.chat/v1/t2a_v2'
COSYVOICE_API_URL = os.getenv('COSYVOICE_API_URL', 'http://127.0.0.1:5001/v1/audio/synthesis')
@bp.route('/synthesize', methods=['POST'])
def synthesize():
data = request.get_json()
if not data or 'text' not in data:
return jsonify({'error': '缺少text参数'}), 400
text = data.get('text')
engine = data.get('engine', 'minimax')
speed = data.get('speed', 1.0)
pitch = data.get('pitch', 0)
volume = data.get('volume', 1.0)
instruction = data.get('instruction') # 用于CosyVoice的情绪指令
if not text.strip():
return jsonify({'error': '文本不能为空'}), 400
if len(text) > 500:
return jsonify({'error': '文本不能超过500字'}), 400
try:
if engine == 'cosyvoice':
audio_data = synthesize_cosyvoice(
text=text,
speed=speed,
pitch=pitch,
volume=volume,
instruction=instruction
)
else:
audio_data = synthesize_minimax(
text=text,
speed=speed,
pitch=pitch,
volume=volume
)
return jsonify({
'success': True,
'audio': base64.b64encode(audio_data).decode('utf-8')
})
except Exception as e:
return jsonify({'error': str(e)}), 500