feat: 登录验证 + 邀请码注册 + 用量监控后台
- 新增用户认证系统(登录/登出/邀请码注册) - 新增管理后台(邀请码管理/用户管理/使用记录/用量统计) - 合成接口加登录验证,每次调用记录用户和稿件内容 - SQLite存储用户数据和使用日志 - 默认admin账号: simonkoson Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -1,56 +1,63 @@
|
||||
from flask import Blueprint, request, jsonify
|
||||
from flask import Blueprint, request, jsonify, session
|
||||
import base64
|
||||
import os
|
||||
import requests
|
||||
from app.services.minimax_service import synthesize_minimax
|
||||
from app.services.cosyvoice_service import synthesize_cosyvoice
|
||||
from app.routes.auth import login_required
|
||||
from app.db import get_db
|
||||
|
||||
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'])
|
||||
@login_required
|
||||
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的情绪指令
|
||||
emotion = data.get('emotion', 'neutral') # 用于MiniMax的情绪参数
|
||||
|
||||
instruction = data.get('instruction')
|
||||
emotion = data.get('emotion', 'neutral')
|
||||
|
||||
if not text.strip():
|
||||
return jsonify({'error': '文本不能为空'}), 400
|
||||
|
||||
|
||||
if len(text) > 2000:
|
||||
return jsonify({'error': '文本不能超过2000字'}), 400
|
||||
|
||||
|
||||
try:
|
||||
if engine == 'cosyvoice':
|
||||
audio_data = synthesize_cosyvoice(
|
||||
text=text,
|
||||
speed=speed,
|
||||
pitch=pitch,
|
||||
volume=volume,
|
||||
instruction=instruction
|
||||
text=text, speed=speed, pitch=pitch,
|
||||
volume=volume, instruction=instruction
|
||||
)
|
||||
else:
|
||||
audio_data = synthesize_minimax(
|
||||
text=text,
|
||||
speed=speed,
|
||||
pitch=pitch,
|
||||
volume=volume,
|
||||
emotion=emotion
|
||||
text=text, speed=speed, pitch=pitch,
|
||||
volume=volume, emotion=emotion
|
||||
)
|
||||
|
||||
|
||||
# 记录使用日志
|
||||
try:
|
||||
conn = get_db()
|
||||
conn.execute(
|
||||
'''INSERT INTO usage_logs
|
||||
(user_id, username, display_name, text_content, text_length, engine, emotion)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)''',
|
||||
(session['user_id'], session['username'], session['display_name'],
|
||||
text, len(text), engine, emotion)
|
||||
)
|
||||
conn.commit()
|
||||
conn.close()
|
||||
except Exception:
|
||||
pass # 日志写入失败不影响正常合成
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'audio': base64.b64encode(audio_data).decode('utf-8')
|
||||
|
||||
Reference in New Issue
Block a user