2025-05-11 21:56:55 +08:00
|
|
|
import logging
|
2025-05-11 23:18:06 +08:00
|
|
|
from homeassistant.components.stt import AudioCodecs
|
2025-05-11 21:56:55 +08:00
|
|
|
from tencentcloud.common import credential
|
|
|
|
|
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
|
|
|
|
|
from tencentcloud.asr.v20190614.asr_client import AsrClient
|
|
|
|
|
from tencentcloud.asr.v20190614.models import SentenceRecognitionRequest, SentenceRecognitionResponse
|
|
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SentenceRecognitionSourceTypePost = 1
|
|
|
|
|
SentenceRecognitionSourceTypeURL = 0
|
|
|
|
|
ModuleSupportLanguage = {
|
2026-07-19 22:19:24 +08:00
|
|
|
"8k_zh": ["zh-cn"],
|
|
|
|
|
"8k_en": ["en"],
|
2025-05-18 21:24:57 +08:00
|
|
|
"16k_zh": ["zh-cn"],
|
|
|
|
|
"16k_zh-PY": ["zh-cn", "en", "zh-hk"],
|
2026-07-19 22:19:24 +08:00
|
|
|
"16k_zh_medical": ["zh-cn"],
|
2025-05-11 21:56:55 +08:00
|
|
|
"16k_en": ["en"],
|
2025-05-18 21:24:57 +08:00
|
|
|
"16k_yue": ["zh-hk"],
|
2026-07-19 22:19:24 +08:00
|
|
|
"16k_ja": ["ja"],
|
|
|
|
|
"16k_ko": ["ko"],
|
|
|
|
|
"16k_vi": ["vi"],
|
|
|
|
|
"16k_ms": ["ms"],
|
|
|
|
|
"16k_id": ["id"],
|
|
|
|
|
"16k_fil": ["fil"],
|
|
|
|
|
"16k_th": ["th"],
|
|
|
|
|
"16k_pt": ["pt"],
|
|
|
|
|
"16k_tr": ["tr"],
|
|
|
|
|
"16k_ar": ["ar"],
|
|
|
|
|
"16k_es": ["es"],
|
|
|
|
|
"16k_hi": ["hi"],
|
|
|
|
|
"16k_fr": ["fr"],
|
|
|
|
|
"16k_de": ["de"],
|
2025-05-11 21:56:55 +08:00
|
|
|
"16k_zh_dialect": ["zh"],
|
|
|
|
|
}
|
2025-05-11 23:18:06 +08:00
|
|
|
DefaultModel = "16k_zh"
|
2025-05-11 21:56:55 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class TencentCloudAsrAPi(object):
|
|
|
|
|
def __init__(self, secretId, secretKey):
|
2025-05-11 23:18:06 +08:00
|
|
|
cred = credential.Credential(secretId, secretKey)
|
|
|
|
|
region = ""
|
|
|
|
|
self.asrClient = AsrClient(cred, region)
|
2025-05-11 21:56:55 +08:00
|
|
|
|
|
|
|
|
def SentenceRecognition(self, engSerViceType, data: str) -> (tuple[bool, str | None]):
|
|
|
|
|
req = SentenceRecognitionRequest()
|
|
|
|
|
req.EngSerViceType = engSerViceType
|
|
|
|
|
req.SourceType = SentenceRecognitionSourceTypePost
|
2025-05-11 23:18:06 +08:00
|
|
|
req.VoiceFormat = AudioCodecs.PCM
|
2025-05-11 21:56:55 +08:00
|
|
|
req.ProjectId = 0
|
|
|
|
|
req.SubServiceType = 2
|
|
|
|
|
req.UsrAudioKey = ""
|
|
|
|
|
req.Data = data
|
|
|
|
|
req.DataLen = len(data)
|
|
|
|
|
try:
|
|
|
|
|
res: SentenceRecognitionResponse = self.asrClient.SentenceRecognition(req)
|
|
|
|
|
return True, res.Result
|
|
|
|
|
except TencentCloudSDKException as err:
|
|
|
|
|
_LOGGER.error("recognition failed:%s", err.message)
|
|
|
|
|
return False, str(err)
|