-
Notifications
You must be signed in to change notification settings - Fork 309
Expand file tree
/
Copy pathProgram.cs
More file actions
79 lines (60 loc) · 2.22 KB
/
Program.cs
File metadata and controls
79 lines (60 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// <complete_code>
// <imports>
using Microsoft.AI.Foundry.Local;
// </imports>
// <init>
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());
// </init>
// <model_setup>
// 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.");
// </model_setup>
// <transcription>
// 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();
// </transcription>
// <cleanup>
// Tidy up - unload the model
await model.UnloadAsync();
// </cleanup>
// </complete_code>