Language
Important
Windows ML Runtime と音声認識 API は現在、実験的な機能であり、運用環境での使用はサポートされていません。 これらの API を試しているアプリは、Microsoft Store に公開しないでください。
Important
Windows ML ランタイム API と音声認識 API は、現在 C++ および Python で使用できます。 C# は、プロジェクトにコピーする一時的なラッパーによってサポートされます。今後の更新プログラムには、C# API がネイティブに含まれる予定です。 プロジェクトを設定するを参照してください。
Windows ML 音声認識 API を使用すると、Windows ML ランタイム API に基づいて、独自の音声認識モデルで自動音声認識 (ASR) を簡単に実行できます。 文字起こしループ、ストリーミング出力、取り消しを処理します。 この API 用のモデルは Windows には付属していません。モデルはご自身で選択し、パッケージ化して、アプリと共に配布します。 これは、Windows ML タスク API の 1 つです。
Tip
特定の音声モデルやカスタムデプロイが不要で、Microsoftが管理するモデルを使用してデバイス上で音声のテキスト化を行うだけの場合は、代わりにすぐに使用できる音声認識 APIを使用します。
音声認識のモデルを指定するには、次の 2 つの方法があります。
- チェックポイント フォルダーから (Windows ML 2.9)。 パブリッシュされた音声モデル フォルダー (Whisper や Nemotron など) を
LoadModelFromPathで読み込み、GPU 実行ターゲットを指定してSetModelを呼び出します。 Windows ML は、CGC バックエンドを使用して GPU 用にモデルをコンパイルします。 モデルのトークナイザーを明示的に渡します。 詳細は、タスクがモデルを実行する方法を選択するを参照してください。 - パイプラインから築き上げる。 ささやきモデルの場合は、基になるランタイム オブジェクト (エンコーダーとデコーダー モデル、実行ターゲット、パイプライン) を作成し、セッションに渡します。 このパスでは、ハードウェアの配置は非表示になりません。独自のデコード ループを記述する必要がなくなるだけです。
サポートされているモデル
checkpoint-folder パスは、公開された音声モデル フォルダーを受け取ります。 フォルダーには、 config.json、 model.safetensors 内の重み (または名前が付いているすべてのシャードで model.safetensors.index.json )、トークナイザー ファイル ( tokenizer.json や tokenizer_config.jsonなど) が必要です。 Whisper フォルダーにも generation_config.json が必要です。ただし、 config.json に no_timestamps_token_id が含まれている場合はこの限りではありません。 Windows ML がサポートしていないモデルは、HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED)で失敗します。
パイプライン パスは、Whisper エンコーダー/デコーダー アーキテクチャに固有です。 ONNX ランタイムを介して実行される ONNX エンコーダーとデコーダー モデル (encoder_model.onnx と decoder_model.onnx) とトークナイザー ファイルを提供します。
チェックポイント フォルダーからのセッションでは、モデルのサンプル レートまたはモデルが入力を変換するときに 8,000 ~ 48,000 Hz の速度でモノラル波形を受け入れます。 入力テンソルには、モノラル浮動小数点 PCM サンプルに加えて、サンプル レートと有効な長さのメタデータが保持されます。
Note
Windows ML で CGC バックエンドを使用してモデルを実行する場合は、推奨される音声モデル(Whisper Medium、Whisper tiny(サンプルのモデル)、および Nemotron 3.5 ASR Streaming 0.6B)から開始します。 他の音声モデルはまだ検証されていません。
ライブ入力
アプリが記録するオーディオを受け入れるモデル (Nemotron などのストリーミング モデルなど) は、ライブ セッション (IWinMLAutomaticSpeechRecognitionLiveSession) を公開します。 ライブ入力を開始し、キャプチャ時にオーディオ チャンクを書き込み、モデルがコミットするときにトランスクリプト フラグメントを読み取ります。 ライブ入力では、境界付きバックプレッシャが使用されます。 WriteChunk は、タスクがチャンクを受け入れたかどうかを報告します。 サポート状況をテストするには、セッションからライブ インターフェースを照会してください。Whisper などの固定長ウィンドウで音声を認識する認識エンジンでは、このインターフェースが公開されていないためです。
言語、モード、予測
音声認識構成では、言語、モード、予測を報告するかどうかを設定できます。 言語のサポートはモデルによって異なります。
-
モード
TRANSCRIBE(デフォルト) では、音声が話された言語でテキストとして書き出されます。TRANSLATEは英語のテキストとして書き込みます。 チェックポイント フォルダーから作成されたセッションは、Windows ML 2.9 でのみ文字起こしできるため、TRANSLATEはCreateSessionでHRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED)に失敗します。 エンコーダー デコーダーコンポジションは、トークナイザーが<|translate|>を識別子 50358 にマップするときに翻訳されます。 - 予測。 認識されたテキストの後に続く暫定的なテキストを有効にし、そのテキストは後の認識処理でリビジョンされる可能性があります。 既定ではオフになっています。 Whisper など固定長ウィンドウで音声を認識する認識エンジンや、エンコーダー デコーダー構成のモデルは、予測結果を報告しません。 予測がない場合、各更新では、モデルが確定した時点のセグメントのテキストが配信され、Windows ML がそれを後から修正することはありません。 最初の後のすべてのセグメントは 1 つのスペースで始まるので、操作のフラグメントはそのトランスクリプトに連結されます。
Note
Windows ML 2.7 の Whisper 固有の音声認識名 (Whisper 構成や入力インターフェイスなど) は、同じインターフェイス ID を持つエンコーダー デコーダー名になりました。 Pythonプロジェクションには、whisper_bindings、transcribe_whisper_log_mel、またはWhisperAutomaticSpeechRecognitionBindingsが含まれなくなりました。 インターフェイスが言語、モード、予測のメソッドを取得したため、 IWinMLAutomaticSpeechRecognitionConfiguration を実装するコードを再構築する必要があります。
チェックポイント フォルダーからの文字起こし
LoadModelFromPathで音声モデルのチェックポイント フォルダーを読み込み、モデルのトークナイザーを使用してタスクを作成し、GPU 実行ターゲットでSetModelを呼び出します。 例では Whisper tiny を使用していますが、別のサポートされている音声モデル フォルダー (Nemotron など) を指定すると、そのモデルを使用できます。 テキスト生成とは異なり、音声認識はトークナイザーを明示的に受け取ります。 言語とモードを設定し、セッションを作成します。
// 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 ストリーミング SpeechRecognitionUpdate 値を await foreachします。
Whisper パイプラインを使用してオーディオを文字起こしする
次の例では、Whisper encoder-decoder composition を使用します。 波形サンプルを保持するランタイム テンソルを提供します。 Whisperエンコーダー モデルとデコーダー モデルを読み込み、それぞれのパイプラインを構築し、トークナイザーと組み合わせてセッションを構成します。
// 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.runtimeプロジェクションをwindowsml.tasksと共に使用します。 C# アプリケーションでは、今のところプロジェクトにコピーするラッパーを介して CsWin32 バインドが使用されます。 「プロジェクトを設定する」を参照してください。
Windows ML サンプルには、モデルを指定する両方の方法の C++、C#、およびPython音声認識のサンプルが含まれます。 サンプルでは、音声認識構成の横に完全なランタイム 設定が示されているため、抽象化が開始される場所と、基になるワークフローをカスタマイズする方法を確認できます。