Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Use Foundry Local's native audio transcription API to convert a local audio file into text. In this article, you create a console application that downloads a Whisper model, loads it, and streams transcription output.
Prerequisites
- .NET 8.0 SDK or later installed.
Samples repository
The complete sample code for this article is available in the Foundry Local GitHub repository. To clone the repository and navigate to the sample use:
git clone https://github.com/microsoft/Foundry-Local.git
cd Foundry-Local/samples/cs/audio-transcription-example
Install packages
If you're developing or shipping on Windows, select the Windows tab. The Windows package integrates with the Windows ML runtime — it provides the same API surface area with a wider breadth of hardware acceleration.
dotnet add package Microsoft.AI.Foundry.Local.WinML
dotnet add package OpenAI
The C# samples in the GitHub repository are preconfigured projects. If you're building from scratch, you should read the Foundry Local SDK reference for more details on how to set up your C# project with Foundry Local.
Transcribe an audio file
Copy and paste the following code into a C# file named Program.cs:
using Microsoft.AI.Foundry.Local;
var config = new Configuration
{
AppName = "foundry_local_samples",
LogLevel = Microsoft.AI.Foundry.Local.LogLevel.Information
};
// Initialize the singleton instance.
await FoundryLocalManager.CreateAsync(config, Utils.GetAppLogger());
var mgr = FoundryLocalManager.Instance;
// Ensure that any Execution Provider (EP) downloads run and are completed.
// EP packages include dependencies and may be large.
// Download is only required again if a new version of the EP is released.
// For cross platform builds there is no dynamic EP download and this will return immediately.
await Utils.RunWithSpinner("Registering execution providers", mgr.DownloadAndRegisterEpsAsync());
// Get the model catalog
var catalog = await mgr.GetCatalogAsync();
// Get a model using an alias and select the CPU model variant
var model = await catalog.GetModelAsync("whisper-tiny") ?? throw new System.Exception("Model not found");
var modelVariant = model.Variants.First(v => v.Info.Runtime?.DeviceType == DeviceType.CPU);
model.SelectVariant(modelVariant);
// Download the model (the method skips download if already cached)
await model.DownloadAsync(progress =>
{
Console.Write($"\rDownloading model: {progress:F2}%");
if (progress >= 100f)
{
Console.WriteLine();
}
});
// Load the model
Console.Write($"Loading model {model.Id}...");
await model.LoadAsync();
Console.WriteLine("done.");
// Get an audio client
var audioClient = await model.GetAudioClientAsync();
audioClient.Settings.Language = "en";
// Get a transcription with streaming outputs
var audioFile = args.Length > 0 ? args[0] : Path.Combine(AppContext.BaseDirectory, "Recording.mp3");
Console.WriteLine($"Transcribing audio with streaming output: {Path.GetFileName(audioFile)}");
var response = audioClient.TranscribeAudioStreamingAsync(audioFile, CancellationToken.None);
await foreach (var chunk in response)
{
Console.Write(chunk.Text);
Console.Out.Flush();
}
Console.WriteLine();
// Tidy up - unload the model
await model.UnloadAsync();
The sample includes a Recording.mp3 file. To transcribe a different audio file, pass the file path as an argument.
dotnet run
To transcribe a custom audio file:
dotnet run -- path/to/audio.mp3
Prerequisites
- Node.js version 20 or later installed.
Samples repository
The complete sample code for this article is available in the Foundry Local GitHub repository. To clone the repository and navigate to the sample use:
git clone https://github.com/microsoft/Foundry-Local.git
cd Foundry-Local/samples/js/audio-transcription-example
Install packages
If you're developing or shipping on Windows, select the Windows tab. The Windows package integrates with the Windows ML runtime — it provides the same API surface area with a wider breadth of hardware acceleration.
npm install foundry-local-sdk-winml openai
Transcribe an audio file
Copy and paste the following code into a JavaScript file named app.js:
import { FoundryLocalManager } from 'foundry-local-sdk';
// Initialize the Foundry Local SDK
console.log('Initializing Foundry Local SDK...');
const manager = FoundryLocalManager.create({
appName: 'foundry_local_samples',
logLevel: 'info'
});
console.log('✓ SDK initialized successfully');
// Get the model object
const modelAlias = 'whisper-tiny'; // Using an available model from the list above
let model = await manager.catalog.getModel(modelAlias);
console.log(`Using model: ${model.id}`);
// Download the model
console.log(`\nDownloading model ${modelAlias}...`);
await model.download((progress) => {
process.stdout.write(`\rDownloading... ${progress.toFixed(2)}%`);
});
console.log('\n✓ Model downloaded');
// Load the model
console.log(`\nLoading model ${modelAlias}...`);
await model.load();
console.log('✓ Model loaded');
// Create audio client
console.log('\nCreating audio client...');
const audioClient = model.createAudioClient();
console.log('✓ Audio client created');
// Example audio transcription
const audioFile = process.argv[2] || './Recording.mp3';
console.log(`\nTranscribing ${audioFile}...`);
const transcription = await audioClient.transcribe(audioFile);
console.log('\nAudio transcription result:');
console.log(transcription.text);
console.log('✓ Audio transcription completed');
// Same example but with streaming transcription using async iteration
console.log('\nTesting streaming audio transcription...');
for await (const result of audioClient.transcribeStreaming(audioFile)) {
// Output the intermediate transcription results as they are received without line ending
process.stdout.write(result.text);
}
console.log('\n✓ Streaming transcription completed');
// Unload the model
console.log('Unloading model...');
await model.unload();
console.log(`✓ Model unloaded`);
The sample includes a Recording.mp3 file. To transcribe a different audio file, pass the file path as an argument.
To run the application, use the following command in your terminal:
node app.js
To transcribe a custom audio file:
node app.js path/to/audio.mp3
Prerequisites
- Python 3.11 or later installed.
Samples repository
The complete sample code for this article is available in the Foundry Local GitHub repository. To clone the repository and navigate to the sample use:
git clone https://github.com/microsoft/Foundry-Local.git
cd Foundry-Local/samples/python/audio-transcription
Install packages
If you're developing or shipping on Windows, select the Windows tab. The Windows package integrates with the Windows ML runtime — it provides the same API surface area with a wider breadth of hardware acceleration.
pip install foundry-local-sdk-winml openai
Transcribe an audio file
Copy and paste the following code into a Python file named app.py:
import sys
from foundry_local_sdk import Configuration, FoundryLocalManager
# Initialize the Foundry Local SDK
config = Configuration(app_name="foundry_local_samples")
FoundryLocalManager.initialize(config)
manager = FoundryLocalManager.instance
# Load the whisper model for speech-to-text
model = manager.catalog.get_model("whisper-tiny")
model.download(
lambda progress: print(
f"\rDownloading model: {progress:.2f}%",
end="",
flush=True,
)
)
print()
model.load()
print("Model loaded.")
# Get the audio client and transcribe
audio_client = model.get_audio_client()
audio_file = sys.argv[1] if len(sys.argv) > 1 else "Recording.mp3"
result = audio_client.transcribe(audio_file)
print("Transcription:")
print(result.text)
# Clean up
model.unload()
The sample includes a Recording.mp3 file. To transcribe a different audio file, pass the file path as an argument.
Run the code by using the following command:
python app.py
To transcribe a custom audio file:
python app.py path/to/audio.mp3
Prerequisites
- Rust and Cargo installed (Rust 1.70.0 or later).
Samples repository
The complete sample code for this article is available in the Foundry Local GitHub repository. To clone the repository and navigate to the sample use:
git clone https://github.com/microsoft/Foundry-Local.git
cd Foundry-Local/samples/rust/audio-transcription-example
Install packages
If you're developing or shipping on Windows, select the Windows tab. The Windows package integrates with the Windows ML runtime — it provides the same API surface area with a wider breadth of hardware acceleration.
cargo add foundry-local-sdk --features winml
cargo add tokio --features full
cargo add tokio-stream anyhow
Transcribe an audio file
Replace the contents of main.rs with the following code:
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
use std::env;
use std::io::{self, Write};
use foundry_local_sdk::{FoundryLocalConfig, FoundryLocalManager};
use tokio_stream::StreamExt;
const ALIAS: &str = "whisper-tiny";
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Audio Transcription Example");
println!("===========================\n");
// Accept an optional audio file path as a CLI argument, defaulting to Recording.mp3.
let audio_path = env::args()
.nth(1)
.unwrap_or_else(|| "Recording.mp3".to_string());
// ── 1. Initialise the manager ────────────────────────────────────────
let manager = FoundryLocalManager::create(FoundryLocalConfig::new("foundry_local_samples"))?;
// ── 2. Pick the whispermodel and ensure it is downloaded ────────────
let model = manager.catalog().get_model(ALIAS).await?;
println!("Model: {} (id: {})", model.alias(), model.id());
if !model.is_cached().await? {
println!("Downloading model...");
model
.download(Some(|progress: f64| {
print!("\r {progress:.1}%");
io::stdout().flush().ok();
}))
.await?;
println!();
}
println!("Loading model...");
model.load().await?;
println!("✓ Model loaded\n");
// ── 3. Create an audio client────────────────────────────────────────
let audio_client = model.create_audio_client();
// ── 4. Non-streaming transcription ───────────────────────────────────
println!("--- Non-streaming transcription ---");
let result = audio_client.transcribe(&audio_path).await?;
println!("Transcription: {}", result.text);
// ── 5. Streaming transcription ───────────────────────────────────────
println!("--- Streaming transcription ---");
print!("Transcription: ");
let mut stream = audio_client.transcribe_streaming(&audio_path).await?;
while let Some(chunk) = stream.next().await {
let chunk = chunk?;
print!("{}", chunk.text);
io::stdout().flush().ok();
}
println!("\n");
// ── 6. Unload the model──────────────────────────────────────────────
println!("Unloading model...");
model.unload().await?;
println!("Done.");
Ok(())
}
The sample includes a Recording.mp3 file. To transcribe a different audio file, pass the file path as an argument.
Run the code by using the following command:
cargo run
To transcribe a custom audio file:
cargo run -- path/to/audio.mp3