2026-03-18 17:18:23 +08:00
|
|
|
|
"""Tencent Cloud ASR adapter — implements ASRProvider port."""
|
|
|
|
|
|
|
2026-03-30 11:53:04 +08:00
|
|
|
|
import asyncio
|
2026-03-18 17:18:23 +08:00
|
|
|
|
import base64
|
2026-04-08 15:37:09 +08:00
|
|
|
|
|
2026-03-18 17:18:23 +08:00
|
|
|
|
from app.core.logging import get_logger
|
2026-04-08 15:37:09 +08:00
|
|
|
|
from app.ports.asr import ASRTranscriptionError
|
2026-03-18 17:18:23 +08:00
|
|
|
|
|
|
|
|
|
|
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:
|
2026-03-26 12:13:36 +08:00
|
|
|
|
logger.error("Tencent ASR client init failed: {}", e)
|
2026-03-18 17:18:23 +08:00
|
|
|
|
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:
|
|
|
|
|
|
client = self._get_client()
|
|
|
|
|
|
if not client:
|
2026-04-08 15:37:09 +08:00
|
|
|
|
raise ASRTranscriptionError(
|
|
|
|
|
|
"Tencent ASR client not initialized (check credentials)"
|
|
|
|
|
|
)
|
2026-03-18 17:18:23 +08:00
|
|
|
|
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
|
2026-03-27 16:01:28 +08:00
|
|
|
|
# 小写;与文档一致。iOS 常见为 m4a(AAC) 容器,与 16k 引擎匹配
|
|
|
|
|
|
req.VoiceFormat = (format or "m4a").lower()
|
2026-03-18 17:18:23 +08:00
|
|
|
|
req.Data = audio_base64
|
|
|
|
|
|
req.DataLen = len(audio)
|
|
|
|
|
|
|
2026-03-30 11:53:04 +08:00
|
|
|
|
# 腾讯 SDK 为同步阻塞调用;放到线程池里避免卡住事件循环。
|
|
|
|
|
|
resp = await asyncio.to_thread(client.SentenceRecognition, req)
|
2026-03-27 16:01:28 +08:00
|
|
|
|
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,
|
|
|
|
|
|
)
|
2026-04-08 15:37:09 +08:00
|
|
|
|
raise ASRTranscriptionError(
|
|
|
|
|
|
"Tencent ASR empty Result (check sample rate / format / audio)"
|
2026-03-27 16:01:28 +08:00
|
|
|
|
)
|
2026-04-08 15:37:09 +08:00
|
|
|
|
except ASRTranscriptionError:
|
|
|
|
|
|
raise
|
2026-03-18 17:18:23 +08:00
|
|
|
|
except Exception as e:
|
2026-03-27 16:01:28 +08:00
|
|
|
|
logger.error("Tencent ASR transcribe failed: {}", e, exc_info=True)
|
2026-04-08 15:37:09 +08:00
|
|
|
|
raise ASRTranscriptionError(f"Tencent ASR transcribe failed: {e!s}") from e
|