@@ -17,16 +17,26 @@ import { Transform } from 'stream';
1717import * as path from 'path' ;
1818import { BrowserWindow } from 'electron' ;
1919import { logger } from '../../logger.js' ;
20- import { getModelsDir , getModelDir , isModelReady , ASR_MODELS } from './config.js' ;
20+ import {
21+ getModelsDir ,
22+ getModelDir ,
23+ getRescoreModelDir ,
24+ isModelReady ,
25+ isRescoreModelReady ,
26+ ASR_MODELS ,
27+ } from './config.js' ;
2128import { resetAvailabilityCache } from './SherpaAsrService.js' ;
29+ import { resetRescoreCache } from './sensevoiceRescore.js' ;
2230
2331/** 所有出站 HTTP 请求统一携带 UA,避免 CDN 返回 403 */
2432const HTTP_HEADERS = { 'User-Agent' : 'EntropyDecrease-Desktop/1.0 (Electron)' } ;
2533
26- /** 已废弃的旧模型目录名(Paraformer、SenseVoice),不再被使用,应清理 */
34+ /**
35+ * 已废弃的旧模型目录名(仅 Paraformer 双语模型——SenseVoice 已于
36+ * P1-1 重新启用为重打分模型,不再清理;误清会导致重打分不可用)
37+ */
2738const OBSOLETE_MODEL_DIRS = [
2839 'sherpa-onnx-streaming-paraformer-bilingual-zh-en' ,
29- 'sherpa-onnx-sense-voice-zh-en-ja-ko-yue-int8-2024-07-17' ,
3040] ;
3141
3242// ================================================================
@@ -47,14 +57,25 @@ export function getDownloadStatus(): { downloading: string | null; progress: num
4757
4858/** 获取模型状态列表 */
4959export function getModelsStatus ( ) : Array < { engine : string ; id : string ; label : string ; description : string ; size : string ; ready : boolean } > {
50- return [ {
51- engine : 'streaming' ,
52- id : ASR_MODELS . streaming . id ,
53- label : ASR_MODELS . streaming . label ,
54- description : ASR_MODELS . streaming . description ,
55- size : ASR_MODELS . streaming . size ,
56- ready : isModelReady ( ) ,
57- } ] ;
60+ return [
61+ {
62+ engine : 'streaming' ,
63+ id : ASR_MODELS . streaming . id ,
64+ label : ASR_MODELS . streaming . label ,
65+ description : ASR_MODELS . streaming . description ,
66+ size : ASR_MODELS . streaming . size ,
67+ ready : isModelReady ( ) ,
68+ } ,
69+ // P1-1:SenseVoice 重打分模型(可选增强,未下载不影响流式转写)
70+ {
71+ engine : 'rescore' ,
72+ id : ASR_MODELS . rescore . id ,
73+ label : ASR_MODELS . rescore . label ,
74+ description : ASR_MODELS . rescore . description ,
75+ size : ASR_MODELS . rescore . size ,
76+ ready : isRescoreModelReady ( ) ,
77+ } ,
78+ ] ;
5879}
5980
6081/**
@@ -96,14 +117,15 @@ export async function downloadModel(engine: string): Promise<string> {
96117 throw new Error ( `已有模型正在下载中(${ _downloading } ),请等待完成` ) ;
97118 }
98119
99- // 当前仅支持 streaming 引擎,参数验证确保前端误传时能给出明确错误
100- if ( engine !== 'streaming' ) {
101- throw new Error ( `不支持的引擎: ${ engine } (当前仅支持 streaming)` ) ;
120+ // P1-1:支持 streaming(流式主引擎)与 rescore(SenseVoice 重打分)
121+ if ( engine !== 'streaming' && engine !== 'rescore' ) {
122+ throw new Error ( `不支持的引擎: ${ engine } (支持 streaming / rescore )` ) ;
102123 }
103124
104- const modelDef = ASR_MODELS . streaming ;
125+ const modelDef = ASR_MODELS [ engine ] ;
105126 const modelsDir = getModelsDir ( ) ;
106- const targetDir = getModelDir ( ) ;
127+ const targetDir = engine === 'streaming' ? getModelDir ( ) : getRescoreModelDir ( ) ;
128+ const modelReady = engine === 'streaming' ? isModelReady : isRescoreModelReady ;
107129
108130 await mkdir ( modelsDir , { recursive : true } ) ;
109131
@@ -132,15 +154,15 @@ export async function downloadModel(engine: string): Promise<string> {
132154
133155 // ── 策略 2 + 3:逐文件下载(hf-mirror → huggingface 直连) ──
134156 if ( ghError ) {
135- if ( ! isModelReady ( ) ) {
157+ if ( ! modelReady ( ) ) {
136158 const bases = [
137159 modelDef . mirrorBaseUrl , // hf-mirror.com
138160 modelDef . mirrorBaseUrl . replace ( 'hf-mirror.com' , 'huggingface.co' ) , // 直连兜底
139161 ] ;
140162 let lastErr : unknown = ghError ;
141163 for ( const base of bases ) {
142164 try {
143- await downloadFromMirror ( engine , base , targetDir ) ;
165+ await downloadFromMirror ( engine , base , targetDir , modelDef ) ;
144166 lastErr = null ;
145167 break ;
146168 } catch ( mirrorErr ) {
@@ -154,11 +176,12 @@ export async function downloadModel(engine: string): Promise<string> {
154176
155177 // 校验关键文件
156178 broadcastProgress ( engine , 95 ) ;
157- if ( ! isModelReady ( ) ) {
179+ if ( ! modelReady ( ) ) {
158180 throw new Error ( `模型下载后校验失败:缺少关键文件(${ modelDef . files . join ( ', ' ) } )` ) ;
159181 }
160182
161183 resetAvailabilityCache ( ) ;
184+ resetRescoreCache ( ) ;
162185 logger . info ( `[LocalASR] ${ engine } model ready at: ${ targetDir } ` ) ;
163186 broadcastProgress ( engine , 100 ) ;
164187 // 新模型下载完成后清理旧模型
@@ -173,11 +196,22 @@ export async function downloadModel(engine: string): Promise<string> {
173196 }
174197}
175198
199+ /** 模型定义最小结构(streaming / rescore 通用) */
200+ interface ModelDef {
201+ files : readonly string [ ] ;
202+ downloadUrl : string ;
203+ mirrorBaseUrl : string ;
204+ }
205+
176206/**
177207 * 从指定 baseUrl 逐文件下载模型(无需解压)
178208 */
179- async function downloadFromMirror ( engine : string , baseUrl : string , targetDir : string ) : Promise < void > {
180- const modelDef = ASR_MODELS . streaming ;
209+ async function downloadFromMirror (
210+ engine : string ,
211+ baseUrl : string ,
212+ targetDir : string ,
213+ modelDef : ModelDef ,
214+ ) : Promise < void > {
181215 await mkdir ( targetDir , { recursive : true } ) ;
182216
183217 logger . info ( `[LocalASR] Downloading ${ modelDef . files . length } files from: ${ baseUrl } ` ) ;
@@ -197,14 +231,15 @@ async function downloadFromMirror(engine: string, baseUrl: string, targetDir: st
197231}
198232
199233/**
200- * 删除模型
234+ * 删除模型(P1-1:支持 streaming / rescore 双引擎)
201235 */
202- export async function deleteModel ( _engine : string ) : Promise < void > {
203- const modelDir = getModelDir ( ) ;
236+ export async function deleteModel ( engine : string ) : Promise < void > {
237+ const modelDir = engine === 'rescore' ? getRescoreModelDir ( ) : getModelDir ( ) ;
204238 if ( existsSync ( modelDir ) ) {
205239 rmSync ( modelDir , { recursive : true , force : true } ) ;
206240 resetAvailabilityCache ( ) ;
207- logger . info ( `[LocalASR] streaming model deleted: ${ modelDir } ` ) ;
241+ resetRescoreCache ( ) ;
242+ logger . info ( `[LocalASR] ${ engine } model deleted: ${ modelDir } ` ) ;
208243 }
209244}
210245
0 commit comments