-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunAsrRuntimeManager.cs
More file actions
1082 lines (949 loc) · 49 KB
/
FunAsrRuntimeManager.cs
File metadata and controls
1082 lines (949 loc) · 49 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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace TimeTask
{
internal sealed class PythonVersionInfo
{
public string Executable { get; set; }
public int Major { get; set; }
public int Minor { get; set; }
public override string ToString()
{
return $"{Major}.{Minor}";
}
}
internal sealed class FunAsrRuntimeBootstrapResult
{
public bool IsReady { get; private set; }
public string PythonExe { get; private set; }
public string Message { get; private set; }
public static FunAsrRuntimeBootstrapResult Ready(string pythonExe, string message)
{
return new FunAsrRuntimeBootstrapResult
{
IsReady = true,
PythonExe = pythonExe ?? string.Empty,
Message = message ?? "ok"
};
}
public static FunAsrRuntimeBootstrapResult NotReady(string pythonExe, string message)
{
return new FunAsrRuntimeBootstrapResult
{
IsReady = false,
PythonExe = pythonExe ?? string.Empty,
Message = message ?? "not-ready"
};
}
}
internal static class FunAsrRuntimeManager
{
private static readonly object Sync = new object();
private static Task<FunAsrRuntimeBootstrapResult> _bootstrapTask;
private static readonly object LogSync = new object();
private static DateTime _lastPipProgressLogUtc = DateTime.MinValue;
public static Task<FunAsrRuntimeBootstrapResult> EnsureReadyAsync(CancellationToken cancellationToken = default)
{
lock (Sync)
{
if (_bootstrapTask == null)
{
_bootstrapTask = EnsureReadyInternalAsync(cancellationToken);
}
return _bootstrapTask;
}
}
public static Task<FunAsrRuntimeBootstrapResult> ForceRebootstrapAsync(CancellationToken cancellationToken = default)
{
lock (Sync)
{
_bootstrapTask = EnsureReadyInternalAsync(cancellationToken);
return _bootstrapTask;
}
}
public static void KickoffIfNeeded()
{
try
{
bool autoBootstrap = ReadBool("FunAsrAutoBootstrap", true);
string provider = ReadString("VoiceAsrProvider", "hybrid");
VoiceRuntimeLog.Info($"FunASR kickoff check: autoBootstrap={autoBootstrap}, provider={provider}");
if (!autoBootstrap)
{
VoiceRuntimeLog.Info("FunASR kickoff skipped: auto bootstrap disabled.");
return;
}
if (provider.IndexOf("funasr", StringComparison.OrdinalIgnoreCase) < 0)
{
VoiceRuntimeLog.Info("FunASR kickoff skipped: provider does not include funasr.");
return;
}
VoiceListenerStatusCenter.Publish(VoiceListenerState.Loading, "正在准备 FunASR 运行环境");
VoiceRuntimeLog.Info("FunASR kickoff started.");
_ = EnsureReadyAsync();
}
catch (Exception ex)
{
VoiceRuntimeLog.Error("FunASR runtime kickoff failed.", ex);
}
}
private static async Task<FunAsrRuntimeBootstrapResult> EnsureReadyInternalAsync(CancellationToken cancellationToken)
{
try
{
if (!ReadBool("FunAsrAutoBootstrap", true))
{
string fallback = ReadString("FunAsrPythonExe", "python");
VoiceListenerStatusCenter.Publish(VoiceListenerState.Unavailable, "FunASR 自动准备已关闭");
return FunAsrRuntimeBootstrapResult.NotReady(fallback, "auto-bootstrap-disabled");
}
string configuredPython = ReadString("FunAsrPythonExe", "python");
string appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string runtimeRoot = Path.Combine(appData, "TimeTask", "funasr-runtime");
bool preferPrebuiltRuntime = ReadBool("FunAsrPreferPrebuiltRuntime", true);
bool allowOnlineInstallFallback = ReadBool("FunAsrAllowOnlineInstallFallback", false);
string bundlePath = ReadString("FunAsrRuntimeBundlePath", @"data\funasr-runtime-bundle.zip");
Directory.CreateDirectory(runtimeRoot);
VoiceRuntimeLog.Info($"FunASR runtime strategy: preferPrebuiltRuntime={preferPrebuiltRuntime}, allowOnlineInstallFallback={allowOnlineInstallFallback}, bundlePath={bundlePath}");
if (preferPrebuiltRuntime)
{
PublishProgress(1, 7, "检查预置语音运行包", VoiceListenerState.Loading);
var prebuilt = TryUsePrebuiltRuntime(runtimeRoot, bundlePath);
if (prebuilt.IsReady)
{
VoiceRuntimeLog.Info($"FunASR bootstrap: using prebuilt runtime. python={prebuilt.PythonExe}, source={prebuilt.Message}");
PublishProgress(6, 7, "预置语音环境就绪,初始化监听", VoiceListenerState.Loading);
PublishProgress(7, 7, "语音监听可用", VoiceListenerState.Ready);
return prebuilt;
}
if (!allowOnlineInstallFallback)
{
string msg = $"未找到可用预置语音包({prebuilt.Message})";
VoiceRuntimeLog.Info($"FunASR bootstrap stop: {msg}");
VoiceListenerStatusCenter.Publish(VoiceListenerState.Unavailable, msg);
return FunAsrRuntimeBootstrapResult.NotReady(configuredPython, $"prebuilt-runtime-not-ready:{prebuilt.Message}");
}
VoiceRuntimeLog.Info($"FunASR bootstrap: prebuilt runtime unavailable ({prebuilt.Message}), fallback to online bootstrap.");
}
PublishProgress(1, 7, "检测 Python 运行环境", VoiceListenerState.Loading);
string pythonExe = await ResolvePythonExecutableAsync(configuredPython, cancellationToken).ConfigureAwait(false);
if (string.IsNullOrWhiteSpace(pythonExe))
{
VoiceListenerStatusCenter.Publish(VoiceListenerState.Unavailable, "未检测到可用 Python,语音监听暂不可用");
return FunAsrRuntimeBootstrapResult.NotReady(configuredPython, "python-not-found");
}
int maxSupportedMinor = ReadInt("FunAsrMaxPythonMinor", 12);
PublishProgress(2, 7, "检查 Python 版本", VoiceListenerState.Loading);
var basePythonVersion = await GetPythonVersionAsync(pythonExe, cancellationToken).ConfigureAwait(false);
if (!IsSupportedPythonVersion(basePythonVersion, maxSupportedMinor))
{
string appDataForProvision = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string runtimeRootForProvision = Path.Combine(appDataForProvision, "TimeTask", "funasr-runtime");
Directory.CreateDirectory(runtimeRootForProvision);
var provisioned = await TryProvisionCompatiblePythonAsync(runtimeRootForProvision, cancellationToken).ConfigureAwait(false);
if (!string.IsNullOrWhiteSpace(provisioned))
{
var provisionedVersion = await GetPythonVersionAsync(provisioned, cancellationToken).ConfigureAwait(false);
if (IsSupportedPythonVersion(provisionedVersion, maxSupportedMinor))
{
pythonExe = provisioned;
basePythonVersion = provisionedVersion;
VoiceRuntimeLog.Info($"FunASR bootstrap: switched to provisioned Python {basePythonVersion} ({pythonExe})");
}
}
}
if (!IsSupportedPythonVersion(basePythonVersion, maxSupportedMinor))
{
string versionText = basePythonVersion == null ? "unknown" : basePythonVersion.ToString();
string msg = $"FunASR 暂不支持当前 Python {versionText},请安装 Python 3.10/3.11/3.12。";
VoiceRuntimeLog.Info(msg);
VoiceListenerStatusCenter.Publish(VoiceListenerState.Unavailable, msg);
return FunAsrRuntimeBootstrapResult.NotReady(pythonExe, $"python-version-unsupported:{versionText}");
}
string venvPath = Path.Combine(runtimeRoot, "venv");
string venvPython = Path.Combine(venvPath, "Scripts", "python.exe");
string markerPath = Path.Combine(runtimeRoot, "install.ok");
string healthPath = Path.Combine(runtimeRoot, "health.json");
string packageSpec = ReadString("FunAsrPipPackages", "funasr modelscope torch torchaudio");
string baseDependencySpec = ReadString("FunAsrBaseDependencies", "modelscope torch torchaudio");
string failCachePath = Path.Combine(runtimeRoot, "install.fail");
bool useNoDepsInstallStrategy = ReadBool("FunAsrInstallUseNoDepsStrategy", true);
VoiceRuntimeLog.Info($"FunASR bootstrap config: runtimeRoot={runtimeRoot}, useNoDepsInstallStrategy={useNoDepsInstallStrategy}, packageSpec={packageSpec}, baseDeps={baseDependencySpec}");
if (!File.Exists(venvPython))
{
PublishProgress(3, 7, "创建语音运行环境", VoiceListenerState.Installing);
VoiceRuntimeLog.Info($"FunASR bootstrap: creating venv at {venvPath}");
var createVenv = await RunProcessAsync(
pythonExe,
$"-m venv \"{venvPath}\"",
Math.Max(30, ReadInt("FunAsrBootstrapTimeoutSeconds", 900)),
cancellationToken).ConfigureAwait(false);
if (createVenv.ExitCode != 0 || !File.Exists(venvPython))
{
VoiceListenerStatusCenter.Publish(VoiceListenerState.Unavailable, "语音环境创建失败");
return FunAsrRuntimeBootstrapResult.NotReady(configuredPython, $"venv-create-failed: {createVenv.Stderr}");
}
}
string markerKey = $"packages={packageSpec}";
bool needsInstall = true;
if (File.Exists(markerPath))
{
string existing = File.ReadAllText(markerPath).Trim();
needsInstall = !string.Equals(existing, markerKey, StringComparison.Ordinal);
}
if (needsInstall)
{
int timeoutSeconds = Math.Max(120, ReadInt("FunAsrBootstrapTimeoutSeconds", 900));
if (IsInstallFailureCoolingDown(
failCachePath,
markerKey,
venvPython,
ReadInt("FunAsrInstallRetryCooldownMinutes", 30),
out string cooldownReason,
out int remainingSeconds))
{
if (useNoDepsInstallStrategy && IsEditdistanceBuildFailure(cooldownReason, cooldownReason))
{
VoiceRuntimeLog.Info("FunASR bootstrap: cooldown bypassed (no-deps strategy + editdistance historical failure).");
ClearInstallFailureCache(failCachePath);
}
else
{
VoiceRuntimeLog.Info($"FunASR bootstrap: install skipped by cooldown. reason={cooldownReason}");
VoiceListenerStatusCenter.Publish(VoiceListenerState.Unavailable, $"语音环境冷却中,{remainingSeconds}s 后自动重试");
return FunAsrRuntimeBootstrapResult.NotReady(configuredPython, $"install-cooldown:retry-after-sec={remainingSeconds};detail={cooldownReason}");
}
}
PublishProgress(4, 7, "安装语音识别依赖", VoiceListenerState.Installing);
VoiceRuntimeLog.Info($"FunASR bootstrap: installing pip packages ({packageSpec})");
var pipUpgrade = await RunProcessAsync(
venvPython,
"-m pip install --disable-pip-version-check --no-input --upgrade pip setuptools wheel",
timeoutSeconds,
cancellationToken).ConfigureAwait(false);
if (pipUpgrade.ExitCode != 0)
{
VoiceRuntimeLog.Info($"FunASR bootstrap: pip upgrade failed but continue. detail={BuildProcessFailureSummary(pipUpgrade)}");
}
(int ExitCode, string Stdout, string Stderr) pipInstall;
if (useNoDepsInstallStrategy)
{
bool noDepsOk = await TryInstallFunAsrNoDepsFallbackAsync(
venvPython,
baseDependencySpec,
timeoutSeconds,
cancellationToken).ConfigureAwait(false);
if (noDepsOk)
{
File.WriteAllText(markerPath, markerKey);
SaveHealthCache(healthPath, markerKey, venvPython, true);
ClearInstallFailureCache(failCachePath);
PublishProgress(6, 7, "语音环境就绪,初始化监听", VoiceListenerState.Loading);
PublishProgress(7, 7, "语音监听可用", VoiceListenerState.Ready);
return FunAsrRuntimeBootstrapResult.Ready(venvPython, "ok");
}
pipInstall = await RunProcessAsync(
venvPython,
$"-m pip install --disable-pip-version-check --no-input --prefer-binary {packageSpec}",
timeoutSeconds,
cancellationToken).ConfigureAwait(false);
}
else
{
pipInstall = await RunProcessAsync(
venvPython,
$"-m pip install --disable-pip-version-check --no-input --prefer-binary {packageSpec}",
timeoutSeconds,
cancellationToken).ConfigureAwait(false);
}
if (pipInstall.ExitCode != 0)
{
if (IsEditdistanceBuildFailure(pipInstall.Stderr, pipInstall.Stdout))
{
VoiceRuntimeLog.Info("FunASR bootstrap: detected editdistance wheel build failure, trying compatibility fallback.");
bool fallbackOk = await TryInstallEditdistanceCompatibilityAsync(venvPython, timeoutSeconds, cancellationToken).ConfigureAwait(false);
if (fallbackOk)
{
var retryInstall = await RunProcessAsync(
venvPython,
$"-m pip install --disable-pip-version-check --no-input --prefer-binary {packageSpec}",
timeoutSeconds,
cancellationToken).ConfigureAwait(false);
if (retryInstall.ExitCode == 0)
{
File.WriteAllText(markerPath, markerKey);
SaveHealthCache(healthPath, markerKey, venvPython, true);
ClearInstallFailureCache(failCachePath);
PublishProgress(6, 7, "语音环境就绪,初始化监听", VoiceListenerState.Loading);
PublishProgress(7, 7, "语音监听可用", VoiceListenerState.Ready);
return FunAsrRuntimeBootstrapResult.Ready(venvPython, "ok");
}
pipInstall = retryInstall;
}
if (pipInstall.ExitCode != 0)
{
VoiceRuntimeLog.Info("FunASR bootstrap: trying no-deps fallback install for funasr.");
bool noDepsOk = await TryInstallFunAsrNoDepsFallbackAsync(
venvPython,
baseDependencySpec,
timeoutSeconds,
cancellationToken).ConfigureAwait(false);
if (noDepsOk)
{
File.WriteAllText(markerPath, markerKey);
SaveHealthCache(healthPath, markerKey, venvPython, true);
ClearInstallFailureCache(failCachePath);
PublishProgress(6, 7, "语音环境就绪,初始化监听", VoiceListenerState.Loading);
PublishProgress(7, 7, "语音监听可用", VoiceListenerState.Ready);
return FunAsrRuntimeBootstrapResult.Ready(venvPython, "ok");
}
}
}
SaveInstallFailureCache(failCachePath, markerKey, venvPython, BuildProcessFailureSummary(pipInstall));
VoiceListenerStatusCenter.Publish(VoiceListenerState.Unavailable, "语音依赖安装失败");
return FunAsrRuntimeBootstrapResult.NotReady(configuredPython, $"pip-install-failed: {BuildProcessFailureSummary(pipInstall)}");
}
File.WriteAllText(markerPath, markerKey);
SaveHealthCache(healthPath, markerKey, venvPython, true);
ClearInstallFailureCache(failCachePath);
}
else
{
bool skipHeavyHealthCheck = IsHealthCacheFresh(healthPath, markerKey, venvPython, ReadInt("FunAsrHealthCacheHours", 72));
bool healthy = skipHeavyHealthCheck;
if (!skipHeavyHealthCheck)
{
PublishProgress(5, 7, "校验语音依赖完整性", VoiceListenerState.Loading);
healthy = await VerifyFunAsrDependenciesAsync(
venvPython,
Math.Max(30, ReadInt("FunAsrBootstrapTimeoutSeconds", 900)),
cancellationToken).ConfigureAwait(false);
}
if (!healthy)
{
int timeoutSeconds = Math.Max(120, ReadInt("FunAsrBootstrapTimeoutSeconds", 900));
VoiceListenerStatusCenter.Publish(VoiceListenerState.Installing, "检测到语音依赖异常,正在自动修复");
VoiceRuntimeLog.Info($"FunASR bootstrap: dependency health check failed, reinstalling packages ({packageSpec})");
var pipInstallRepair = await RunProcessAsync(
venvPython,
$"-m pip install --disable-pip-version-check --no-input --prefer-binary --upgrade {packageSpec}",
timeoutSeconds,
cancellationToken).ConfigureAwait(false);
if (pipInstallRepair.ExitCode != 0)
{
VoiceListenerStatusCenter.Publish(VoiceListenerState.Unavailable, "语音依赖修复失败");
return FunAsrRuntimeBootstrapResult.NotReady(configuredPython, $"pip-repair-failed: {BuildProcessFailureSummary(pipInstallRepair)}");
}
bool healthyAfterRepair = await VerifyFunAsrDependenciesAsync(
venvPython,
Math.Max(30, timeoutSeconds),
cancellationToken).ConfigureAwait(false);
if (!healthyAfterRepair)
{
SaveInstallFailureCache(failCachePath, markerKey, venvPython, "dependency-health-check-failed-after-repair");
VoiceListenerStatusCenter.Publish(VoiceListenerState.Unavailable, "语音依赖校验失败");
return FunAsrRuntimeBootstrapResult.NotReady(configuredPython, "dependency-health-check-failed");
}
File.WriteAllText(markerPath, markerKey);
SaveHealthCache(healthPath, markerKey, venvPython, true);
}
else
{
SaveHealthCache(healthPath, markerKey, venvPython, true);
}
}
PublishProgress(6, 7, "语音环境就绪,初始化监听", VoiceListenerState.Loading);
PublishProgress(7, 7, "语音监听可用", VoiceListenerState.Ready);
return FunAsrRuntimeBootstrapResult.Ready(venvPython, "ok");
}
catch (Exception ex)
{
VoiceRuntimeLog.Error("FunASR runtime bootstrap failed.", ex);
string fallback = ReadString("FunAsrPythonExe", "python");
VoiceListenerStatusCenter.Publish(VoiceListenerState.Unavailable, "语音环境准备失败");
return FunAsrRuntimeBootstrapResult.NotReady(fallback, ex.Message);
}
}
private static async Task<string> ResolvePythonExecutableAsync(string configured, CancellationToken cancellationToken)
{
string[] preferred = new[] { configured, "python", "py" }
.Where(x => !string.IsNullOrWhiteSpace(x))
.Distinct(StringComparer.OrdinalIgnoreCase)
.ToArray();
foreach (string command in preferred)
{
string resolved = await TryResolvePythonFromCommandAsync(command, cancellationToken).ConfigureAwait(false);
if (!string.IsNullOrWhiteSpace(resolved) && File.Exists(resolved))
{
return resolved;
}
}
return string.Empty;
}
private static async Task<string> TryResolvePythonFromCommandAsync(string command, CancellationToken cancellationToken)
{
if (string.IsNullOrWhiteSpace(command))
return string.Empty;
if (Path.IsPathRooted(command) && File.Exists(command))
return command;
string args = command.Equals("py", StringComparison.OrdinalIgnoreCase)
? "-3 -c \"import sys;print(sys.executable)\""
: "-c \"import sys;print(sys.executable)\"";
var result = await RunProcessAsync(command, args, 20, cancellationToken).ConfigureAwait(false);
if (result.ExitCode != 0)
return string.Empty;
string line = (result.Stdout ?? string.Empty)
.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
.Select(x => x.Trim())
.LastOrDefault();
return line ?? string.Empty;
}
private static async Task<PythonVersionInfo> GetPythonVersionAsync(string pythonExe, CancellationToken cancellationToken)
{
if (string.IsNullOrWhiteSpace(pythonExe))
return null;
var result = await RunProcessAsync(
pythonExe,
"-c \"import sys;print(f'{sys.executable}|{sys.version_info[0]}|{sys.version_info[1]}')\"",
20,
cancellationToken).ConfigureAwait(false);
if (result.ExitCode != 0)
return null;
string line = (result.Stdout ?? string.Empty)
.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
.Select(x => x.Trim())
.LastOrDefault();
if (string.IsNullOrWhiteSpace(line))
return null;
string[] parts = line.Split('|');
if (parts.Length < 3)
return null;
if (!int.TryParse(parts[1], out int major))
return null;
if (!int.TryParse(parts[2], out int minor))
return null;
return new PythonVersionInfo
{
Executable = parts[0],
Major = major,
Minor = minor
};
}
private static async Task<string> TryProvisionCompatiblePythonAsync(string runtimeRoot, CancellationToken cancellationToken)
{
if (!ReadBool("FunAsrAutoProvisionCondaPython", true))
{
return string.Empty;
}
string condaExe = ResolveCondaExecutable();
if (string.IsNullOrWhiteSpace(condaExe) || !File.Exists(condaExe))
{
VoiceRuntimeLog.Info("FunASR bootstrap: conda not found, skip compatible-python provisioning.");
return string.Empty;
}
string pyVersion = ReadString("FunAsrCondaPythonVersion", "3.11");
string envPath = Path.Combine(runtimeRoot, "conda-py311");
string envPython = Path.Combine(envPath, "python.exe");
if (File.Exists(envPython))
{
return envPython;
}
VoiceListenerStatusCenter.Publish(VoiceListenerState.Installing, $"正在准备 Python {pyVersion} 运行环境");
VoiceRuntimeLog.Info($"FunASR bootstrap: creating conda env at {envPath} (python={pyVersion})");
int timeoutSeconds = Math.Max(120, ReadInt("FunAsrBootstrapTimeoutSeconds", 900));
var createEnv = await RunProcessAsync(
condaExe,
$"create -y -p \"{envPath}\" python={pyVersion}",
timeoutSeconds,
cancellationToken).ConfigureAwait(false);
if (createEnv.ExitCode != 0 || !File.Exists(envPython))
{
VoiceRuntimeLog.Info($"FunASR bootstrap: conda env create failed. detail={BuildProcessFailureSummary(createEnv)}");
return string.Empty;
}
return envPython;
}
private static string ResolveCondaExecutable()
{
string configured = ReadString("FunAsrCondaExe", string.Empty);
if (!string.IsNullOrWhiteSpace(configured) && File.Exists(configured))
{
return configured;
}
string[] candidates = new[]
{
@"D:\tools\miniconda3\Scripts\conda.exe",
@"C:\ProgramData\miniconda3\Scripts\conda.exe",
@"C:\ProgramData\Anaconda3\Scripts\conda.exe"
};
string found = candidates.FirstOrDefault(File.Exists);
if (!string.IsNullOrWhiteSpace(found))
{
return found;
}
return string.Empty;
}
private static bool IsSupportedPythonVersion(PythonVersionInfo version, int maxSupportedMinor)
{
if (version == null)
return false;
if (version.Major != 3)
return false;
return version.Minor <= maxSupportedMinor;
}
private static async Task<bool> VerifyFunAsrDependenciesAsync(string pythonExe, int timeoutSeconds, CancellationToken cancellationToken)
{
if (string.IsNullOrWhiteSpace(pythonExe) || !File.Exists(pythonExe))
return false;
var probe = await RunProcessAsync(
pythonExe,
"-m pip show funasr modelscope torch torchaudio editdistance",
timeoutSeconds,
cancellationToken).ConfigureAwait(false);
if (probe.ExitCode != 0)
{
VoiceRuntimeLog.Info($"FunASR bootstrap: dependency probe failed. detail={BuildProcessFailureSummary(probe)}");
return false;
}
string output = (probe.Stdout ?? string.Empty).Trim();
return output.IndexOf("Name: funasr", StringComparison.OrdinalIgnoreCase) >= 0
&& output.IndexOf("Name: modelscope", StringComparison.OrdinalIgnoreCase) >= 0
&& output.IndexOf("Name: torch", StringComparison.OrdinalIgnoreCase) >= 0;
}
private static void PublishProgress(int step, int total, string detail, VoiceListenerState state)
{
int percent = Math.Max(0, Math.Min(100, (int)Math.Round(step * 100.0 / Math.Max(1, total))));
VoiceListenerStatusCenter.Publish(state, $"语音准备进度 {percent}%({step}/{total}):{detail}");
}
private static bool IsHealthCacheFresh(string healthPath, string markerKey, string pythonExe, int maxAgeHours)
{
try
{
if (!File.Exists(healthPath))
return false;
var map = ReadHealthMap(healthPath);
bool ok = string.Equals(GetHealthMapValue(map, "ok"), "true", StringComparison.OrdinalIgnoreCase);
string cachedMarker = GetHealthMapValue(map, "markerKey");
string cachedPython = GetHealthMapValue(map, "pythonExe");
string checkedUtcRaw = GetHealthMapValue(map, "checkedAtUtc");
if (!ok)
return false;
if (!string.Equals(cachedMarker, markerKey, StringComparison.Ordinal))
return false;
if (!string.Equals(cachedPython, pythonExe, StringComparison.OrdinalIgnoreCase))
return false;
if (!DateTime.TryParse(checkedUtcRaw, out DateTime checkedUtc))
return false;
return (DateTime.UtcNow - checkedUtc.ToUniversalTime()) < TimeSpan.FromHours(Math.Max(1, maxAgeHours));
}
catch
{
return false;
}
}
private static void SaveHealthCache(string healthPath, string markerKey, string pythonExe, bool ok)
{
try
{
var lines = new[]
{
$"ok={(ok ? "true" : "false")}",
$"markerKey={markerKey ?? string.Empty}",
$"pythonExe={pythonExe ?? string.Empty}",
$"checkedAtUtc={DateTime.UtcNow:o}"
};
File.WriteAllLines(healthPath, lines, Encoding.UTF8);
}
catch
{
}
}
private static string[] ReadHealthMap(string path)
{
try
{
return File.ReadAllLines(path, Encoding.UTF8);
}
catch
{
return Array.Empty<string>();
}
}
private static string GetHealthMapValue(string[] lines, string key)
{
if (lines == null || lines.Length == 0 || string.IsNullOrWhiteSpace(key))
return string.Empty;
string prefix = key + "=";
string line = lines.FirstOrDefault(x => x != null && x.StartsWith(prefix, StringComparison.OrdinalIgnoreCase));
if (string.IsNullOrWhiteSpace(line))
return string.Empty;
return line.Substring(prefix.Length).Trim();
}
private static bool IsEditdistanceBuildFailure(string stderr, string stdout)
{
string all = ((stderr ?? string.Empty) + "\n" + (stdout ?? string.Empty)).ToLowerInvariant();
return all.Contains("building wheel for editdistance")
|| all.Contains("no matching distribution found for editdistance")
|| all.Contains("failed building wheel for editdistance");
}
private static async Task<bool> TryInstallEditdistanceCompatibilityAsync(string venvPython, int timeoutSeconds, CancellationToken cancellationToken)
{
string[] candidates = new[]
{
"editdistance==0.6.2",
"editdistance==0.7.0",
"editdistance==0.7.1"
};
foreach (string candidate in candidates)
{
var result = await RunProcessAsync(
venvPython,
$"-m pip install --disable-pip-version-check --no-input --prefer-binary --only-binary=:all: {candidate}",
timeoutSeconds,
cancellationToken).ConfigureAwait(false);
if (result.ExitCode == 0)
{
VoiceRuntimeLog.Info($"FunASR bootstrap: compatible {candidate} installed.");
return true;
}
}
return false;
}
private static async Task<bool> TryInstallFunAsrNoDepsFallbackAsync(string venvPython, string baseDependencySpec, int timeoutSeconds, CancellationToken cancellationToken)
{
VoiceRuntimeLog.Info($"FunASR bootstrap: no-deps strategy start. baseDeps={baseDependencySpec}");
var depsInstall = await RunProcessAsync(
venvPython,
$"-m pip install --disable-pip-version-check --no-input --prefer-binary {baseDependencySpec}",
timeoutSeconds,
cancellationToken).ConfigureAwait(false);
if (depsInstall.ExitCode != 0)
{
VoiceRuntimeLog.Info($"FunASR bootstrap: no-deps fallback deps install failed. detail={BuildProcessFailureSummary(depsInstall)}");
return false;
}
var funasrNoDeps = await RunProcessAsync(
venvPython,
"-m pip install --disable-pip-version-check --no-input --prefer-binary --no-deps funasr",
timeoutSeconds,
cancellationToken).ConfigureAwait(false);
if (funasrNoDeps.ExitCode != 0)
{
VoiceRuntimeLog.Info($"FunASR bootstrap: no-deps funasr install failed. detail={BuildProcessFailureSummary(funasrNoDeps)}");
return false;
}
var probe = await RunProcessAsync(
venvPython,
"-c \"import funasr,modelscope,torch;print('ok')\"",
Math.Max(30, timeoutSeconds / 3),
cancellationToken).ConfigureAwait(false);
if (probe.ExitCode != 0)
{
VoiceRuntimeLog.Info($"FunASR bootstrap: no-deps fallback probe failed. detail={BuildProcessFailureSummary(probe)}");
return false;
}
VoiceRuntimeLog.Info("FunASR bootstrap: no-deps strategy completed successfully.");
return (probe.Stdout ?? string.Empty).IndexOf("ok", StringComparison.OrdinalIgnoreCase) >= 0;
}
private static bool IsInstallFailureCoolingDown(string failCachePath, string markerKey, string pythonExe, int cooldownMinutes, out string reason, out int remainingSeconds)
{
reason = string.Empty;
remainingSeconds = 0;
try
{
if (!File.Exists(failCachePath))
return false;
var lines = File.ReadAllLines(failCachePath, Encoding.UTF8);
string cachedMarker = GetHealthMapValue(lines, "markerKey");
string cachedPython = GetHealthMapValue(lines, "pythonExe");
string tsRaw = GetHealthMapValue(lines, "failedAtUtc");
string detail = GetHealthMapValue(lines, "detail");
if (!string.Equals(cachedMarker, markerKey, StringComparison.Ordinal))
return false;
if (!string.Equals(cachedPython, pythonExe, StringComparison.OrdinalIgnoreCase))
return false;
if (!DateTime.TryParse(tsRaw, out DateTime failedAtUtc))
return false;
TimeSpan age = DateTime.UtcNow - failedAtUtc.ToUniversalTime();
if (age >= TimeSpan.FromMinutes(Math.Max(1, cooldownMinutes)))
return false;
remainingSeconds = Math.Max(1, (int)Math.Ceiling(TimeSpan.FromMinutes(Math.Max(1, cooldownMinutes)).Subtract(age).TotalSeconds));
reason = string.IsNullOrWhiteSpace(detail) ? "上次安装失败,稍后重试" : detail;
return true;
}
catch
{
return false;
}
}
private static void SaveInstallFailureCache(string failCachePath, string markerKey, string pythonExe, string detail)
{
try
{
string dir = Path.GetDirectoryName(failCachePath);
if (!string.IsNullOrWhiteSpace(dir))
{
Directory.CreateDirectory(dir);
}
var lines = new[]
{
$"markerKey={markerKey ?? string.Empty}",
$"pythonExe={pythonExe ?? string.Empty}",
$"failedAtUtc={DateTime.UtcNow:o}",
$"detail={(detail ?? string.Empty).Replace('\r', ' ').Replace('\n', ' ')}"
};
File.WriteAllLines(failCachePath, lines, Encoding.UTF8);
VoiceRuntimeLog.Info($"FunASR bootstrap: failure cache written: {failCachePath}");
}
catch (Exception ex)
{
VoiceRuntimeLog.Error($"FunASR bootstrap: failure cache write failed: {failCachePath}", ex);
}
}
private static void ClearInstallFailureCache(string failCachePath)
{
try
{
if (File.Exists(failCachePath))
{
File.Delete(failCachePath);
VoiceRuntimeLog.Info($"FunASR bootstrap: failure cache cleared: {failCachePath}");
}
}
catch
{
}
}
private static string BuildProcessFailureSummary((int ExitCode, string Stdout, string Stderr) result)
{
string err = (result.Stderr ?? string.Empty).Trim();
string output = (result.Stdout ?? string.Empty).Trim();
if (output.Length > 180)
{
output = output.Substring(0, 180) + "...";
}
if (err.Length > 180)
{
err = err.Substring(0, 180) + "...";
}
return $"code={result.ExitCode}, stderr={err}, stdout={output}";
}
private static async Task<(int ExitCode, string Stdout, string Stderr)> RunProcessAsync(
string fileName,
string arguments,
int timeoutSeconds,
CancellationToken cancellationToken)
{
DateTime started = DateTime.UtcNow;
VoiceRuntimeLog.Info($"RunProcess start: file={fileName}, args={CompactArgs(arguments)}, timeoutSec={timeoutSeconds}");
var psi = new ProcessStartInfo
{
FileName = fileName,
Arguments = arguments,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
StandardOutputEncoding = Encoding.UTF8,
StandardErrorEncoding = Encoding.UTF8
};
using (var process = new Process { StartInfo = psi })
{
var stdoutBuilder = new StringBuilder();
var stderrBuilder = new StringBuilder();
process.OutputDataReceived += (s, e) =>
{
if (e.Data == null) return;
lock (stdoutBuilder) { stdoutBuilder.AppendLine(e.Data); }
MaybeLogPipProgress(fileName, arguments, e.Data);
};
process.ErrorDataReceived += (s, e) =>
{
if (e.Data == null) return;
lock (stderrBuilder) { stderrBuilder.AppendLine(e.Data); }
MaybeLogPipProgress(fileName, arguments, e.Data);
};
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
int timeoutMs = Math.Max(5, timeoutSeconds) * 1000;
bool exited = await Task.Run(() => process.WaitForExit(timeoutMs), cancellationToken).ConfigureAwait(false);
if (!exited)
{
try { process.Kill(); } catch { }
string timeoutStdout;
string timeoutStderr;
lock (stdoutBuilder) { timeoutStdout = stdoutBuilder.ToString(); }
lock (stderrBuilder) { timeoutStderr = stderrBuilder.ToString(); }
VoiceRuntimeLog.Info($"RunProcess timeout: file={fileName}, args={CompactArgs(arguments)}, elapsedSec={(DateTime.UtcNow - started).TotalSeconds:F1}");
return (-1, timeoutStdout, string.IsNullOrWhiteSpace(timeoutStderr) ? "timeout" : timeoutStderr);
}
process.WaitForExit();
string stdout;
string stderr;
lock (stdoutBuilder) { stdout = stdoutBuilder.ToString(); }
lock (stderrBuilder) { stderr = stderrBuilder.ToString(); }
VoiceRuntimeLog.Info($"RunProcess end: file={fileName}, code={process.ExitCode}, elapsedSec={(DateTime.UtcNow - started).TotalSeconds:F1}, args={CompactArgs(arguments)}");
return (process.ExitCode, stdout, stderr);
}
}
private static string CompactArgs(string args)
{
if (string.IsNullOrWhiteSpace(args))
return string.Empty;
string compact = args.Replace("\r", " ").Replace("\n", " ").Trim();
if (compact.Length > 220)
{
compact = compact.Substring(0, 220) + "...";
}
return compact;
}
private static void MaybeLogPipProgress(string fileName, string arguments, string line)
{
if (string.IsNullOrWhiteSpace(line))
return;
bool isPip = (arguments ?? string.Empty).IndexOf("-m pip", StringComparison.OrdinalIgnoreCase) >= 0;
if (!isPip)
return;
string lower = line.ToLowerInvariant();
bool interesting = lower.Contains("collecting ")
|| lower.Contains("downloading ")
|| lower.Contains("building wheel")
|| lower.Contains("installing collected packages")
|| lower.Contains("successfully installed")
|| lower.Contains("error:")
|| lower.Contains("failed");
if (!interesting)
return;
lock (LogSync)
{
DateTime now = DateTime.UtcNow;
if ((now - _lastPipProgressLogUtc) < TimeSpan.FromSeconds(2) && lower.Contains("downloading "))
return;
_lastPipProgressLogUtc = now;
}
VoiceRuntimeLog.Info($"pip-progress: {line.Trim()}");
}
private static FunAsrRuntimeBootstrapResult TryUsePrebuiltRuntime(string runtimeRoot, string configuredBundlePath)
{
try
{
string[] bundleCandidates = BuildBundleCandidates(configuredBundlePath, runtimeRoot);
string bundle = bundleCandidates.FirstOrDefault(File.Exists);
if (string.IsNullOrWhiteSpace(bundle))
{
return FunAsrRuntimeBootstrapResult.NotReady(string.Empty, "bundle-not-found");
}
string currentSignature = $"{bundle}|{new FileInfo(bundle).Length}|{File.GetLastWriteTimeUtc(bundle):o}";
string signatureFolder = ComputeStableFolderName(currentSignature);
string prebuiltRoot = Path.Combine(runtimeRoot, "prebuilt", signatureFolder);
if (!Directory.Exists(prebuiltRoot))
{
Directory.CreateDirectory(prebuiltRoot);
ZipFile.ExtractToDirectory(bundle, prebuiltRoot);
VoiceRuntimeLog.Info($"FunASR prebuilt runtime extracted: bundle={bundle}, target={prebuiltRoot}");
}
string pythonExe = FindPythonInPrebuilt(prebuiltRoot);
if (string.IsNullOrWhiteSpace(pythonExe) || !File.Exists(pythonExe))
{
return FunAsrRuntimeBootstrapResult.NotReady(string.Empty, "prebuilt-python-not-found");
}
return FunAsrRuntimeBootstrapResult.Ready(pythonExe, $"prebuilt:{bundle}");
}
catch (Exception ex)
{
VoiceRuntimeLog.Error("FunASR prebuilt runtime prepare failed.", ex);
return FunAsrRuntimeBootstrapResult.NotReady(string.Empty, "prebuilt-prepare-failed");
}
}
private static string ComputeStableFolderName(string signature)
{
try
{
using (var sha1 = System.Security.Cryptography.SHA1.Create())
{
byte[] hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(signature ?? string.Empty));
string hex = BitConverter.ToString(hash).Replace("-", string.Empty).ToLowerInvariant();
return hex.Substring(0, 16);