Files
happy-life-star/backend-single/tts-service/app.py
T
peanut ee5a6aba5d feat: 小程序脚本首页重构 + 社交数据导入 + TTS 播放优化
- 后端:新增社交数据导入/审批/洞察生成 API(SocialContent/SocialInsight)
- 后端:优化脚本上下文服务,TTS 服务增强
- 小程序:重构脚本首页布局,新增社交导入页面
- 小程序:新增 useTtsPlayer composable,移除旧 ScriptAudioPlayer 组件
- 小程序:新增社交导入服务,优化请求服务
- SQL:新增社交数据导入建表脚本
- 文档:补充设计文档和实施计划

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-20 07:18:02 +08:00

74 lines
1.9 KiB
Python

import subprocess
from pathlib import Path
from fastapi import FastAPI
from pydantic import BaseModel, Field
app = FastAPI(title="Emotion Museum TTS")
BASE_DIR = Path(__file__).resolve().parent
PIPER_BIN = BASE_DIR / ".venv" / "bin" / "piper"
PIPER_MODEL = BASE_DIR / "models" / "zh_CN-huayan-medium.onnx"
PIPER_CONFIG = BASE_DIR / "models" / "zh_CN-huayan-medium.onnx.json"
class SynthesizeRequest(BaseModel):
text: str = Field(min_length=1, max_length=5000)
voice: str = "default_zh_female"
outputPath: str
@app.get("/health")
def health():
return {
"status": "ok",
"engine": "piper",
"modelReady": PIPER_MODEL.exists() and PIPER_CONFIG.exists(),
}
@app.post("/synthesize")
def synthesize(request: SynthesizeRequest):
output = Path(request.outputPath)
output.parent.mkdir(parents=True, exist_ok=True)
try:
if not PIPER_BIN.exists():
raise RuntimeError(f"piper binary not found: {PIPER_BIN}")
if not PIPER_MODEL.exists() or not PIPER_CONFIG.exists():
raise RuntimeError("piper Chinese voice model is not installed")
subprocess.run(
[
str(PIPER_BIN),
"--model",
str(PIPER_MODEL),
"--config",
str(PIPER_CONFIG),
"--output_file",
str(output),
"--sentence-silence",
"0.35",
],
input=request.text,
text=True,
check=True,
capture_output=True,
timeout=180,
)
except Exception as exc:
return {
"success": False,
"audioPath": None,
"durationMs": None,
"engine": "piper",
"errorMessage": str(exc),
}
return {
"success": True,
"audioPath": str(output),
"durationMs": None,
"engine": "piper",
}