wangzhibo
2026-08-30 c8515170fd399b4d2dfaebb59128d4674e58915e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import time
import os
from typing import Optional, Tuple, List
from aip import AipSpeech
from core.providers.asr.base import ASRProviderBase
from config.logger import setup_logging
from core.providers.asr.dto.dto import InterfaceType
 
TAG = __name__
logger = setup_logging()
 
 
class ASRProvider(ASRProviderBase):
    def __init__(self, config: dict, delete_audio_file: bool = True):
        super().__init__()
        self.interface_type = InterfaceType.NON_STREAM
        self.app_id = config.get("app_id")
        self.api_key = config.get("api_key")
        self.secret_key = config.get("secret_key")
 
        dev_pid = config.get("dev_pid", "1537")
        self.dev_pid = int(dev_pid) if dev_pid else 1537
 
        self.output_dir = config.get("output_dir")
        self.delete_audio_file = delete_audio_file
 
        self.client = AipSpeech(str(self.app_id), self.api_key, self.secret_key)
 
        # 确保输出目录存在
        os.makedirs(self.output_dir, exist_ok=True)
 
    async def speech_to_text(
        self, opus_data: List[bytes], session_id: str, artifacts=None
    ) -> Tuple[Optional[str], Optional[str]]:
        """将语音数据转换为文本"""
        if not opus_data:
            logger.bind(tag=TAG).warning("音频数据为空!")
            return None, None
 
        try:
            # 检查配置是否已设置
            if not self.app_id or not self.api_key or not self.secret_key:
                logger.bind(tag=TAG).error("百度语音识别配置未设置,无法进行识别")
                return None, None
 
            if artifacts is None:
                return "", None
 
            start_time = time.time()
            # 识别本地文件
            result = self.client.asr(
                artifacts.pcm_bytes,
                "pcm",
                16000,
                {
                    "dev_pid": str(self.dev_pid),
                },
            )
 
            if result and result["err_no"] == 0:
                logger.bind(tag=TAG).debug(
                    f"百度语音识别耗时: {time.time() - start_time:.3f}s | 结果: {result}"
                )
                result = result["result"][0]
                return result, artifacts.file_path
            else:
                raise Exception(
                    f"百度语音识别失败,错误码: {result['err_no']},错误信息: {result['err_msg']}"
                )
                return None, artifacts.file_path
 
        except Exception as e:
            logger.bind(tag=TAG).error(f"处理音频时发生错误!{e}", exc_info=True)
            return None, None