mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-29 05:09:46 +01:00
update:只保留doubao asr
This commit is contained in:
parent
63d597250f
commit
b8051de81d
9 changed files with 472 additions and 136 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -1,5 +1,6 @@
|
||||||
# Byte-compiled / optimized / DLL files
|
# Byte-compiled / optimized / DLL files
|
||||||
__pycache__/
|
__pycache__/
|
||||||
|
.idea/
|
||||||
*.py[cod]
|
*.py[cod]
|
||||||
*$py.class
|
*$py.class
|
||||||
|
|
||||||
|
|
|
||||||
10
config.yaml
10
config.yaml
|
|
@ -59,7 +59,7 @@ CMD_exit:
|
||||||
|
|
||||||
# 具体处理时选择的模块(The module selected for specific processing)
|
# 具体处理时选择的模块(The module selected for specific processing)
|
||||||
selected_module:
|
selected_module:
|
||||||
ASR: FunASR
|
ASR: DoubaoASR
|
||||||
VAD: SileroVAD
|
VAD: SileroVAD
|
||||||
# 将根据配置名称对应的type调用实际的LLM适配器
|
# 将根据配置名称对应的type调用实际的LLM适配器
|
||||||
LLM: ChatGLMLLM
|
LLM: ChatGLMLLM
|
||||||
|
|
@ -68,9 +68,15 @@ selected_module:
|
||||||
|
|
||||||
ASR:
|
ASR:
|
||||||
FunASR:
|
FunASR:
|
||||||
|
type: fun_local
|
||||||
model_dir: models/SenseVoiceSmall
|
model_dir: models/SenseVoiceSmall
|
||||||
output_dir: tmp/
|
output_dir: tmp/
|
||||||
|
DoubaoASR:
|
||||||
|
type: doubao
|
||||||
|
appid: 你的火山引擎语音合成服务appid
|
||||||
|
access_token: 你的火山引擎语音合成服务access_token
|
||||||
|
cluster: volcengine_input_common
|
||||||
|
output_dir: tmp/
|
||||||
VAD:
|
VAD:
|
||||||
SileroVAD:
|
SileroVAD:
|
||||||
threshold: 0.5
|
threshold: 0.5
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ def setup_logging(log_dir='tmp', data_dir='data'):
|
||||||
|
|
||||||
# 设置日志格式,时间、日志级别、标签、消息
|
# 设置日志格式,时间、日志级别、标签、消息
|
||||||
log_format = (
|
log_format = (
|
||||||
# "<green>{time:YYYY-MM-DD HH:mm:ss}</green>"
|
"<green>{time:YY-MM-DD HH:mm:ss}</green>"
|
||||||
"[<light-blue>{extra[tag]}</light-blue>]"
|
"[<light-blue>{extra[tag]}</light-blue>]"
|
||||||
" - <level>{level}</level> - "
|
" - <level>{level}</level> - "
|
||||||
"<light-green>{message}</light-green>"
|
"<light-green>{message}</light-green>"
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,11 @@ async def handleAudioMessage(conn, audio):
|
||||||
if conn.client_voice_stop:
|
if conn.client_voice_stop:
|
||||||
conn.client_abort = False
|
conn.client_abort = False
|
||||||
conn.asr_server_receive = False
|
conn.asr_server_receive = False
|
||||||
text, file_path = conn.asr.speech_to_text(conn.asr_audio, conn.session_id)
|
# 音频太短了,无法识别
|
||||||
|
if len(conn.asr_audio) < 3:
|
||||||
|
conn.asr_server_receive = True
|
||||||
|
else:
|
||||||
|
text, file_path = await conn.asr.speech_to_text(conn.asr_audio, conn.session_id)
|
||||||
logger.bind(tag=TAG).info(f"识别文本: {text}")
|
logger.bind(tag=TAG).info(f"识别文本: {text}")
|
||||||
text_len, text_without_punctuation = remove_punctuation_and_length(text)
|
text_len, text_without_punctuation = remove_punctuation_and_length(text)
|
||||||
if text_len <= conn.max_cmd_length and await handleCMDMessage(conn, text_without_punctuation):
|
if text_len <= conn.max_cmd_length and await handleCMDMessage(conn, text_without_punctuation):
|
||||||
|
|
@ -40,6 +44,20 @@ async def handleAudioMessage(conn, audio):
|
||||||
conn.asr_audio.clear()
|
conn.asr_audio.clear()
|
||||||
conn.reset_vad_states()
|
conn.reset_vad_states()
|
||||||
|
|
||||||
|
async def speechToText(conn):
|
||||||
|
conn.client_abort = False
|
||||||
|
conn.asr_server_receive = False
|
||||||
|
text, file_path = await conn.asr.speech_to_text(conn.asr_audio, conn.session_id)
|
||||||
|
logger.info(f"识别文本: {text}")
|
||||||
|
text_len, text_without_punctuation = remove_punctuation_and_length(text)
|
||||||
|
if text_len <= conn.max_cmd_length and await handleCMDMessage(conn, text_without_punctuation):
|
||||||
|
return
|
||||||
|
if text_len > 0:
|
||||||
|
await startToChat(conn, text)
|
||||||
|
else:
|
||||||
|
conn.asr_server_receive = True
|
||||||
|
conn.asr_audio.clear()
|
||||||
|
conn.reset_vad_states()
|
||||||
|
|
||||||
async def handleCMDMessage(conn, text):
|
async def handleCMDMessage(conn, text):
|
||||||
cmd_exit = conn.cmd_exit
|
cmd_exit = conn.cmd_exit
|
||||||
|
|
|
||||||
19
core/providers/asr/base.py
Normal file
19
core/providers/asr/base.py
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
from abc import ABC, abstractmethod
|
||||||
|
from typing import Optional, Tuple, List
|
||||||
|
|
||||||
|
from config.logger import setup_logging
|
||||||
|
|
||||||
|
TAG = __name__
|
||||||
|
logger = setup_logging()
|
||||||
|
|
||||||
|
|
||||||
|
class ASRProviderBase(ABC):
|
||||||
|
@abstractmethod
|
||||||
|
def save_audio_to_file(self, opus_data: List[bytes], session_id: str) -> str:
|
||||||
|
"""解码Opus数据并保存为WAV文件"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
async def speech_to_text(self, opus_data: List[bytes], session_id: str) -> Tuple[Optional[str], Optional[str]]:
|
||||||
|
"""将语音数据转换为文本"""
|
||||||
|
pass
|
||||||
286
core/providers/asr/doubao.py
Normal file
286
core/providers/asr/doubao.py
Normal file
|
|
@ -0,0 +1,286 @@
|
||||||
|
import time
|
||||||
|
import io
|
||||||
|
import wave
|
||||||
|
import os
|
||||||
|
from typing import Optional, Tuple, List
|
||||||
|
import uuid
|
||||||
|
import websockets
|
||||||
|
import json
|
||||||
|
import gzip
|
||||||
|
|
||||||
|
import opuslib
|
||||||
|
from core.providers.asr.base import ASRProviderBase
|
||||||
|
|
||||||
|
from config.logger import setup_logging
|
||||||
|
|
||||||
|
TAG = __name__
|
||||||
|
logger = setup_logging()
|
||||||
|
|
||||||
|
CLIENT_FULL_REQUEST = 0b0001
|
||||||
|
CLIENT_AUDIO_ONLY_REQUEST = 0b0010
|
||||||
|
|
||||||
|
NO_SEQUENCE = 0b0000
|
||||||
|
NEG_SEQUENCE = 0b0010
|
||||||
|
|
||||||
|
SERVER_FULL_RESPONSE = 0b1001
|
||||||
|
SERVER_ACK = 0b1011
|
||||||
|
SERVER_ERROR_RESPONSE = 0b1111
|
||||||
|
|
||||||
|
NO_SERIALIZATION = 0b0000
|
||||||
|
JSON = 0b0001
|
||||||
|
THRIFT = 0b0011
|
||||||
|
CUSTOM_TYPE = 0b1111
|
||||||
|
NO_COMPRESSION = 0b0000
|
||||||
|
GZIP = 0b0001
|
||||||
|
CUSTOM_COMPRESSION = 0b1111
|
||||||
|
|
||||||
|
|
||||||
|
def parse_response(res):
|
||||||
|
"""
|
||||||
|
protocol_version(4 bits), header_size(4 bits),
|
||||||
|
message_type(4 bits), message_type_specific_flags(4 bits)
|
||||||
|
serialization_method(4 bits) message_compression(4 bits)
|
||||||
|
reserved (8bits) 保留字段
|
||||||
|
header_extensions 扩展头(大小等于 8 * 4 * (header_size - 1) )
|
||||||
|
payload 类似与http 请求体
|
||||||
|
"""
|
||||||
|
protocol_version = res[0] >> 4
|
||||||
|
header_size = res[0] & 0x0f
|
||||||
|
message_type = res[1] >> 4
|
||||||
|
message_type_specific_flags = res[1] & 0x0f
|
||||||
|
serialization_method = res[2] >> 4
|
||||||
|
message_compression = res[2] & 0x0f
|
||||||
|
reserved = res[3]
|
||||||
|
header_extensions = res[4:header_size * 4]
|
||||||
|
payload = res[header_size * 4:]
|
||||||
|
result = {}
|
||||||
|
payload_msg = None
|
||||||
|
payload_size = 0
|
||||||
|
if message_type == SERVER_FULL_RESPONSE:
|
||||||
|
payload_size = int.from_bytes(payload[:4], "big", signed=True)
|
||||||
|
payload_msg = payload[4:]
|
||||||
|
elif message_type == SERVER_ACK:
|
||||||
|
seq = int.from_bytes(payload[:4], "big", signed=True)
|
||||||
|
result['seq'] = seq
|
||||||
|
if len(payload) >= 8:
|
||||||
|
payload_size = int.from_bytes(payload[4:8], "big", signed=False)
|
||||||
|
payload_msg = payload[8:]
|
||||||
|
elif message_type == SERVER_ERROR_RESPONSE:
|
||||||
|
code = int.from_bytes(payload[:4], "big", signed=False)
|
||||||
|
result['code'] = code
|
||||||
|
payload_size = int.from_bytes(payload[4:8], "big", signed=False)
|
||||||
|
payload_msg = payload[8:]
|
||||||
|
if payload_msg is None:
|
||||||
|
return result
|
||||||
|
if message_compression == GZIP:
|
||||||
|
payload_msg = gzip.decompress(payload_msg)
|
||||||
|
if serialization_method == JSON:
|
||||||
|
payload_msg = json.loads(str(payload_msg, "utf-8"))
|
||||||
|
elif serialization_method != NO_SERIALIZATION:
|
||||||
|
payload_msg = str(payload_msg, "utf-8")
|
||||||
|
result['payload_msg'] = payload_msg
|
||||||
|
result['payload_size'] = payload_size
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
class ASRProvider(ASRProviderBase):
|
||||||
|
def __init__(self, config: dict, delete_audio_file: bool):
|
||||||
|
self.appid = config.get("appid")
|
||||||
|
self.cluster = config.get("cluster")
|
||||||
|
self.access_token = config.get("access_token")
|
||||||
|
self.output_dir = config.get("output_dir")
|
||||||
|
|
||||||
|
self.host = "openspeech.bytedance.com"
|
||||||
|
self.ws_url = f"wss://{self.host}/api/v2/asr"
|
||||||
|
self.success_code = 1000
|
||||||
|
self.seg_duration = 15000
|
||||||
|
|
||||||
|
# 确保输出目录存在
|
||||||
|
os.makedirs(self.output_dir, exist_ok=True)
|
||||||
|
|
||||||
|
def save_audio_to_file(self, opus_data: List[bytes], session_id: str) -> str:
|
||||||
|
"""将Opus音频数据解码并保存为WAV文件"""
|
||||||
|
file_name = f"asr_{session_id}_{uuid.uuid4()}.wav"
|
||||||
|
file_path = os.path.join(self.output_dir, file_name)
|
||||||
|
|
||||||
|
decoder = opuslib.Decoder(16000, 1) # 16kHz, 单声道
|
||||||
|
pcm_data = []
|
||||||
|
|
||||||
|
for opus_packet in opus_data:
|
||||||
|
try:
|
||||||
|
pcm_frame = decoder.decode(opus_packet, 960) # 960 samples = 60ms
|
||||||
|
pcm_data.append(pcm_frame)
|
||||||
|
except opuslib.OpusError as e:
|
||||||
|
logger.bind(tag=TAG).error(f"Opus解码错误: {e}", exc_info=True)
|
||||||
|
|
||||||
|
with wave.open(file_path, "wb") as wf:
|
||||||
|
wf.setnchannels(1)
|
||||||
|
wf.setsampwidth(2) # 2 bytes = 16-bit
|
||||||
|
wf.setframerate(16000)
|
||||||
|
wf.writeframes(b"".join(pcm_data))
|
||||||
|
|
||||||
|
return file_path
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _generate_header(message_type=CLIENT_FULL_REQUEST, message_type_specific_flags=NO_SEQUENCE) -> bytearray:
|
||||||
|
"""Generate protocol header."""
|
||||||
|
header = bytearray()
|
||||||
|
header_size = 1
|
||||||
|
header.append((0b0001 << 4) | header_size) # Protocol version
|
||||||
|
header.append((message_type << 4) | message_type_specific_flags)
|
||||||
|
header.append((0b0001 << 4) | 0b0001) # JSON serialization & GZIP compression
|
||||||
|
header.append(0x00) # reserved
|
||||||
|
return header
|
||||||
|
|
||||||
|
def _construct_request(self, reqid) -> dict:
|
||||||
|
"""Construct the request payload."""
|
||||||
|
return {
|
||||||
|
"app": {
|
||||||
|
"appid": self.appid,
|
||||||
|
"cluster": self.cluster,
|
||||||
|
"token": self.access_token,
|
||||||
|
},
|
||||||
|
"user": {
|
||||||
|
"uid": str(uuid.uuid4()),
|
||||||
|
},
|
||||||
|
"request": {
|
||||||
|
"reqid": reqid,
|
||||||
|
"show_utterances": False,
|
||||||
|
"sequence": 1
|
||||||
|
},
|
||||||
|
"audio": {
|
||||||
|
"format": "wav",
|
||||||
|
"rate": 16000,
|
||||||
|
"language": "zh-CN",
|
||||||
|
"bits": 16,
|
||||||
|
"channel": 1,
|
||||||
|
"codec": "raw",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
async def _send_request(self, audio_data: List[bytes], segment_size: int) -> Optional[str]:
|
||||||
|
"""Send request to Volcano ASR service."""
|
||||||
|
try:
|
||||||
|
auth_header = {'Authorization': 'Bearer; {}'.format(self.access_token)}
|
||||||
|
async with websockets.connect(self.ws_url, additional_headers=auth_header) as websocket:
|
||||||
|
# Prepare request data
|
||||||
|
request_params = self._construct_request(str(uuid.uuid4()))
|
||||||
|
print(request_params)
|
||||||
|
payload_bytes = str.encode(json.dumps(request_params))
|
||||||
|
payload_bytes = gzip.compress(payload_bytes)
|
||||||
|
full_client_request = self._generate_header()
|
||||||
|
full_client_request.extend((len(payload_bytes)).to_bytes(4, 'big')) # payload size(4 bytes)
|
||||||
|
full_client_request.extend(payload_bytes) # payload
|
||||||
|
|
||||||
|
# Send header and metadata
|
||||||
|
# full_client_request
|
||||||
|
await websocket.send(full_client_request)
|
||||||
|
res = await websocket.recv()
|
||||||
|
result = parse_response(res)
|
||||||
|
if 'payload_msg' in result and result['payload_msg']['code'] != self.success_code:
|
||||||
|
logger.bind(tag=TAG).error(f"ASR error: {result}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
for seq, (chunk, last) in enumerate(self.slice_data(audio_data, segment_size), 1):
|
||||||
|
if last:
|
||||||
|
audio_only_request = self._generate_header(
|
||||||
|
message_type=CLIENT_AUDIO_ONLY_REQUEST,
|
||||||
|
message_type_specific_flags=NEG_SEQUENCE
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
audio_only_request = self._generate_header(
|
||||||
|
message_type=CLIENT_AUDIO_ONLY_REQUEST
|
||||||
|
)
|
||||||
|
payload_bytes = gzip.compress(chunk)
|
||||||
|
audio_only_request.extend((len(payload_bytes)).to_bytes(4, 'big')) # payload size(4 bytes)
|
||||||
|
audio_only_request.extend(payload_bytes) # payload
|
||||||
|
# Send audio data
|
||||||
|
await websocket.send(audio_only_request)
|
||||||
|
|
||||||
|
# Receive response
|
||||||
|
response = await websocket.recv()
|
||||||
|
result = parse_response(response)
|
||||||
|
|
||||||
|
if 'payload_msg' in result and result['payload_msg']['code'] == self.success_code:
|
||||||
|
if len(result['payload_msg']['result']) > 0:
|
||||||
|
return result['payload_msg']['result'][0]["text"]
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
logger.bind(tag=TAG).error(f"ASR error: {result}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.bind(tag=TAG).error(f"ASR request failed: {e}", exc_info=True)
|
||||||
|
return None
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def decode_opus(opus_data: List[bytes], session_id: str) -> List[bytes]:
|
||||||
|
|
||||||
|
decoder = opuslib.Decoder(16000, 1) # 16kHz, 单声道
|
||||||
|
pcm_data = []
|
||||||
|
|
||||||
|
for opus_packet in opus_data:
|
||||||
|
try:
|
||||||
|
pcm_frame = decoder.decode(opus_packet, 960) # 960 samples = 60ms
|
||||||
|
pcm_data.append(pcm_frame)
|
||||||
|
except opuslib.OpusError as e:
|
||||||
|
logger.bind(tag=TAG).error(f"Opus解码错误: {e}", exc_info=True)
|
||||||
|
|
||||||
|
return pcm_data
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def read_wav_info(data: io.BytesIO = None) -> (int, int, int, int, int):
|
||||||
|
with io.BytesIO(data) as _f:
|
||||||
|
wave_fp = wave.open(_f, 'rb')
|
||||||
|
nchannels, sampwidth, framerate, nframes = wave_fp.getparams()[:4]
|
||||||
|
wave_bytes = wave_fp.readframes(nframes)
|
||||||
|
return nchannels, sampwidth, framerate, nframes, len(wave_bytes)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def slice_data(data: bytes, chunk_size: int) -> (list, bool):
|
||||||
|
"""
|
||||||
|
slice data
|
||||||
|
:param data: wav data
|
||||||
|
:param chunk_size: the segment size in one request
|
||||||
|
:return: segment data, last flag
|
||||||
|
"""
|
||||||
|
data_len = len(data)
|
||||||
|
offset = 0
|
||||||
|
while offset + chunk_size < data_len:
|
||||||
|
yield data[offset: offset + chunk_size], False
|
||||||
|
offset += chunk_size
|
||||||
|
else:
|
||||||
|
yield data[offset: data_len], True
|
||||||
|
|
||||||
|
async def speech_to_text(self, opus_data: List[bytes], session_id: str) -> Tuple[Optional[str], Optional[str]]:
|
||||||
|
"""将语音数据转换为文本"""
|
||||||
|
try:
|
||||||
|
# 合并所有opus数据包
|
||||||
|
pcm_data = self.decode_opus(opus_data, session_id)
|
||||||
|
combined_pcm_data = b''.join(pcm_data)
|
||||||
|
|
||||||
|
wav_buffer = io.BytesIO()
|
||||||
|
|
||||||
|
with wave.open(wav_buffer, "wb") as wav_file:
|
||||||
|
wav_file.setnchannels(1) # 设置声道数
|
||||||
|
wav_file.setsampwidth(2) # 设置采样宽度
|
||||||
|
wav_file.setframerate(16000) # 设置采样率
|
||||||
|
wav_file.writeframes(combined_pcm_data) # 写入 PCM 数据
|
||||||
|
|
||||||
|
# 获取封装后的 WAV 数据
|
||||||
|
wav_data = wav_buffer.getvalue()
|
||||||
|
nchannels, sampwidth, framerate, nframes, wav_len = self.read_wav_info(wav_data)
|
||||||
|
size_per_sec = nchannels * sampwidth * framerate
|
||||||
|
segment_size = int(size_per_sec * self.seg_duration / 1000)
|
||||||
|
|
||||||
|
# 语音识别
|
||||||
|
start_time = time.time()
|
||||||
|
text = await self._send_request(wav_data, segment_size)
|
||||||
|
if text:
|
||||||
|
logger.bind(tag=TAG).debug(f"语音识别耗时: {time.time() - start_time:.3f}s | 结果: {text}")
|
||||||
|
return text, None
|
||||||
|
return "", None
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.bind(tag=TAG).error(f"语音识别失败: {e}", exc_info=True)
|
||||||
|
return "", None
|
||||||
110
core/providers/asr/fun_local.py
Normal file
110
core/providers/asr/fun_local.py
Normal file
|
|
@ -0,0 +1,110 @@
|
||||||
|
import time
|
||||||
|
import wave
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import io
|
||||||
|
from config.logger import setup_logging
|
||||||
|
from typing import Optional, Tuple, List
|
||||||
|
import uuid
|
||||||
|
import opuslib_next
|
||||||
|
from core.providers.asr.base import ASRProviderBase
|
||||||
|
|
||||||
|
from funasr import AutoModel
|
||||||
|
from funasr.utils.postprocess_utils import rich_transcription_postprocess
|
||||||
|
|
||||||
|
TAG = __name__
|
||||||
|
logger = setup_logging()
|
||||||
|
|
||||||
|
|
||||||
|
# 捕获标准输出
|
||||||
|
class CaptureOutput:
|
||||||
|
def __enter__(self):
|
||||||
|
self._output = io.StringIO()
|
||||||
|
self._original_stdout = sys.stdout
|
||||||
|
sys.stdout = self._output
|
||||||
|
|
||||||
|
def __exit__(self, exc_type, exc_value, traceback):
|
||||||
|
sys.stdout = self._original_stdout
|
||||||
|
self.output = self._output.getvalue()
|
||||||
|
self._output.close()
|
||||||
|
|
||||||
|
# 将捕获到的内容通过 logger 输出
|
||||||
|
if self.output:
|
||||||
|
logger.bind(tag=TAG).info(self.output.strip())
|
||||||
|
|
||||||
|
|
||||||
|
class ASRProvider(ASRProviderBase):
|
||||||
|
def __init__(self, config: dict, delete_audio_file: bool):
|
||||||
|
self.model_dir = config.get("model_dir")
|
||||||
|
self.output_dir = config.get("output_dir") # 修正配置键名
|
||||||
|
self.delete_audio_file = delete_audio_file
|
||||||
|
|
||||||
|
# 确保输出目录存在
|
||||||
|
os.makedirs(self.output_dir, exist_ok=True)
|
||||||
|
with CaptureOutput():
|
||||||
|
self.model = AutoModel(
|
||||||
|
model=self.model_dir,
|
||||||
|
vad_kwargs={"max_single_segment_time": 30000},
|
||||||
|
disable_update=True,
|
||||||
|
hub="hf"
|
||||||
|
# device="cuda:0", # 启用GPU加速
|
||||||
|
)
|
||||||
|
|
||||||
|
def save_audio_to_file(self, opus_data: List[bytes], session_id: str) -> str:
|
||||||
|
"""将Opus音频数据解码并保存为WAV文件"""
|
||||||
|
file_name = f"asr_{session_id}_{uuid.uuid4()}.wav"
|
||||||
|
file_path = os.path.join(self.output_dir, file_name)
|
||||||
|
|
||||||
|
decoder = opuslib_next.Decoder(16000, 1) # 16kHz, 单声道
|
||||||
|
pcm_data = []
|
||||||
|
|
||||||
|
for opus_packet in opus_data:
|
||||||
|
try:
|
||||||
|
pcm_frame = decoder.decode(opus_packet, 960) # 960 samples = 60ms
|
||||||
|
pcm_data.append(pcm_frame)
|
||||||
|
except opuslib_next.OpusError as e:
|
||||||
|
logger.bind(tag=TAG).error(f"Opus解码错误: {e}", exc_info=True)
|
||||||
|
|
||||||
|
with wave.open(file_path, "wb") as wf:
|
||||||
|
wf.setnchannels(1)
|
||||||
|
wf.setsampwidth(2) # 2 bytes = 16-bit
|
||||||
|
wf.setframerate(16000)
|
||||||
|
wf.writeframes(b"".join(pcm_data))
|
||||||
|
|
||||||
|
return file_path
|
||||||
|
|
||||||
|
async def speech_to_text(self, opus_data: List[bytes], session_id: str) -> Tuple[Optional[str], Optional[str]]:
|
||||||
|
"""语音转文本主处理逻辑"""
|
||||||
|
file_path = None
|
||||||
|
try:
|
||||||
|
# 保存音频文件
|
||||||
|
start_time = time.time()
|
||||||
|
file_path = self.save_audio_to_file(opus_data, session_id)
|
||||||
|
logger.bind(tag=TAG).debug(f"音频文件保存耗时: {time.time() - start_time:.3f}s | 路径: {file_path}")
|
||||||
|
|
||||||
|
# 语音识别
|
||||||
|
start_time = time.time()
|
||||||
|
result = self.model.generate(
|
||||||
|
input=file_path,
|
||||||
|
cache={},
|
||||||
|
language="auto",
|
||||||
|
use_itn=True,
|
||||||
|
batch_size_s=60,
|
||||||
|
)
|
||||||
|
text = rich_transcription_postprocess(result[0]["text"])
|
||||||
|
logger.bind(tag=TAG).debug(f"语音识别耗时: {time.time() - start_time:.3f}s | 结果: {text}")
|
||||||
|
|
||||||
|
return text, file_path
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.bind(tag=TAG).error(f"语音识别失败: {e}", exc_info=True)
|
||||||
|
return "", None
|
||||||
|
|
||||||
|
finally:
|
||||||
|
# 文件清理逻辑
|
||||||
|
if self.delete_audio_file and file_path and os.path.exists(file_path):
|
||||||
|
try:
|
||||||
|
os.remove(file_path)
|
||||||
|
logger.bind(tag=TAG).debug(f"已删除临时音频文件: {file_path}")
|
||||||
|
except Exception as e:
|
||||||
|
logger.bind(tag=TAG).error(f"文件删除失败: {file_path} | 错误: {e}")
|
||||||
|
|
@ -1,132 +1,24 @@
|
||||||
import time
|
import importlib
|
||||||
import wave
|
import logging
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import io
|
import time
|
||||||
from abc import ABC, abstractmethod
|
import wave
|
||||||
from config.logger import setup_logging
|
|
||||||
from typing import Optional, Tuple, List
|
|
||||||
import uuid
|
import uuid
|
||||||
|
from abc import ABC, abstractmethod
|
||||||
import opuslib_next
|
from typing import Optional, Tuple, List
|
||||||
from funasr import AutoModel
|
from core.providers.asr.base import ASRProviderBase
|
||||||
from funasr.utils.postprocess_utils import rich_transcription_postprocess
|
from config.logger import setup_logging
|
||||||
|
|
||||||
TAG = __name__
|
TAG = __name__
|
||||||
logger = setup_logging()
|
logger = setup_logging()
|
||||||
|
|
||||||
# 捕获标准输出
|
def create_instance(class_name: str, *args, **kwargs) -> ASRProviderBase:
|
||||||
class CaptureOutput:
|
|
||||||
def __enter__(self):
|
|
||||||
self._output = io.StringIO()
|
|
||||||
self._original_stdout = sys.stdout
|
|
||||||
sys.stdout = self._output
|
|
||||||
|
|
||||||
def __exit__(self, exc_type, exc_value, traceback):
|
|
||||||
sys.stdout = self._original_stdout
|
|
||||||
self.output = self._output.getvalue()
|
|
||||||
self._output.close()
|
|
||||||
|
|
||||||
# 将捕获到的内容通过 logger 输出
|
|
||||||
if self.output:
|
|
||||||
logger.bind(tag=TAG).info(self.output.strip())
|
|
||||||
|
|
||||||
class ASR(ABC):
|
|
||||||
@abstractmethod
|
|
||||||
def save_audio_to_file(self, opus_data: List[bytes], session_id: str) -> str:
|
|
||||||
"""解码Opus数据并保存为WAV文件"""
|
|
||||||
pass
|
|
||||||
|
|
||||||
@abstractmethod
|
|
||||||
def speech_to_text(self, opus_data: List[bytes], session_id: str) -> Tuple[Optional[str], Optional[str]]:
|
|
||||||
"""将语音数据转换为文本"""
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class FunASR(ASR):
|
|
||||||
def __init__(self, config: dict, delete_audio_file: bool):
|
|
||||||
self.model_dir = config.get("model_dir")
|
|
||||||
self.output_dir = config.get("output_dir") # 修正配置键名
|
|
||||||
self.delete_audio_file = delete_audio_file
|
|
||||||
|
|
||||||
# 确保输出目录存在
|
|
||||||
os.makedirs(self.output_dir, exist_ok=True)
|
|
||||||
with CaptureOutput():
|
|
||||||
self.model = AutoModel(
|
|
||||||
model=self.model_dir,
|
|
||||||
vad_kwargs={"max_single_segment_time": 30000},
|
|
||||||
disable_update=True,
|
|
||||||
hub="hf"
|
|
||||||
# device="cuda:0", # 启用GPU加速
|
|
||||||
)
|
|
||||||
|
|
||||||
def save_audio_to_file(self, opus_data: List[bytes], session_id: str) -> str:
|
|
||||||
"""将Opus音频数据解码并保存为WAV文件"""
|
|
||||||
file_name = f"asr_{session_id}_{uuid.uuid4()}.wav"
|
|
||||||
file_path = os.path.join(self.output_dir, file_name)
|
|
||||||
|
|
||||||
decoder = opuslib_next.Decoder(16000, 1) # 16kHz, 单声道
|
|
||||||
pcm_data = []
|
|
||||||
|
|
||||||
for opus_packet in opus_data:
|
|
||||||
try:
|
|
||||||
pcm_frame = decoder.decode(opus_packet, 960) # 960 samples = 60ms
|
|
||||||
pcm_data.append(pcm_frame)
|
|
||||||
except opuslib_next.OpusError as e:
|
|
||||||
logger.bind(tag=TAG).error(f"Opus解码错误: {e}", exc_info=True)
|
|
||||||
|
|
||||||
with wave.open(file_path, "wb") as wf:
|
|
||||||
wf.setnchannels(1)
|
|
||||||
wf.setsampwidth(2) # 2 bytes = 16-bit
|
|
||||||
wf.setframerate(16000)
|
|
||||||
wf.writeframes(b"".join(pcm_data))
|
|
||||||
|
|
||||||
return file_path
|
|
||||||
|
|
||||||
def speech_to_text(self, opus_data: List[bytes], session_id: str) -> Tuple[Optional[str], Optional[str]]:
|
|
||||||
"""语音转文本主处理逻辑"""
|
|
||||||
file_path = None
|
|
||||||
try:
|
|
||||||
# 保存音频文件
|
|
||||||
start_time = time.time()
|
|
||||||
file_path = self.save_audio_to_file(opus_data, session_id)
|
|
||||||
logger.bind(tag=TAG).debug(f"音频文件保存耗时: {time.time() - start_time:.3f}s | 路径: {file_path}")
|
|
||||||
|
|
||||||
# 语音识别
|
|
||||||
start_time = time.time()
|
|
||||||
result = self.model.generate(
|
|
||||||
input=file_path,
|
|
||||||
cache={},
|
|
||||||
language="auto",
|
|
||||||
use_itn=True,
|
|
||||||
batch_size_s=60,
|
|
||||||
)
|
|
||||||
text = rich_transcription_postprocess(result[0]["text"])
|
|
||||||
logger.bind(tag=TAG).debug(f"语音识别耗时: {time.time() - start_time:.3f}s | 结果: {text}")
|
|
||||||
|
|
||||||
return text, file_path
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
logger.bind(tag=TAG).error(f"语音识别失败: {e}", exc_info=True)
|
|
||||||
return None, None
|
|
||||||
|
|
||||||
finally:
|
|
||||||
# 文件清理逻辑
|
|
||||||
if self.delete_audio_file and file_path and os.path.exists(file_path):
|
|
||||||
try:
|
|
||||||
os.remove(file_path)
|
|
||||||
logger.bind(tag=TAG).debug(f"已删除临时音频文件: {file_path}")
|
|
||||||
except Exception as e:
|
|
||||||
logger.bind(tag=TAG).error(f"文件删除失败: {file_path} | 错误: {e}")
|
|
||||||
|
|
||||||
|
|
||||||
def create_instance(class_name: str, *args, **kwargs) -> ASR:
|
|
||||||
"""工厂方法创建ASR实例"""
|
"""工厂方法创建ASR实例"""
|
||||||
cls_map = {
|
if os.path.exists(os.path.join('core', 'providers', 'asr', f'{class_name}.py')):
|
||||||
"FunASR": FunASR,
|
lib_name = f'core.providers.asr.{class_name}'
|
||||||
# 可扩展其他ASR实现
|
if lib_name not in sys.modules:
|
||||||
}
|
sys.modules[lib_name] = importlib.import_module(f'{lib_name}')
|
||||||
|
return sys.modules[lib_name].ASRProvider(*args, **kwargs)
|
||||||
|
|
||||||
if cls := cls_map.get(class_name):
|
raise ValueError(f"不支持的ASR类型: {class_name},请检查该配置的type是否设置正确")
|
||||||
return cls(*args, **kwargs)
|
|
||||||
raise ValueError(f"不支持的ASR类型: {class_name}")
|
|
||||||
|
|
@ -7,6 +7,7 @@ from core.utils import asr, vad, llm, tts
|
||||||
|
|
||||||
TAG = __name__
|
TAG = __name__
|
||||||
|
|
||||||
|
|
||||||
class WebSocketServer:
|
class WebSocketServer:
|
||||||
def __init__(self, config: dict):
|
def __init__(self, config: dict):
|
||||||
self.config = config
|
self.config = config
|
||||||
|
|
@ -21,7 +22,10 @@ class WebSocketServer:
|
||||||
self.config["VAD"][self.config["selected_module"]["VAD"]]
|
self.config["VAD"][self.config["selected_module"]["VAD"]]
|
||||||
),
|
),
|
||||||
asr.create_instance(
|
asr.create_instance(
|
||||||
self.config["selected_module"]["ASR"],
|
self.config["selected_module"]["ASR"]
|
||||||
|
if not 'type' in self.config["ASR"][self.config["selected_module"]["ASR"]]
|
||||||
|
else
|
||||||
|
self.config["ASR"][self.config["selected_module"]["ASR"]]["type"],
|
||||||
self.config["ASR"][self.config["selected_module"]["ASR"]],
|
self.config["ASR"][self.config["selected_module"]["ASR"]],
|
||||||
self.config["delete_audio"]
|
self.config["delete_audio"]
|
||||||
),
|
),
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue