语音识别模块 - 基于Whisper模型的多语言语音转文字解决方案
openai/whisper-large-v3-turboopenai/whisper-large-v3openai/whisper-medium(中文支持一般)openai/whisper-small(中文输出繁体字)输入列名 | 说明 |
|---|---|
audios | 包含音频数据的数组,支持以下格式: - audio_base64: base64编码的音频字符串; - audio_url: 音频文件URL路径; - audio_binary: 原始音频字节数据 |
languages | 每个音频对应语种的数组,语种缩写请参考https://github.com/ggml-org/whisper.cpp/blob/d682e150908e10caa4c15883c633d7902d385237/src/whisper.cpp#L248。如果该处不传入,则使用参数中的source_language设置。 |
处理后的结构化数组,每个元素包含以下字段:
如参数没有默认值,则为必填参数
参数名称 | 类型 | 默认值 | 描述 |
|---|---|---|---|
audio_src_type | str | 音频格式类型 支持的音频格式类型,包含: - tos/http 地址(audio_url) - base64 编码(audio_base64) - 二进制流(audio_binary) 可选值:["audio_binary", "audio_url", "audio_base64"] | |
model_path | str | /opt/las/models | 模型存储路径 默认值:"/opt/las/models" |
model_name | str | openai/whisper-large-v3 | 模型名称 支持的Whisper系列模型: - whisper-small: 小模型 - whisper-medium: 中等模型 - whisper-large-v3: 最新大模型 - whisper-large-v3-turbo: 优化版大模型 可选值:[ "openai/whisper-small", "openai/whisper-medium", "openai/whisper-large-v3-turbo", "openai/whisper-large-v3" ] 默认值:"openai/whisper-large-v3" |
batch_size | int | 10 | 单次处理的音频样本数量 默认值:10 |
source_language | str | None | 音频源语言 支持:chinese/english/japanese/korean等,可以设置为None以启用自动检测 默认值:None |
translate_to_english | bool | False | 英文翻译模式 是否将识别结果翻译为英文 启用后输出文本将为英文翻译结果 默认值:False |
condition_on_prev_tokens | bool | True | 历史依赖模式 是否基于历史token进行预测 关闭后会降低结果连贯性但提升处理速度 默认值:True |
compression_ratio_threshold | float | 1.35 | 文本压缩阈值 控制生成文本的压缩程度(建议范围1.2-2.0) 值越大保留的重复内容越多 默认值:1.35 |
temperature | float | 0.5 | 温度系数 控制生成文本的随机性(0.0-1.0) 较高值适合创造性场景,较低值适合确定性场景 默认值:0.5 |
logprob_threshold | float | -1.0 | 对数概率阈值 对数概率阈值,过滤置信度过低的词。若词的对数概率低于此值,可能被拒绝。 默认为-1.0,不启用过滤,保留所有词。 |
dtype | str | bfloat16 | 计算精度类型 模型推理使用的数值精度: - bfloat16: 平衡精度与速度(默认) - float16: 更快的推理速度 - float32: 最高精度 可选值:["bfloat16", "float16", "float32"] 默认值:"bfloat16" |
rank | int | 0 | GPU设备编号 指定使用的GPU设备ID(多卡环境生效) 默认使用首张显卡(ID=0) |
下面的代码展示了如何使用 daft 运行算子将语音转换为文字。
from __future__ import annotations import logging import os import ray import daft from daft import col from daft.las.functions.audio.audio_asr_whisper import AudioAsrWhisper from daft.las.functions.udf import las_udf if __name__ == "__main__": if os.getenv("DAFT_RUNNER", "ray") == "ray": def configure_logging(): logging.basicConfig( level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S.%s".format(), ) logging.getLogger("tracing.span").setLevel(logging.WARNING) logging.getLogger("daft_io.stats").setLevel(logging.WARNING) logging.getLogger("DaftStatisticsManager").setLevel(logging.WARNING) logging.getLogger("DaftFlotillaScheduler").setLevel(logging.WARNING) logging.getLogger("DaftFlotillaDispatcher").setLevel(logging.WARNING) import ray ray.init(dashboard_host="0.0.0.0", runtime_env={"worker_process_setup_hook": configure_logging}) daft.context.set_runner_ray() daft.set_execution_config(actor_udf_ready_timeout=600) daft.set_execution_config(min_cpu_per_task=0) tos_dir_url = os.getenv("TOS_DIR_URL", "las-cn-beijing-public-online.tos-cn-beijing.volces.com") samples = { "audio_path": [ f"https://{tos_dir_url}/public/shared_audio_dataset/参观八达岭长城。.wav" ] } model_path = os.getenv("MODEL_PATH", "/opt/las/models") model_name = "openai/whisper-large-v3" audio_src_type = "audio_url" dtype = "bfloat16" source_language = "chinese" translate_to_english = False condition_on_prev_tokens = True compression_ratio_threshold = 1.35 temperature = 0.5 logprob_threshold = -1.0 batch_size = 1 rank = 0 df = daft.from_pydict(samples) df = df.with_column( "asr_result_detail", las_udf( AudioAsrWhisper, construct_args={ "audio_src_type": audio_src_type, "model_path": model_path, "model_name": model_name, "dtype": dtype, "source_language": source_language, "translate_to_english": translate_to_english, "condition_on_prev_tokens": condition_on_prev_tokens, "compression_ratio_threshold": compression_ratio_threshold, "temperature": temperature, "logprob_threshold": logprob_threshold, "batch_size": batch_size, "rank": rank, }, num_gpus=1, batch_size=1, concurrency=1, )(col("audio_path")), ) df.show() # ╭────────────────────────────────┬─────────────────────────────────────────────────────────────╮ # │ audio_path ┆ asr_result_detail │ # │ --- ┆ --- │ # │ Utf8 ┆ Struct[asr_result: Utf8, timestamps: List[List[Float32]], │ # │ ┆ segments: List[Utf8]] │ # ╞════════════════════════════════╪═════════════════════════════════════════════════════════════╡ # │ tos://las-cn-beijing-publi-… ┆ {asr_result: 参观八道岭长城, │ # │ ┆ timesta… │ # ╰────────────────────────────────┴─────────────────────────────────────────────────────────────╯