feat: add logging for TTS calls with timing and error details
This commit is contained in:
23
app/main.py
23
app/main.py
@@ -9,6 +9,8 @@ import base64
|
|||||||
import subprocess
|
import subprocess
|
||||||
import uuid
|
import uuid
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import logging
|
||||||
|
import time
|
||||||
from contextlib import asynccontextmanager
|
from contextlib import asynccontextmanager
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
@@ -22,6 +24,15 @@ from sqlalchemy.orm import DeclarativeBase
|
|||||||
|
|
||||||
import config
|
import config
|
||||||
|
|
||||||
|
# ── Logging ───────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
logging.basicConfig(
|
||||||
|
level=logging.INFO,
|
||||||
|
format="%(asctime)s [%(levelname)s] %(message)s",
|
||||||
|
datefmt="%Y-%m-%d %H:%M:%S",
|
||||||
|
)
|
||||||
|
logger = logging.getLogger("tts-service")
|
||||||
|
|
||||||
# ── Database ──────────────────────────────────────────────────────────────
|
# ── Database ──────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
engine = create_async_engine(config.DATABASE_URL, echo=False)
|
engine = create_async_engine(config.DATABASE_URL, echo=False)
|
||||||
@@ -76,20 +87,30 @@ async def call_mimo_tts(text: str, style: str = "") -> bytes:
|
|||||||
"api-key": config.MIMO_API_KEY,
|
"api-key": config.MIMO_API_KEY,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
t0 = time.time()
|
||||||
|
logger.info(f"MiMo TTS 请求: text_len={len(text)}, style={style or '(默认)'}")
|
||||||
|
|
||||||
async with httpx.AsyncClient(timeout=120) as client:
|
async with httpx.AsyncClient(timeout=120) as client:
|
||||||
resp = await client.post(config.MIMO_API_ENDPOINT, json=payload, headers=headers)
|
resp = await client.post(config.MIMO_API_ENDPOINT, json=payload, headers=headers)
|
||||||
|
|
||||||
|
elapsed = round(time.time() - t0, 2)
|
||||||
|
|
||||||
if resp.status_code != 200:
|
if resp.status_code != 200:
|
||||||
|
logger.error(f"MiMo TTS 错误: HTTP {resp.status_code}, 耗时 {elapsed}s, 响应: {resp.text[:200]}")
|
||||||
raise HTTPException(502, f"MiMo TTS API 错误: HTTP {resp.status_code} - {resp.text[:300]}")
|
raise HTTPException(502, f"MiMo TTS API 错误: HTTP {resp.status_code} - {resp.text[:300]}")
|
||||||
|
|
||||||
data = resp.json()
|
data = resp.json()
|
||||||
if data.get("error"):
|
if data.get("error"):
|
||||||
|
logger.error(f"MiMo TTS 业务错误: {data['error']}, 耗时 {elapsed}s")
|
||||||
raise HTTPException(502, f"MiMo TTS 错误: {data['error']}")
|
raise HTTPException(502, f"MiMo TTS 错误: {data['error']}")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
audio_b64 = data["choices"][0]["message"]["audio"]["data"]
|
audio_b64 = data["choices"][0]["message"]["audio"]["data"]
|
||||||
return base64.b64decode(audio_b64)
|
wav_bytes = base64.b64decode(audio_b64)
|
||||||
|
logger.info(f"MiMo TTS 成功: wav_size={len(wav_bytes)} bytes, 耗时 {elapsed}s")
|
||||||
|
return wav_bytes
|
||||||
except (KeyError, IndexError, TypeError) as e:
|
except (KeyError, IndexError, TypeError) as e:
|
||||||
|
logger.error(f"MiMo TTS 响应解析失败: {e}, 耗时 {elapsed}s")
|
||||||
raise HTTPException(502, f"MiMo TTS 响应解析失败: {e}")
|
raise HTTPException(502, f"MiMo TTS 响应解析失败: {e}")
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user