feat: 集成Piper自建TTS并增加朗读回退
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
from flask import Blueprint, jsonify, request, send_file
|
||||
|
||||
from server.auth import login_required
|
||||
from server.tts_service import generate_speech, piper_available
|
||||
|
||||
tts_bp = Blueprint("tts", __name__)
|
||||
|
||||
|
||||
@tts_bp.route("/api/tts", methods=["GET", "POST"])
|
||||
@login_required
|
||||
def tts_audio(user):
|
||||
if request.method == "POST":
|
||||
data = request.get_json(silent=True) or {}
|
||||
text = (data.get("text") or "").strip()
|
||||
lang = (data.get("lang") or "es").strip().lower()
|
||||
else:
|
||||
text = (request.args.get("text") or "").strip()
|
||||
lang = (request.args.get("lang") or "es").strip().lower()
|
||||
|
||||
if not text:
|
||||
return jsonify({"error": "朗读内容不能为空"}), 400
|
||||
if len(text) > 1000:
|
||||
return jsonify({"error": "朗读内容不能超过 1000 个字符"}), 400
|
||||
if lang not in ("es", "en", "zh"):
|
||||
return jsonify({"error": "不支持的朗读语言"}), 400
|
||||
if not piper_available():
|
||||
return jsonify({"error": "Piper 未配置或未安装"}), 503
|
||||
|
||||
try:
|
||||
audio_path = generate_speech(text, lang)
|
||||
except Exception as exc:
|
||||
return jsonify({"error": "Piper 生成失败: " + str(exc)}), 500
|
||||
|
||||
return send_file(
|
||||
audio_path,
|
||||
mimetype="audio/wav",
|
||||
as_attachment=False,
|
||||
download_name="piper.wav",
|
||||
conditional=True,
|
||||
)
|
||||
Reference in New Issue
Block a user