Repository navigation
Expand file tree
/
Copy pathModelManager.cs
More file actions
147 lines (126 loc) · 6.31 KB
/
Copy pathModelManager.cs
File metadata and controls
147 lines (126 loc) · 6.31 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System.IO;
using System.Net.Http;
using System.Security.Cryptography;
namespace WhisperTyper
{
public enum ModelStatus { NotDownloaded, Downloading, Downloaded, Loaded }
public class KnownModel
{
public string FileName { get; init; } = "";
public string DisplayName { get; init; } = "";
public string SizeLabel { get; init; } = "";
public string Notes { get; init; } = "";
// Computed at runtime
public ModelStatus Status { get; set; } = ModelStatus.NotDownloaded;
public double Progress { get; set; } // 0–100 during download
public string LocalPath { get; set; } = "";
}
public class ModelManager
{
private const string BaseUrl = "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/";
public static readonly KnownModel[] Catalog =
[
new() { FileName = "ggml-tiny.bin", DisplayName = "Tiny", SizeLabel = "75 MB", Notes = "Fastest, lowest accuracy" },
new() { FileName = "ggml-tiny.en.bin", DisplayName = "Tiny (English)", SizeLabel = "75 MB", Notes = "English-only" },
new() { FileName = "ggml-base.bin", DisplayName = "Base", SizeLabel = "142 MB", Notes = "Good for general use" },
new() { FileName = "ggml-base.en.bin", DisplayName = "Base (English)", SizeLabel = "142 MB", Notes = "English-only" },
new() { FileName = "ggml-small.bin", DisplayName = "Small", SizeLabel = "466 MB", Notes = "" },
new() { FileName = "ggml-small.en.bin", DisplayName = "Small (English)", SizeLabel = "466 MB", Notes = "English-only" },
new() { FileName = "ggml-medium.bin", DisplayName = "Medium", SizeLabel = "1.5 GB", Notes = "" },
new() { FileName = "ggml-large-v3.bin", DisplayName = "Large v3", SizeLabel = "3.1 GB", Notes = "Best accuracy" },
new() { FileName = "ggml-large-v3-turbo.bin", DisplayName = "Large v3 Turbo", SizeLabel = "1.5 GB", Notes = "⭐ Recommended" },
];
public string ModelsDirectory { get; set; }
public event Action<KnownModel>? ModelStatusChanged;
private readonly HttpClient _http = new() { Timeout = TimeSpan.FromHours(2) };
public ModelManager(string modelsDirectory)
{
ModelsDirectory = modelsDirectory;
Directory.CreateDirectory(modelsDirectory);
RefreshStatus(null);
}
public void RefreshStatus(string? loadedPath)
{
foreach (var m in Catalog)
{
m.LocalPath = Path.Combine(ModelsDirectory, m.FileName);
bool exists = File.Exists(m.LocalPath);
if (!exists)
// Also scan the known legacy scan paths
m.LocalPath = FindInLegacyPaths(m.FileName) ?? m.LocalPath;
m.Status = !File.Exists(m.LocalPath) ? ModelStatus.NotDownloaded
: m.LocalPath == loadedPath ? ModelStatus.Loaded
: ModelStatus.Downloaded;
}
}
private static string? FindInLegacyPaths(string fileName)
{
string[] dirs = [@"C:\Tools\whisper\models", @"C:\Program Files\Audacity\openvino-models"];
foreach (var d in dirs)
{
string p = Path.Combine(d, fileName);
if (File.Exists(p)) return p;
}
return null;
}
public async Task DownloadAsync(KnownModel model, CancellationToken ct, Action<double>? onProgress = null)
{
Directory.CreateDirectory(ModelsDirectory);
model.LocalPath = Path.Combine(ModelsDirectory, model.FileName);
model.Status = ModelStatus.Downloading;
model.Progress = 0;
ModelStatusChanged?.Invoke(model);
string url = BaseUrl + model.FileName;
string tmp = model.LocalPath + ".tmp";
try
{
using var response = await _http.GetAsync(url, HttpCompletionOption.ResponseHeadersRead, ct)
.ConfigureAwait(false);
response.EnsureSuccessStatusCode();
long total = response.Content.Headers.ContentLength ?? -1;
await using var src = await response.Content.ReadAsStreamAsync(ct).ConfigureAwait(false);
await using var dest = File.Create(tmp);
var buf = new byte[81920];
long downloaded = 0;
double lastReported = -1;
int read;
while ((read = await src.ReadAsync(buf, ct).ConfigureAwait(false)) > 0)
{
await dest.WriteAsync(buf.AsMemory(0, read), ct).ConfigureAwait(false);
downloaded += read;
if (total > 0)
{
model.Progress = downloaded * 100.0 / total;
// Only fire the event when progress moves by ≥1% to avoid flooding the UI.
if (model.Progress - lastReported >= 1.0)
{
lastReported = model.Progress;
onProgress?.Invoke(model.Progress);
ModelStatusChanged?.Invoke(model);
}
}
}
dest.Close();
File.Move(tmp, model.LocalPath, overwrite: true);
model.Status = ModelStatus.Downloaded;
model.Progress = 100;
ModelStatusChanged?.Invoke(model);
}
catch
{
if (File.Exists(tmp)) File.Delete(tmp);
model.Status = ModelStatus.NotDownloaded;
model.Progress = 0;
ModelStatusChanged?.Invoke(model);
throw;
}
}
public void Delete(KnownModel model)
{
if (File.Exists(model.LocalPath))
File.Delete(model.LocalPath);
model.Status = ModelStatus.NotDownloaded;
ModelStatusChanged?.Invoke(model);
}
}
}