语言
Important
Windows ML 运行时和语音识别 API 目前是实验性的,不支持在生产环境中使用。 尝试这些 API 的应用不应发布到 Microsoft 应用商店。
Important
Windows ML 运行时和语音识别 API 目前在 C++ 和 Python中可用。 C# 目前通过一个复制到项目中的临时包装器获得支持;未来的更新将原生包含 C# API。 请参阅 设置项目。
Windows ML 语音识别 API 简化了在Windows ML 运行时 API 的基础上,通过自己的语音模型运行自动语音识别(ASR)。 它为你处理转录循环、流式处理输出和取消。 Windows 不会为此 API 提供模型;你需要自行选择、打包,并随你的应用一起分发该模型。 它是Windows ML 任务 API之一。
Tip
如果你不需要特定的语音模型或自定义部署,并且只需使用模型Microsoft管理的模型进行设备上的语音转文本,请改用现成的语音识别 API。
有两种方法可以指定用于语音识别的模型:
- 从检查点文件夹中 (Windows ML 2.9)。 使用
LoadModelFromPath加载已发布的语音模型文件夹(如 Whisper 或 Nemotron),并使用 GPU 执行目标调用SetModel。 Windows ML 使用 CGC 后端为 GPU 编译该模型。 显式传递模型的 tokenizer。 请参阅 “选择任务如何运行模型”。 - 从你构建的管道中。 对于 Whisper 模型,请创建基础运行时对象(编码器和解码器模型、执行目标和管道),并将其传递给会话。 这种方式不会隐藏硬件放置;它只是让你无需自行编写解码循环。
支持的模型
检查点文件夹路径指向已发布的语音模型文件夹。 该文件夹需要 config.json、tokenizer.json 中的权重(或 tokenizer_config.json 及其列出的每个分片),以及 tokenizer 文件,例如 model.safetensors 和 model.safetensors.index.json。 Whisper 文件夹还需要no_timestamps_token_id,除非config.json包含generation_config.json。 对于 Windows ML 不支持的模型,运行会失败并返回 HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED)。
管道路径特定于 Whisper 编码器/解码器体系结构。 你需要提供通过 ONNX Runtime 运行的编码器和解码器模型(encoder_model.onnx 和 decoder_model.onnx),另加一个 tokenizer 文件。
来自检查点文件夹的会话接受采用模型采样率的单声道波形,或者在模型转换输入时接受从 8,000 到 48,000 Hz 的速率。 输入张量包含单声道浮点 PCM 样本以及采样率和有效长度元数据。
Note
Windows ML 使用 CGC 后端运行模型时,请从建议的语音模型开始:低语中、低声小(示例模型)和 Nemotron 3.5 ASR 流式处理 0.6B。 尚未验证其他语音模型。
实时输入
在应用录制时接收音频的模型,例如 Nemotron 这样的流式模型,会提供实时会话(IWinMLAutomaticSpeechRecognitionLiveSession)。 你启动实时输入、在捕获音频区块时将其写入,并在模型提交转录片段时读取这些片段。 实时输入使用有界背压:WriteChunk 报告任务是否接受该数据块。 查询会话中的实时接口以测试是否支持,因为识别固定长度窗口中的语音的模型(如 Whisper)不会暴露该接口。
语言、模式和预测
语音识别配置可以设置语言、模式以及是否报告预测。 语言支持取决于模型。
-
Mode.
TRANSCRIBE(默认值)将语音按所说语言转写为文本。TRANSLATE将其写入为英语文本。 从检查点文件夹创建的会话仅在 Windows ML 2.9 中转录,因此TRANSLATE在CreateSession中失败,并出现HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED)。 当编码器解码器组合的分词器将<|translate|>映射到标识符 50358 时,该编码器解码器组合会翻译。 - 预测。 选择启用已识别文本后面的临时文本,该文本可能会在后续识别过程中被修改。 默认情况下,它处于关闭状态。 在固定长度窗口中识别语音的模型(如 Whisper)以及编码器-解码器组合都不会报告任何预测。 在没有预测的情况下,每个更新都会在模型提交该段时提供该段的文本,Windows ML 永远不会对其进行修改。 除第一个片段外,每个片段都以一个空格开头,因此操作的各个片段会串联成其转录文本。
Note
Windows ML 2.7(如 Whisper 配置和输入接口)中特定于低语的语音识别名称现在是编码器解码器名称,具有相同的接口 ID。 Python投影不再包括whisper_bindings、transcribe_whisper_log_mel或WhisperAutomaticSpeechRecognitionBindings。 必须重新构建实现 IWinMLAutomaticSpeechRecognitionConfiguration 的代码,因为该接口新增了语言、模式和预测方法。
从检查点文件夹转录
使用 LoadModelFromPath 加载语音模型的检查点文件夹,使用模型的标记化器创建任务,并使用 GPU 执行目标进行调用 SetModel 。 这些示例使用 Whisper tiny;将它们指向另一个受支持的语音模型文件夹,如 Nemotron,以使用该模型。 与文本生成不同,语音识别显式采用 tokenizer。 设置语言和模式,然后创建会话:
// Temporary: WinMLRuntime and the other wrapper types in the WinMLSamples.Runtime
// namespaces aren't in the Windows ML package yet. Copy the wrapper files from
// https://github.com/microsoft/WindowsML/tree/main/Samples/Runtime/shared/csharp
// into your project. A future update will include the C# APIs natively.
using WinMLSamples.Runtime;
using WinMLSamples.Runtime.Tasks;
using WinMLRuntime runtime = WinMLRuntime.Create();
using ExecutionTarget target = runtime.CreateExecutionTarget(
ExecutionTargetKind.Gpu, ExecutionTargetPreference.Performance);
using Model model = runtime.LoadModelFromPath(@"C:\models\whisper-tiny");
using Tokenizer tokenizer = Tokenizer.CreateFromFile(@"C:\models\whisper-tiny\tokenizer.json");
using WinMLTasks tasks = WinMLTasks.Create(runtime);
using AutomaticSpeechRecognitionTask task =
tasks.AutomaticSpeechRecognition.CreateAutomaticSpeechRecognitionTask(tokenizer);
// Bind the model to the configuration on the GPU target.
using SpeechRecognitionConfiguration configuration = task.CreateConfiguration();
using (ModelSpeechRecognitionConfiguration modelConfiguration =
configuration.As<ModelSpeechRecognitionConfiguration>())
{
modelConfiguration.SetModel(target, model);
}
// Without a language, the model's default applies.
configuration.Language = "en";
configuration.Mode = SpeechRecognitionMode.Transcribe;
configuration.Validate();
// A SpeechRecognizer keeps the session on a thread of its own and streams the transcript.
await using SpeechRecognizer recognizer =
await SpeechRecognizer.CreateAsync(tasks, task, configuration);
若要从任一路径流式传输转录文本,请调用 TranscribeWaveform,如下一部分所示,在 C# SpeechRecognizer 中使用 await foreach 流式传输 SpeechRecognitionUpdate 值。
使用 Whisper 管道转录音频
以下示例使用 Whisper 编码器解码器组合。 提供包含波形样本的运行时张量。 加载 Whisper 编码器和解码器模型,为每个模型生成管道,并使用 tokenizer 将它们组合到会话中:
// Temporary: WinMLRuntime and the other wrapper types in the WinMLSamples.Runtime
// namespaces aren't in the Windows ML package yet. Copy the wrapper files from
// https://github.com/microsoft/WindowsML/tree/main/Samples/Runtime/shared/csharp
// into your project. A future update will include the C# APIs natively.
using WinMLSamples.Runtime;
using WinMLSamples.Runtime.Tasks;
// Create the runtime and an execution target (CPU here; GPU/NPU also available).
using WinMLRuntime runtime = WinMLRuntime.Create();
using ExecutionTarget target = runtime.CreateCpuExecutionTarget();
// Load the Whisper encoder as its own single-stage pipeline.
using Model encoderModel = runtime.LoadModelFromFile(@"C:\models\whisper\encoder_model.onnx");
using PipelineBuilder encoderBuilder = runtime.CreatePipelineBuilder();
using Stage encoderStage = encoderBuilder.AddModelStage(encoderModel, target, "whisper-encoder");
using Pipeline encoderPipeline = encoderBuilder.Build();
// Load the Whisper decoder the same way.
using Model decoderModel = runtime.LoadModelFromFile(@"C:\models\whisper\decoder_model.onnx");
using PipelineBuilder decoderBuilder = runtime.CreatePipelineBuilder();
using Stage decoderStage = decoderBuilder.AddModelStage(decoderModel, target, "whisper-decoder");
using Pipeline decoderPipeline = decoderBuilder.Build();
using Tokenizer tokenizer = Tokenizer.CreateFromFile(@"C:\models\whisper\tokenizer.json");
// Compose the encoder/decoder pipelines and tokenizer into a session.
using WinMLTasks tasks = WinMLTasks.Create(runtime);
using AutomaticSpeechRecognitionTask task =
tasks.AutomaticSpeechRecognition.CreateAutomaticSpeechRecognitionTask(tokenizer);
using SpeechRecognitionConfiguration configuration = task.CreateConfiguration();
using (EncoderDecoderSpeechRecognitionConfiguration whisper =
configuration.As<EncoderDecoderSpeechRecognitionConfiguration>())
{
whisper.SetBindings(new EncoderDecoderSpeechRecognitionBindings
{
EncoderPipeline = encoderPipeline,
EncoderStage = encoderStage,
DecoderPipeline = decoderPipeline,
DecoderStage = decoderStage,
TensorTarget = target,
});
}
configuration.Validate();
// A SpeechRecognizer keeps the session on a thread of its own and streams the transcript.
await using SpeechRecognizer recognizer =
await SpeechRecognizer.CreateAsync(tasks, task, configuration);
调用 TranscribeWaveform 以启动流式处理操作,然后从返回的流拉取片段,直到它报告完成:
// Cancelling the token stops transcription in the Runtime.
CancellationTokenSource cancellation = new();
// Read transcript text one fragment at a time until the stream completes.
await foreach (SpeechRecognitionUpdate update in
recognizer.TranscribeWaveformAsync(waveform, waveformMetadata, cancellation.Token))
{
Console.Write(update.Text);
}
// The outcome is the authoritative transcript and finish reason.
SpeechRecognitionOutcome outcome = recognizer.LastOutcome!;
Console.WriteLine($"\nFinished because: {outcome.FinishReason}");
// Closing the session reports a failure that disposing it would not.
await recognizer.CloseAsync();
waveform 和 waveformMetadata 来自你自己的音频加载代码:前者是一个包含单声道浮点 PCM 样本的运行时张量,后者是描述该张量的采样率和有效长度元数据。 在 C# 中, target.AudioTensorFactory 创建音频张量。
完成的结果报告完整转录文本和完成原因,例如转录文本结束令牌、取消或错误。
C#、C++ 和 Python
本机 API 通过 WinMLTasks.h 以及 winml/tasks/speech/asr/WhisperAutomaticSpeechRecognition.hpp 下的类型化 C++ 帮助程序公开。 Python 应用程序使用 windowsml.tasks 投影以及 windowsml.runtime。 C# 应用程序通过复制到项目中的包装器使用 CsWin32 绑定;请参阅 “设置项目”。
Windows ML 示例包括 C++、C# 和Python用于指定模型的两种方式的语音识别示例。 这些示例显示语音识别配置旁边的完整运行时设置,以便查看抽象的开始位置以及如何自定义基础工作流。