-
Notifications
You must be signed in to change notification settings - Fork 330
Expand file tree
/
Copy pathProgram.cs
More file actions
118 lines (100 loc) · 3.19 KB
/
Copy pathProgram.cs
File metadata and controls
118 lines (100 loc) · 3.19 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// <complete_code>
// <imports>
using Microsoft.AI.Foundry.Local;
using Betalgo.Ranul.OpenAI.ObjectModels.RequestModels;
using Microsoft.Extensions.Logging;
using System.Text;
// </imports>
// <init>
CancellationToken ct = CancellationToken.None;
var config = new Configuration
{
AppName = "foundry_local_samples",
LogLevel = Microsoft.AI.Foundry.Local.LogLevel.Information
};
using var loggerFactory = LoggerFactory.Create(builder =>
{
builder.SetMinimumLevel(
Microsoft.Extensions.Logging.LogLevel.Information
);
});
var logger = loggerFactory.CreateLogger<Program>();
// Initialize the singleton instance
await FoundryLocalManager.CreateAsync(config, logger);
var mgr = FoundryLocalManager.Instance;
// Download and register all execution providers.
var currentEp = "";
await mgr.DownloadAndRegisterEpsAsync((epName, percent) =>
{
if (epName != currentEp)
{
if (currentEp != "") Console.WriteLine();
currentEp = epName;
}
Console.Write($"\r {epName.PadRight(30)} {percent,6:F1}%");
});
if (currentEp != "") Console.WriteLine();
var catalog = await mgr.GetCatalogAsync();
// </init>
// <transcription>
// Load the speech-to-text model
var speechModel = await catalog.GetModelAsync("whisper-tiny")
?? throw new Exception("Speech model not found");
await speechModel.DownloadAsync(progress =>
{
Console.Write($"\rDownloading speech model: {progress:F2}%");
if (progress >= 100f) Console.WriteLine();
});
await speechModel.LoadAsync();
Console.WriteLine("Speech model loaded.");
// Transcribe the audio file
var audioClient = await speechModel.GetAudioClientAsync();
var transcriptionText = new StringBuilder();
Console.WriteLine("\nTranscription:");
var audioResponse = audioClient
.TranscribeAudioStreamingAsync("meeting-notes.wav", ct);
await foreach (var chunk in audioResponse)
{
Console.Write(chunk.Text);
transcriptionText.Append(chunk.Text);
}
Console.WriteLine();
// Unload the speech model to free memory
await speechModel.UnloadAsync();
// </transcription>
// <summarization>
// Load the chat model for summarization
var chatModel = await catalog.GetModelAsync("qwen2.5-0.5b")
?? throw new Exception("Chat model not found");
await chatModel.DownloadAsync(progress =>
{
Console.Write($"\rDownloading chat model: {progress:F2}%");
if (progress >= 100f) Console.WriteLine();
});
await chatModel.LoadAsync();
Console.WriteLine("Chat model loaded.");
// Summarize the transcription into organized notes
var chatClient = await chatModel.GetChatClientAsync();
var messages = new List<ChatMessage>
{
new ChatMessage
{
Role = "system",
Content = "You are a note-taking assistant. Summarize " +
"the following transcription into organized, " +
"concise notes with bullet points."
},
new ChatMessage
{
Role = "user",
Content = transcriptionText.ToString()
}
};
var chatResponse = await chatClient.CompleteChatAsync(messages, ct);
var summary = chatResponse.Choices[0].Message.Content;
Console.WriteLine($"\nSummary:\n{summary}");
// Clean up
await chatModel.UnloadAsync();
Console.WriteLine("\nDone. Models unloaded.");
// </summarization>
// </complete_code>