-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpeechModelManager.cs
More file actions
422 lines (366 loc) · 15.5 KB
/
SpeechModelManager.cs
File metadata and controls
422 lines (366 loc) · 15.5 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
using System;
using System.Configuration;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Collections.Generic;
using System.Net.Http;
using System.Security.Cryptography;
using System.Threading;
using System.Threading.Tasks;
namespace TimeTask
{
/// <summary>
/// 语音模型管理:
/// - 自动下载/解压模型压缩包
/// - 通过 SHA256 做可选完整性校验
/// - 启动时做轻量预热,减少首次访问延迟
/// </summary>
public sealed class SpeechModelManager
{
private static readonly HttpClient SharedClient = new HttpClient { Timeout = TimeSpan.FromMinutes(5) };
private readonly SemaphoreSlim _bootstrapLock = new SemaphoreSlim(1, 1);
private Task<SpeechModelBootstrapResult> _bootstrapTask;
public string ModelRootPath { get; }
public string ModelName { get; }
public bool AutoDownloadEnabled { get; }
public SpeechModelManager()
{
string appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
ModelRootPath = Path.Combine(appData, "TimeTask", "speech-models");
ModelName = ReadAppSetting("SpeechModelName", "vosk-model-cn-0.22");
AutoDownloadEnabled = ReadBoolAppSetting("SpeechModelAutoDownload", true);
}
public Task<SpeechModelBootstrapResult> EnsureReadyAsync(CancellationToken cancellationToken = default)
{
lock (this)
{
if (_bootstrapTask == null)
{
_bootstrapTask = EnsureReadyInternalAsync(cancellationToken);
}
return _bootstrapTask;
}
}
private async Task<SpeechModelBootstrapResult> EnsureReadyInternalAsync(CancellationToken cancellationToken)
{
await _bootstrapLock.WaitAsync(cancellationToken).ConfigureAwait(false);
try
{
VoiceRuntimeLog.Info($"Speech model bootstrap started. root={ModelRootPath}, model={ModelName}, autoDownload={AutoDownloadEnabled}");
Directory.CreateDirectory(ModelRootPath);
string modelDir = GetModelDirectory();
if (IsModelReady(modelDir))
{
VoiceRuntimeLog.Info($"Speech model already exists: {modelDir}");
await WarmupModelAsync(modelDir, cancellationToken).ConfigureAwait(false);
return SpeechModelBootstrapResult.Ready(modelDir, "local-cache");
}
if (!AutoDownloadEnabled)
{
VoiceRuntimeLog.Info("Speech model missing and auto download disabled.");
return SpeechModelBootstrapResult.NotReady("模型不存在,且自动下载已关闭。");
}
string modelUrl = ReadAppSetting("SpeechModelUrl", GetDefaultModelUrl(ModelName));
if (string.IsNullOrWhiteSpace(modelUrl))
{
VoiceRuntimeLog.Info("SpeechModelUrl is empty.");
return SpeechModelBootstrapResult.NotReady("未配置 SpeechModelUrl,无法自动下载模型。");
}
string expectedSha256 = ReadAppSetting("SpeechModelSha256", string.Empty);
VoiceRuntimeLog.Info($"Downloading speech model from {modelUrl}");
string zipPath = await DownloadModelZipAsync(modelUrl, cancellationToken).ConfigureAwait(false);
VoiceRuntimeLog.Info($"Speech model downloaded: {zipPath}");
if (!string.IsNullOrWhiteSpace(expectedSha256))
{
ValidateSha256(zipPath, expectedSha256);
VoiceRuntimeLog.Info("Speech model SHA256 validated.");
}
string extractedPath = ExtractModelZip(zipPath);
VoiceRuntimeLog.Info($"Speech model extracted: {extractedPath}");
EnsureDefaultPhrases(extractedPath);
await WarmupModelAsync(extractedPath, cancellationToken).ConfigureAwait(false);
VoiceRuntimeLog.Info("Speech model warmup completed.");
return SpeechModelBootstrapResult.Ready(extractedPath, "downloaded");
}
catch (OperationCanceledException)
{
VoiceRuntimeLog.Info("Speech model bootstrap canceled.");
throw;
}
catch (Exception ex)
{
VoiceRuntimeLog.Error("Speech model bootstrap failed.", ex);
return SpeechModelBootstrapResult.NotReady($"模型准备失败: {ex.Message}");
}
finally
{
_bootstrapLock.Release();
}
}
public string GetModelDirectory()
{
return Path.Combine(ModelRootPath, ModelName);
}
public string GetHintsFilePath()
{
return Path.Combine(GetModelDirectory(), "phrases.txt");
}
private async Task<string> DownloadModelZipAsync(string modelUrl, CancellationToken cancellationToken)
{
string fileName = $"{ModelName}.zip";
string zipPath = Path.Combine(ModelRootPath, fileName);
using (var response = await SharedClient.GetAsync(modelUrl, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false))
{
response.EnsureSuccessStatusCode();
long? contentLength = response.Content.Headers.ContentLength;
string tmpPath = zipPath + ".tmp";
using (var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
using (var fs = new FileStream(tmpPath, FileMode.Create, FileAccess.Write, FileShare.None))
{
var buffer = new byte[81920];
long totalRead = 0;
int lastPercent = -1;
while (true)
{
int read = await stream.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false);
if (read <= 0) break;
await fs.WriteAsync(buffer, 0, read, cancellationToken).ConfigureAwait(false);
totalRead += read;
if (contentLength.HasValue && contentLength.Value > 0)
{
int percent = (int)(totalRead * 100 / contentLength.Value);
if (percent >= lastPercent + 5)
{
lastPercent = percent;
VoiceRuntimeLog.Info($"Speech model download progress: {percent}% ({FormatBytes(totalRead)}/{FormatBytes(contentLength.Value)})");
}
}
}
}
if (File.Exists(zipPath))
{
File.Delete(zipPath);
}
File.Move(tmpPath, zipPath);
}
return zipPath;
}
private string ExtractModelZip(string zipPath)
{
string targetDir = GetModelDirectory();
string tempDir = $"{targetDir}.tmp_{Guid.NewGuid():N}";
if (Directory.Exists(tempDir))
{
Directory.Delete(tempDir, true);
}
Directory.CreateDirectory(tempDir);
ZipFile.ExtractToDirectory(zipPath, tempDir);
string extractedRoot = FindExtractedRoot(tempDir);
if (Directory.Exists(targetDir))
{
Directory.Delete(targetDir, true);
}
if (!string.Equals(extractedRoot, tempDir, StringComparison.OrdinalIgnoreCase))
{
Directory.Move(extractedRoot, targetDir);
Directory.Delete(tempDir, true);
}
else
{
Directory.Move(tempDir, targetDir);
}
return targetDir;
}
private static string FindExtractedRoot(string dir)
{
var directChildren = Directory.GetDirectories(dir);
if (directChildren.Length == 1 &&
Directory.GetFiles(dir).Length == 0 &&
Directory.GetDirectories(directChildren[0]).Length > 0)
{
return directChildren[0];
}
return dir;
}
private static void ValidateSha256(string filePath, string expectedSha256)
{
string normalized = expectedSha256.Trim().ToLowerInvariant();
using (var sha = SHA256.Create())
using (var stream = File.OpenRead(filePath))
{
var hash = sha.ComputeHash(stream);
string actual = BitConverter.ToString(hash).Replace("-", string.Empty).ToLowerInvariant();
if (!string.Equals(actual, normalized, StringComparison.Ordinal))
{
throw new InvalidDataException("模型文件 SHA256 校验失败。");
}
}
}
private static bool IsModelReady(string modelDir)
{
if (!Directory.Exists(modelDir))
{
return false;
}
// Vosk 模型最关键文件
string voskMarker = Path.Combine(modelDir, "am", "final.mdl");
if (File.Exists(voskMarker))
{
return true;
}
bool hasAnyFiles = Directory.EnumerateFiles(modelDir, "*", SearchOption.AllDirectories).Any();
return hasAnyFiles;
}
private void EnsureDefaultPhrases(string modelDir)
{
try
{
string hintsPath = GetHintsFilePath();
if (File.Exists(hintsPath))
{
MergeUserLexicon(hintsPath);
return;
}
Directory.CreateDirectory(Path.GetDirectoryName(hintsPath));
var lines = new[]
{
"请提醒我明天与市场部门的会议",
"提醒我明天的会议",
"明天与市场部门的会议",
"会议",
"市场部门",
"明天",
"提醒我"
};
File.WriteAllLines(hintsPath, lines);
VoiceRuntimeLog.Info($"Default phrases.txt created: {hintsPath}");
MergeUserLexicon(hintsPath);
}
catch (Exception ex)
{
VoiceRuntimeLog.Error("Failed to create default phrases.txt.", ex);
}
}
private void MergeUserLexicon(string hintsPath)
{
try
{
string appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string lexiconPath = Path.Combine(appData, "TimeTask", "voice_lexicon.txt");
if (!File.Exists(lexiconPath))
return;
var userPhrases = File.ReadAllLines(lexiconPath)
.Select(l => l?.Trim())
.Where(l => !string.IsNullOrWhiteSpace(l))
.ToList();
if (userPhrases.Count == 0)
return;
var existing = File.Exists(hintsPath)
? File.ReadAllLines(hintsPath).Select(l => l?.Trim()).Where(l => !string.IsNullOrWhiteSpace(l)).ToList()
: new List<string>();
var merged = existing
.Concat(userPhrases)
.Distinct(StringComparer.OrdinalIgnoreCase)
.Take(500)
.ToList();
File.WriteAllLines(hintsPath, merged);
VoiceRuntimeLog.Info($"Merged user lexicon into phrases.txt, count={merged.Count}");
}
catch (Exception ex)
{
VoiceRuntimeLog.Error("Failed to merge user lexicon into phrases.txt.", ex);
}
}
private static async Task WarmupModelAsync(string modelDir, CancellationToken cancellationToken)
{
if (!Directory.Exists(modelDir))
{
return;
}
var files = Directory.EnumerateFiles(modelDir, "*", SearchOption.AllDirectories)
.OrderBy(f => new FileInfo(f).Length)
.Take(16)
.ToArray();
var buffer = new byte[8192];
foreach (var file in files)
{
cancellationToken.ThrowIfCancellationRequested();
using (var fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read))
{
await fs.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false);
}
}
}
private static string ReadAppSetting(string key, string fallback)
{
try
{
var value = ConfigurationManager.AppSettings[key];
return string.IsNullOrWhiteSpace(value) ? fallback : value;
}
catch
{
return fallback;
}
}
private static bool ReadBoolAppSetting(string key, bool fallback)
{
var value = ReadAppSetting(key, fallback.ToString());
return bool.TryParse(value, out bool parsed) ? parsed : fallback;
}
private static string GetDefaultModelUrl(string modelName)
{
if (string.Equals(modelName, "vosk-model-small-cn-0.22", StringComparison.OrdinalIgnoreCase))
{
return "https://alphacephei.com/vosk/models/vosk-model-small-cn-0.22.zip";
}
if (string.Equals(modelName, "vosk-model-cn-0.22", StringComparison.OrdinalIgnoreCase))
{
return "https://alphacephei.com/vosk/models/vosk-model-cn-0.22.zip";
}
if (string.Equals(modelName, "vosk-model-small-en-us-0.15", StringComparison.OrdinalIgnoreCase))
{
return "https://alphacephei.com/vosk/models/vosk-model-small-en-us-0.15.zip";
}
return string.Empty;
}
private static string FormatBytes(long bytes)
{
if (bytes < 1024) return $"{bytes} B";
double kb = bytes / 1024.0;
if (kb < 1024) return $"{kb:F1} KB";
double mb = kb / 1024.0;
if (mb < 1024) return $"{mb:F1} MB";
double gb = mb / 1024.0;
return $"{gb:F2} GB";
}
}
public sealed class SpeechModelBootstrapResult
{
public bool IsReady { get; private set; }
public string ModelDirectory { get; private set; }
public string Source { get; private set; }
public string Message { get; private set; }
public static SpeechModelBootstrapResult Ready(string modelDirectory, string source)
{
return new SpeechModelBootstrapResult
{
IsReady = true,
ModelDirectory = modelDirectory,
Source = source,
Message = "OK"
};
}
public static SpeechModelBootstrapResult NotReady(string message)
{
return new SpeechModelBootstrapResult
{
IsReady = false,
ModelDirectory = string.Empty,
Source = "none",
Message = message ?? "Unknown"
};
}
}
}