Files
life-echo/api/app/adapters/asr/tencent_asr.py
Sully fa42757916 feat: OpenTelemetry LGTM observability, dev tooling, and memoir UX fixes (#31)
* add staging ios app build script

* feat(api): add OpenTelemetry LGTM stack for local observability

Wire OTel traces, metrics, and logs through a collector to Tempo,
Prometheus, and Loki, with custom LLM instrumentation, dev compose overlay,
Grafana provisioning, env templates, and development.sh auto-start.

Co-authored-by: Cursor <cursoragent@cursor.com>

* feat: expand observability, harden dev tooling, and fix expo staging UX

Add business and LLM Prometheus metrics with Grafana dashboards, alerting,
and a metrics verification script. Wire telemetry through adapters and core
LLM paths, and document the local LGTM workflow.

Fix development.sh for macOS bash 3.2, open Grafana and eval-web in Chrome,
and repair eval-web auto-open (unbound EVAL_WEB_BROWSER_SCHEDULED). Merge
internal-eval into the main dev script with improved compose handling.

Require EXPO_PUBLIC_* at build time, improve iOS HTTP ATS for staging IPs,
show memoir empty state instead of load errors when no chapters exist, and
add jest env setup plus chapter list response normalization.

Co-authored-by: Cursor <cursoragent@cursor.com>

* chore: enable Grafana Assistant Cursor plugin

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix: memoir empty state and repair withdrawn 0020_chapters_book_id stamp

Show empty memoir UI when the chapter list succeeds with no items; treat auth/404 as non-fatal. Extend alembic revision repair so local dev DBs stamped with the removed 0020_chapters_book_id migration can roll back and upgrade to 0019.

Co-authored-by: Cursor <cursoragent@cursor.com>

---------

Co-authored-by: Kevin <kevin@brighteng.org>
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-20 15:12:21 +08:00

84 lines
3.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Tencent Cloud ASR adapter — implements ASRProvider port."""
import asyncio
import base64
from app.core.business_telemetry import business_span
from app.core.logging import get_logger
from app.ports.asr import ASRTranscriptionError
logger = get_logger(__name__)
class TencentASRProvider:
def __init__(self, secret_id: str, secret_key: str):
self._secret_id = secret_id
self._secret_key = secret_key
self._client = None
def _get_client(self):
if self._client is not None:
return self._client
try:
from tencentcloud.asr.v20190614 import asr_client
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
cred = credential.Credential(self._secret_id, self._secret_key)
http_profile = HttpProfile()
http_profile.endpoint = "asr.tencentcloudapi.com"
client_profile = ClientProfile()
client_profile.httpProfile = http_profile
self._client = asr_client.AsrClient(cred, "", client_profile)
return self._client
except Exception as e:
logger.error("Tencent ASR client init failed: {}", e)
return None
def ensure_ready(self) -> bool:
return bool(self._secret_id and self._secret_key and self._get_client())
async def transcribe(self, audio: bytes, format: str = "m4a") -> str:
with business_span("asr.transcribe", provider="tencent"):
return await self._transcribe_inner(audio, format)
async def _transcribe_inner(self, audio: bytes, format: str) -> str:
client = self._get_client()
if not client:
raise ASRTranscriptionError(
"Tencent ASR client not initialized (check credentials)"
)
try:
from tencentcloud.asr.v20190614 import models
audio_base64 = base64.b64encode(audio).decode("utf-8")
req = models.SentenceRecognitionRequest()
req.EngSerViceType = "16k_zh"
req.SourceType = 1
# 小写与文档一致。iOS 常见为 m4a(AAC) 容器,与 16k 引擎匹配
req.VoiceFormat = (format or "m4a").lower()
req.Data = audio_base64
req.DataLen = len(audio)
# 腾讯 SDK 为同步阻塞调用;放到线程池里避免卡住事件循环。
resp = await asyncio.to_thread(client.SentenceRecognition, req)
text = (resp.Result or "").strip()
if text:
return text
err = getattr(resp, "Error", None) or getattr(resp, "Message", None)
logger.warning(
"Tencent ASR empty Result, audio_len={} format={} err={}",
len(audio),
req.VoiceFormat,
err,
)
raise ASRTranscriptionError(
"Tencent ASR empty Result (check sample rate / format / audio)"
)
except ASRTranscriptionError:
raise
except Exception as e:
logger.error("Tencent ASR transcribe failed: {}", e, exc_info=True)
raise ASRTranscriptionError(f"Tencent ASR transcribe failed: {e!s}") from e