ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

Haystack 集成 FunASR:本地化语音转写组件 FunASRTranscriber 完全指南

Haystack 集成 FunASR:本地化语音转写组件 FunASRTranscriber 完全指南 Haystack 集成 FunASR本地化语音转写组件 FunASRTranscriber 完全指南【免费下载链接】haystackOpen-source AI orchestration framework for building context-engineered, production-ready LLM applications. Design modular pipelines and agent workflows with explicit control over retrieval, routing, memory, and generation. Built for scalable agents, RAG, multimodal applications, semantic search, and conversational systems.项目地址: https://gitcode.com/GitHub_Trending/ha/haystackFunASRTranscriber 是 Haystack 生态中用于把音频文件转写为 HaystackDocument的组件它基于阿里达摩院开源的 FunASR 语音识别工具包完全本地运行、无需任何 API Key并原生支持 50 语言、说话人分离与时间戳提取。通过本文你将掌握 FunASRTranscriber 的全部初始化参数、run/warm_up/序列化接口的用法以及如何在独立场景和 RAG 索引管道中落地音频转写。组件概览本地化的语音转写能力FunASRTranscriber的定位是把音频文件转成结构化的 HaystackDocument对象供后续管道继续处理。它的核心特性包括完全本地运行无需 API Key音频数据不离开本机适合对隐私敏感的场景多语言与附加能力支持 50 语言、说话人分离speaker diarization和时间戳提取默认模型高性能默认使用iic/SenseVoiceSmall多语言模型官方文档给出的描述是相比 Whisper 快 5–10 倍模型自动下载缓存模型在首次使用时从 ModelScope 下载并缓存在~/.cache/modelscope目录下。从管道位置来看它最常见的用法是作为索引管道indexing pipeline中的第一个组件——文档在 音频组件总览 中与LocalWhisperTranscriber、RemoteWhisperTranscriber并列三者的差异在于运行后端FunASR 完全本地且免费而两个 Whisper 组件分别对应本地安装版与远程 API 版。一分钟快速上手最小使用示例非常简洁from haystack_integrations.components.audio.funasr import FunASRTranscriber transcriber FunASRTranscriber() result transcriber.run(sources[speech.wav, interview.mp3]) documents result[documents]这段代码做的事实例化组件默认模型iic/SenseVoiceSmall把两个音频文件路径交给run()返回结果中documents键对应一份转写后的Document列表——每个输入源对应一个Document完整转写文本存放在content字段中。模型会在组件第一次运行时自动加载进内存首次使用会因下载模型而耗时较长之后便直接命中本地缓存。详细 API 参考见 version-2.23 FunASR 集成文档管道组件使用指南见 FunASRTranscriber 组件文档。构造函数与全部参数解析__init__的完整签名如下__init__( *, model: str iic/SenseVoiceSmall, vad_model: str | None fsmn-vad, punc_model: str | None ct-punc, spk_model: str | None None, device: ComponentDevice | None None, batch_size_s: int 300, store_full_path: bool False, generation_kwargs: dict[str, Any] | None None ) - None注意所有参数均为关键字参数*之后的参数不能按位置传参下面逐一说明。model选择识别模型FunASR 模型名称或本地模型路径默认iic/SenseVoiceSmall这是一个支持 50 语言的多语言模型。可选替代模型包括paraformer-zh面向中文场景paraformer-en面向英文场景本地路径如果你已把模型下载到磁盘可以直接传入本地目录路径跳过联网下载。更多模型可在 FunASR 官方模型选择页面浏览https://modelscope.github.io/FunASR/model-selection.html该链接为模型清单页非本文引用依据仅作指引。vad_model语音活动检测语音活动检测Voice Activity Detection模型用于把长音频切分成片段后再识别默认fsmn-vad。设为None时整段音频会被当作单一流处理——如果音频很长且静音段较多保留 VAD 通常能获得更好的转写体验。可选 VAD 模型可在 ModelScope 模型库中查找。punc_model标点恢复标点恢复模型默认ct-punc用于给转写文本补回标点符号。设为None则禁用标点恢复输出的content将是无标点的纯文本。spk_model说话人分离说话人分离diarization模型例如cam。默认None表示关闭分离功能一旦设置该参数生成的Document元数据中会额外包含speakers键标识每段文本对应的说话人。device推理设备ComponentDevice | None指定推理运行的设备。None时自动选择默认设备显式指定 GPU 用ComponentDevice.from_str(cuda)。ComponentDevice定义于 haystack/utils/device.py它支持的设备类型见DeviceType枚举cpu、cudaGPU、mpsApple Silicon、xpuIntel GPU以及仅用于设备映射的disk字符串解析入口ComponentDevice.from_str()内部通过Device.from_str()拆分设备类型与设备 ID例如cuda:1指定第二块 GPU随后转为 PyTorch 设备格式to_torch()。batch_size_sVAD 分段的批处理时长VAD 切分后音频段的批处理时长单位秒默认 300。数值越大吞吐越高但内存占用也随之上升需要根据实际音频长度和硬件内存权衡。store_full_path路径存储方式控制Document元数据中路径的存法True存储音频文件的完整路径False默认只存文件名。generation_kwargs透传生成参数额外关键字参数会被原样转发给 FunASR 的AutoModel.generate()。这是模型级微调的通道典型用法包括SenseVoice 系模型的use_itnTrue开启逆文本正则化把数字、时间等口语转为规范写法、merge_vadTrue、languageauto自动语言识别上下文热词识别hotword...。run音频转写为 Documentsrun是组件的核心执行入口签名如下run( sources: list[str | Path | ByteStream], meta: dict[str, Any] | list[dict[str, Any]] | None None, ) - dict[str, list[Document]]sources 支持三种输入str或Path音频文件路径ByteStream对象Haystack 的二进制流数据类便于接收来自网络抓取或管道上游组件如LinkContentFetcher的二进制音频。支持格式包括 WAV、MP3、FLAC、OGG、M4A、AAC以及 FunASR 底层音频后端soundfile/ffmpeg能够解码的任何格式。meta 的两种用法传入单个dict同一份元数据应用到所有生成的Document传入与sources等长的list[dict]逐源对齐每个源使用自己的元数据。返回值返回dict仅含一个键documents值为Document列表——每个输入源对应一个Documentcontent字段存放完整转写文本。单独运行时这样读取结果result transcriber.run(sources[speech.wav]) print(result[documents][0].content)高级配置示例说话人分离 标点恢复 GPU中文会议录音的典型配置中文识别模型、VAD 切分、标点恢复、CAM 说话人分离并强制使用 GPU 推理from haystack.utils import ComponentDevice transcriber FunASRTranscriber( modelparaformer-zh, vad_modelfsmn-vad, punc_modelct-punc, spk_modelcam, deviceComponentDevice.from_str(cuda), )设置spk_model后结果Document的元数据中会带speakers键可用于区分不同说话人的转写段落。SenseVoice 逆文本正则化使用多语言 SenseVoice 模型并开启 ITNinverse text normalisation等生成选项transcriber FunASRTranscriber( modeliic/SenseVoiceSmall, generation_kwargs{use_itn: True, merge_vad: True, language: auto}, )use_itn会把一百二十三这类口语规范化成123merge_vad合并 VAD 片段languageauto自动识别语种——适合多语言混合的音频。生命周期方法warm_up 与序列化warm_upwarm_up() - None把 FunASR 模型加载进内存。模型在首次调用时从 ModelScope 下载并在本地缓存因此首次warm_up可能较慢。该方法幂等——重复调用是安全的Haystack 管道在正式run前通常会统一触发各组件warm_up避免推理时现场加载。to_dict / from_dictto_dict() - dict[str, Any] from_dict(data: dict[str, Any]) - FunASRTranscriberto_dict把组件序列化为字典from_dict反向反序列化还原组件实例。这是 Haystack 组件可序列化协议的一部分与 haystack/utils/base_serialization.py 中定义的标准组件序列化机制保持一致让组件状态模型名、VAD/标点/说话人模型、设备、批处理时长等配置可以随 YAML/JSON 管道定义保存与恢复。由于所有配置均通过构造参数固化to_dict/from_dict无需感知模型权重本身——权重只存在于~/.cache/modelscope本地缓存。在 Haystack 管道中编排音频转写FunASRTranscriber 最常见的管道位置是索引管道的第一组件先把音频转写成文本再交给后续的文本处理组件如清洗、切分、嵌入、写入文档存储。组件文档中给出了与LinkContentFetcher组合的示例——先抓取网络上的音频 URL再本地转写from haystack import Pipeline from haystack.components.fetchers import LinkContentFetcher from haystack_integrations.components.audio.funasr import FunASRTranscriber pipe Pipeline() pipe.add_component(fetcher, LinkContentFetcher()) pipe.add_component(transcriber, FunASRTranscriber()) pipe.connect(fetcher, transcriber) result pipe.run( data{ fetcher: { urls: [https://example.com/interview.wav], }, }, ) print(result[transcriber][documents][0].content)LinkContentFetcher源码见 haystack/components/fetchers/link_content.py抓取到的二进制音频以ByteStream形式流入transcriber天然满足run(sources...)对ByteStream输入的支持——这正是sources设计上同时接受文件路径与字节流的原因。如果要构建一个同时处理音频与非音频文件的索引管道还可以与 FileTypeRouter 搭配用mime_types[raudio/.*]之类的正则把音频文件路由到 FunASRTranscriber 分支文本/PDF 走常规转换分支实现多类型文件的分流处理。一个完整的音频索引管道示例把上述要素组合起来一个可落地的音频 → 可检索文本索引管道长这样from haystack import Pipeline from haystack.components.preprocessors import DocumentCleaner, DocumentSplitter from haystack.components.writers import DocumentWriter from haystack.document_stores.in_memory import InMemoryDocumentStore from haystack_integrations.components.audio.funasr import FunASRTranscriber document_store InMemoryDocumentStore() pipe Pipeline() pipe.add_component(transcriber, FunASRTranscriber(spk_modelcam)) pipe.add_component(cleaner, DocumentCleaner()) pipe.add_component(splitter, DocumentSplitter(split_bysentence, split_length150)) pipe.add_component(writer, DocumentWriter(document_storedocument_store)) pipe.connect(transcriber.documents, cleaner.documents) pipe.connect(cleaner.documents, splitter.documents) pipe.connect(splitter.documents, writer.documents) pipe.run({transcriber: {sources: [meeting.wav, interview.mp3]}})执行后转写文本经清洗、句子级切分后写入InMemoryDocumentStore后续即可接嵌入器与检索器构成 RAG。说话人分离元数据speakers会随Document一路保留可用于按说话人过滤检索结果。实践注意事项首次运行需联网模型首次从 ModelScope 下载并缓存在~/.cache/modelscope离线环境需要预先准备缓存或使用本地模型路径长音频推荐保留 VADvad_model默认开启会把长音频切成片段处理配合batch_size_s控制批处理内存若音频较短或希望整段直通可把vad_model设为None内存与吞吐权衡batch_size_s越大单批处理的音频越长吞吐越高但内存占用越大设备选择无 GPU 环境保持deviceNone走 CPU 即可有 NVIDIA GPU 时ComponentDevice.from_str(cuda)可获得明显加速Apple Silicon 可尝试mps本地优先整个转写链路不依赖任何外部 API音频内容不会上传适合会议纪要、访谈分析等对数据隐私有要求的场景。相关资源本文依据的 API 参考version-2.23 integrations-api/funasr.md最新版见 reference/integrations-api/funasr.md组件使用指南docs/pipeline-components/audio/funasrtranscriber.mdx音频组件总览docs/pipeline-components/audio.mdx设备抽象实现haystack/utils/device.py相关组件源码haystack/components/fetchers/link_content.py、haystack/components/routers/file_type_router.py【免费下载链接】haystackOpen-source AI orchestration framework for building context-engineered, production-ready LLM applications. Design modular pipelines and agent workflows with explicit control over retrieval, routing, memory, and generation. Built for scalable agents, RAG, multimodal applications, semantic search, and conversational systems.项目地址: https://gitcode.com/GitHub_Trending/ha/haystack创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表