xiaozhi-esp32-server/core/providers/asr/fun_local.py
whoyao 14b8adf964
添加豆包在线 ASR (#82)
* 添加 长期记忆功能

* fix bugs

* add memory parm for config.yaml

* fix is_segment for tts

* 推送主分支测试

* 🎈 perf: 美化log

* 🐞 fix: bug

改用opus_next,增加文档

* 合并main到develop (#75)

* fix: 多种docker build 编译方式

* fix:push

* 优化打包速度 (#65)

* 🐳 chore: 优化打包速度

使用国内镜像

* 🎈 perf: 优化打包速度

注释日志的时间信息

* 合并多个更新 (#68)

* 🐳 chore: 优化打包速度

使用国内镜像

* 🎈 perf: 优化打包速度

注释日志的时间信息

* 🐳 chore: 改用docker hub

* 🐎 ci(ci): 自动打包docker

* 🐳 chore: 更新文档

* 🎈 perf: 改用opuslib_next

* 🌈 style: 统一log

* minimax语音tts和阿里云智能语音tts适配

* 本地模型不重复创建,共用一个 (#51)

* 本地模型不重复创建,共用一个

* 更新web端页面发布版本

* 🎈 perf: 优化开发体验 (#70)

添加内置模型下载,yarn

---------

Co-authored-by: TOM88812 <ttangxs0808@163.com>
Co-authored-by: HonestQiao <honestqiao@163.com>
Co-authored-by: 玄凤科技 <128943504+Eric0308@users.noreply.github.com>
Co-authored-by: 欣南科技 <huangrongzhuang@xin-nan.com>
Co-authored-by: Huang <32005838+openrz@users.noreply.github.com>

* feat: support doubao online ASR

* feat: support doubao online ASR

---------

Co-authored-by: fofo <zhongpei@vip.qq.com>
Co-authored-by: hrz <1710360675@qq.com>
Co-authored-by: 玄凤科技 <128943504+Eric0308@users.noreply.github.com>
Co-authored-by: kalicyh <34980061+kalicyh@users.noreply.github.com>
Co-authored-by: TOM88812 <ttangxs0808@163.com>
Co-authored-by: HonestQiao <honestqiao@163.com>
Co-authored-by: 欣南科技 <huangrongzhuang@xin-nan.com>
Co-authored-by: Huang <32005838+openrz@users.noreply.github.com>
Co-authored-by: 胡垚 <qsct9501@163.com>
2025-02-21 10:05:58 +08:00

110 lines
3.9 KiB
Python

import time
import wave
import os
import sys
import io
from abc import ABC, abstractmethod
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}")