dsh-podwave 0.11.26 → 0.11.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/segment.js ADDED
@@ -0,0 +1,205 @@
1
+ // 识别完之后的「再断句」。
2
+ //
3
+ // 语音识别出来的分段是按模型自己的声学块来的:本地引擎尤其碎,常常一行只有两三个词,
4
+ // 一句话被劈成好几行,行尾还没有标点。翻译是按行送进模型的,碎片行会带来两个问题:
5
+ // 1. 小模型看到一堆不成句的短行,会自作主张把几行并成一行,编号一乱译文就整体错位;
6
+ // 2. 一行半句话翻出来的东西读着别扭,播放器里也没法当字幕用。
7
+ //
8
+ // 所以识别完成后先做一遍整理:
9
+ // · 先把每行按句末标点拆成「句」;
10
+ // · 再把相邻的碎片按「上一句没说完 + 行间几乎没停顿」拼回整句;
11
+ // · 时间轴按字数比例在原来的起止时间之间重新分摊。
12
+ // 没有任何标点的音频靠停顿兜底:行间隔超过 maxPause 就当作句子边界。
13
+ const STRONG_END = /[。.!?!?…‼⁇]/
14
+ const CLOSER = /[」』))】》〉”’"'\]]/
15
+ const SEMI = /[;;]/
16
+ // 英文里的句号要当心小数点和缩写:「3.5」「Mr.」「U.S.」都不是句末。
17
+ const ABBREV = /(?:\b(?:mr|mrs|ms|dr|st|jr|sr|prof|vs|etc|approx|dept|est|fig|vol|no)|\b[a-z])\.$/i
18
+
19
+ const DEFAULTS = {
20
+ // 一句话最多拼多长(字符数)。宁可长一点也不要把句子劈成两半——劈开了翻译就更难。
21
+ maxChars: 168,
22
+ // 一句话最长占用多少秒。太长了播放器跟读会累,超过就断开。
23
+ maxDuration: 16,
24
+ // 两行之间超过这个间隔(秒)就当成句子边界,不再往一起拼。
25
+ maxPause: 0.6,
26
+ }
27
+
28
+ function round2(value) {
29
+ return Math.round((Number(value) || 0) * 100) / 100
30
+ }
31
+
32
+ /**
33
+ * 只按标点拆句,不做长度限制(长度限制在合并那一步做)。
34
+ * 句末标点后面的右引号/右括号跟在同一句里;连续的省略号当作一个整体。
35
+ */
36
+ export function splitSentences(text) {
37
+ const chars = Array.from(String(text || ''))
38
+ const parts = []
39
+ let buffer = ''
40
+ const flush = () => {
41
+ const piece = buffer.trim()
42
+ if (piece) parts.push(piece)
43
+ buffer = ''
44
+ }
45
+ for (let index = 0; index < chars.length; index += 1) {
46
+ const char = chars[index]
47
+ const next = chars[index + 1] || ''
48
+ const prev = chars[index - 1] || ''
49
+ buffer += char
50
+ if (CLOSER.test(char)) {
51
+ // 右引号/右括号跟着上一句一起收尾。
52
+ if (STRONG_END.test(prev) || prev === '.') flush()
53
+ continue
54
+ }
55
+ if (STRONG_END.test(char)) {
56
+ if (next === char) continue
57
+ if (CLOSER.test(next)) continue
58
+ flush()
59
+ continue
60
+ }
61
+ if (char === '.') {
62
+ // 只有「后面是空白或到结尾」的点才可能是句末;小数、网址里的点不收。
63
+ if (next && !/\s/.test(next)) continue
64
+ if (ABBREV.test(buffer.trimEnd())) continue
65
+ flush()
66
+ }
67
+ }
68
+ flush()
69
+ return parts
70
+ }
71
+
72
+ function sliceText(text, maxChars) {
73
+ const pieces = []
74
+ let rest = String(text || '')
75
+ while (rest.length > maxChars) {
76
+ const window = rest.slice(0, maxChars)
77
+ let cut = maxChars
78
+ const space = window.lastIndexOf(' ')
79
+ if (space >= maxChars * 0.6) {
80
+ cut = space + 1
81
+ } else {
82
+ const punct = Math.max(window.lastIndexOf(','), window.lastIndexOf('、'), window.lastIndexOf(','), window.lastIndexOf(';'))
83
+ if (punct >= maxChars * 0.5) cut = punct + 1
84
+ }
85
+ pieces.push(rest.slice(0, cut))
86
+ rest = rest.slice(cut)
87
+ }
88
+ if (rest) pieces.push(rest)
89
+ return pieces.map((piece) => piece.trim()).filter(Boolean)
90
+ }
91
+
92
+ // 一「句」如果长到超过时长/字数上限(比如整段没有标点),就在句子内部再切,时间按字数分摊。
93
+ function explodeUnit(unit, config) {
94
+ const text = String(unit.text || '').trim()
95
+ if (!text) return []
96
+ const span = Math.max(Number(unit.end) - Number(unit.start), 0.2)
97
+ if (text.length <= config.maxChars && span <= config.maxDuration) return [{ ...unit, text }]
98
+ const ratio = Math.min(1, config.maxDuration / span)
99
+ const budget = Math.max(12, Math.floor(text.length * ratio))
100
+ const pieces = sliceText(text, Math.min(config.maxChars, budget))
101
+ if (pieces.length <= 1) return [{ ...unit, text }]
102
+ const total = pieces.reduce((sum, piece) => sum + piece.length, 0) || 1
103
+ let cursor = Number(unit.start) || 0
104
+ return pieces.map((piece, index) => {
105
+ const start = round2(cursor)
106
+ cursor += (piece.length / total) * span
107
+ const isLast = index === pieces.length - 1
108
+ const end = round2(isLast ? Number(unit.end) : Math.max(cursor, start + 0.2))
109
+ return { start, end: Math.max(end, round2(start + 0.2)), text: piece, speaker: unit.speaker ?? null }
110
+ })
111
+ }
112
+
113
+ function endsSentence(text) {
114
+ const trimmed = String(text || '').trimEnd()
115
+ if (!trimmed) return true
116
+ const last = trimmed[trimmed.length - 1]
117
+ if (CLOSER.test(last)) {
118
+ const prev = trimmed[trimmed.length - 2] || ''
119
+ return STRONG_END.test(prev) || prev === '.'
120
+ }
121
+ if (STRONG_END.test(last) || SEMI.test(last)) return true
122
+ if (last === '.') return !ABBREV.test(trimmed)
123
+ return false
124
+ }
125
+
126
+ function joinText(left, right) {
127
+ const a = String(left || '')
128
+ const b = String(right || '')
129
+ if (!a) return b
130
+ if (!b) return a
131
+ // 两个拉丁词之间补一个空格,中文/日文直接拼。
132
+ return /[A-Za-z0-9,.;:!?)]$/.test(a) && /^[A-Za-z0-9('"]/.test(b) ? `${a} ${b}` : a + b
133
+ }
134
+
135
+ function canMerge(prev, next, config) {
136
+ if (endsSentence(prev.text)) return false
137
+ const gap = Number(next.start) - Number(prev.end)
138
+ if (!(gap >= -0.3 && gap <= config.maxPause)) return false
139
+ if (Number(prev.end) - Number(prev.start) >= config.maxDuration) return false
140
+ if (String(prev.text).length + String(next.text).length > config.maxChars) return false
141
+ // 说话人分离的结果不能混成一行。
142
+ if (prev.speaker != null && next.speaker != null && prev.speaker !== next.speaker) return false
143
+ return true
144
+ }
145
+
146
+ /**
147
+ * 把识别出来的碎片行整理成整句。
148
+ * @param {Array<{start: number, end: number, text: string, speaker?: number|null}>} lines
149
+ * @returns {Array<{id: number, start: number, end: number, text: string, speaker: number|null, translations: {}}>}
150
+ */
151
+ export function resegmentLines(lines, options = {}) {
152
+ const config = { ...DEFAULTS, ...(options || {}) }
153
+ const units = []
154
+ for (const line of Array.isArray(lines) ? lines : []) {
155
+ const text = String(line?.text || '').trim()
156
+ if (!text) continue
157
+ const start = Number(line.start) || 0
158
+ const rawEnd = Number(line.end)
159
+ const end = Number.isFinite(rawEnd) && rawEnd > start ? rawEnd : start + 0.4
160
+ const parts = splitSentences(text)
161
+ if (!parts.length) continue
162
+ const total = parts.reduce((sum, part) => sum + part.length, 0) || 1
163
+ const span = Math.max(end - start, 0.2)
164
+ let cursor = start
165
+ parts.forEach((part, index) => {
166
+ const share = (part.length / total) * span
167
+ const unitStart = round2(cursor)
168
+ cursor += share
169
+ const isLast = index === parts.length - 1
170
+ const unitEnd = round2(isLast ? end : Math.max(cursor, unitStart + 0.2))
171
+ units.push(
172
+ ...explodeUnit(
173
+ {
174
+ start: unitStart,
175
+ end: Math.max(unitEnd, round2(unitStart + 0.2)),
176
+ text: part,
177
+ speaker: line.speaker ?? null,
178
+ },
179
+ config,
180
+ ),
181
+ )
182
+ })
183
+ }
184
+
185
+ const merged = []
186
+ for (const unit of units) {
187
+ const prev = merged[merged.length - 1]
188
+ if (prev && canMerge(prev, unit, config)) {
189
+ prev.text = joinText(prev.text, unit.text)
190
+ prev.end = unit.end
191
+ if (prev.speaker == null) prev.speaker = unit.speaker ?? null
192
+ continue
193
+ }
194
+ merged.push({ ...unit })
195
+ }
196
+
197
+ return merged.map((line, index) => ({
198
+ id: index + 1,
199
+ start: round2(line.start),
200
+ end: round2(line.end),
201
+ text: line.text,
202
+ speaker: line.speaker ?? null,
203
+ translations: {},
204
+ }))
205
+ }
package/src/setup.js CHANGED
@@ -11,6 +11,7 @@ import { CATALOG, FFMPEG_BINARY, LLAMA_BINARY, SOURCE_LABELS, artifactFor, expec
11
11
  import { modelsDir } from './models.js'
12
12
  import { downloadArtifact, extractArchive } from './downloader.js'
13
13
  import { detectPlatform, findExecutable, ffmpegPath, ffprobePath, killTree, recommendEngine, venvDir, venvPython, isWindows, isMac } from './platform.js'
14
+ import { asrProvider } from './asr-api.js'
14
15
 
15
16
  const RUNTIME_DIR = path.join(DATA_DIR, 'runtime')
16
17
  const SETUP_PATH = path.join(DATA_DIR, 'setup.json')
@@ -202,6 +203,10 @@ export async function verify() {
202
203
  const engine = recommendEngine(platform)
203
204
  const artifacts = artifactFor(platform, engine)
204
205
  const checks = []
206
+ // 云端转写的用户根本不需要 python 运行时和本地模型:这些检查项标成 optional,
207
+ // 缺了不算「检查没过」,只在列表里显示成灰色,免得用户以为装坏了。
208
+ const cloudAsr = asrProvider() === 'api'
209
+ const cloudTranslate = String(getSection('translate')?.provider || 'api') !== 'local'
205
210
 
206
211
  const ffmpeg = ffmpegPath()
207
212
  checks.push({ id: 'ffmpeg', label: 'ffmpeg 已找到', ok: Boolean(ffmpeg), detail: ffmpeg || '未找到' })
@@ -226,7 +231,13 @@ export async function verify() {
226
231
  checks.push({ id: 'python-mlx', label: 'MLX 组件可导入', ok: mlx.ok, detail: mlx.detail })
227
232
  }
228
233
  } else {
229
- checks.push({ id: 'python', label: 'Python 环境存在', ok: false, detail: '虚拟环境不存在,请先安装' })
234
+ checks.push({
235
+ id: 'python',
236
+ label: 'Python 环境存在(本地模型才需要)',
237
+ ok: false,
238
+ optional: cloudAsr && cloudTranslate,
239
+ detail: '虚拟环境不存在;只用云端 API 的话可以忽略',
240
+ })
230
241
  }
231
242
 
232
243
  if (engine.id !== 'mlx') {
@@ -241,11 +252,13 @@ export async function verify() {
241
252
 
242
253
  for (const artifact of [artifacts.asr, artifacts.translate, artifacts.diarization]) {
243
254
  const state = modelState(artifact)
255
+ const cloudCovered = artifact.kind === 'asr' ? cloudAsr : artifact.kind === 'translate' ? cloudTranslate : false
244
256
  checks.push({
245
257
  id: `model-${artifact.id}`,
246
258
  label: `模型就绪:${artifact.label}`,
247
259
  ok: state.ready,
248
- detail: state.ready ? state.dir : artifact.note || '未下载',
260
+ optional: cloudCovered,
261
+ detail: state.ready ? state.dir : `${artifact.note || '未下载'}${cloudCovered ? '(当前走云端 API,不需要)' : ''}`,
249
262
  })
250
263
  }
251
264
 
@@ -254,21 +267,23 @@ export async function verify() {
254
267
  const config = getSection('models')
255
268
  const expected = expectedModels(platform, engine)
256
269
  const problems = []
257
- if (config.asrModel !== expected.asrModel) {
270
+ if (!cloudAsr && config.asrModel !== expected.asrModel) {
258
271
  problems.push(`转写模型应为 ${expected.asrModel},当前 ${config.asrModel || '未设置'}`)
259
272
  }
260
- if (config.translateModel !== expected.translateModel) {
273
+ if (!cloudTranslate && config.translateModel !== expected.translateModel) {
261
274
  problems.push(`翻译模型应为 ${expected.translateModel},当前 ${config.translateModel || '未设置'}`)
262
275
  }
263
276
  checks.push({
264
277
  id: 'config',
265
278
  label: '模型配置与当前平台匹配',
266
279
  ok: problems.length === 0,
280
+ optional: cloudAsr && cloudTranslate,
267
281
  detail: problems.length ? problems.join(';') : `${config.asrModel} / ${config.translateModel}`,
268
282
  })
269
283
 
270
284
  return {
271
- ok: checks.every((check) => check.ok),
285
+ // 只有「必须项」才决定通过与否;标了 optional 的缺了不算失败。
286
+ ok: checks.every((check) => check.ok || check.optional),
272
287
  checkedAt: new Date().toISOString(),
273
288
  platform,
274
289
  engine,
@@ -276,9 +291,12 @@ export async function verify() {
276
291
  }
277
292
  }
278
293
 
279
- export function plan({ source = 'auto', platform = detectPlatform() } = {}) {
294
+ // mode: 'basic' 只装云端转写必需的 ffmpeg;'local' 才装 python 运行时 + 本地模型。
295
+ // 默认 basic —— 绝大多数用户填个 key 就能用,不该为 2.4 GB 模型等上半小时。
296
+ export function plan({ source = 'auto', mode = 'basic', platform = detectPlatform() } = {}) {
280
297
  const engine = recommendEngine(platform)
281
298
  const artifacts = artifactFor(platform, engine)
299
+ const local = mode === 'local'
282
300
  const items = []
283
301
  const ffAsset = !ffmpegPath() ? ffmpegAsset(platform) : null
284
302
  if (ffAsset) {
@@ -290,7 +308,7 @@ export function plan({ source = 'auto', platform = detectPlatform() } = {}) {
290
308
  sources: ['GitHub', 'gh-proxy.com'],
291
309
  })
292
310
  }
293
- if (!runtimeReady(platform)) {
311
+ if (local && !runtimeReady(platform)) {
294
312
  items.push({
295
313
  id: 'runtime',
296
314
  label: 'Python 运行时 + 依赖(uv / PyPI)',
@@ -300,7 +318,7 @@ export function plan({ source = 'auto', platform = detectPlatform() } = {}) {
300
318
  note: '按 60–100 MB 估算,实际以 pip 下载为准',
301
319
  })
302
320
  }
303
- if (engine.id !== 'mlx' && !llamaBinary(platform)) {
321
+ if (local && engine.id !== 'mlx' && !llamaBinary(platform)) {
304
322
  const asset = llamaAsset(platform)
305
323
  items.push({
306
324
  id: 'llama',
@@ -311,7 +329,7 @@ export function plan({ source = 'auto', platform = detectPlatform() } = {}) {
311
329
  })
312
330
  void asset
313
331
  }
314
- for (const artifact of [artifacts.asr, artifacts.translate, artifacts.diarization]) {
332
+ for (const artifact of local ? [artifacts.asr, artifacts.translate, artifacts.diarization] : []) {
315
333
  if (modelState(artifact).ready) continue
316
334
  items.push({
317
335
  id: artifact.id,
@@ -327,6 +345,7 @@ export function plan({ source = 'auto', platform = detectPlatform() } = {}) {
327
345
  platform,
328
346
  engine,
329
347
  source,
348
+ mode: local ? 'local' : 'basic',
330
349
  items,
331
350
  totalBytes,
332
351
  note: engine.id === 'mlx' ? 'Apple Silicon 使用 MLX 模型;Windows / Linux 使用 ONNX + GGUF。' : 'Windows / Linux 使用 ONNX ASR + llama.cpp GGUF。',
@@ -417,13 +436,14 @@ function updateJob(patch) {
417
436
  return job
418
437
  }
419
438
 
420
- export async function install({ source = 'auto' } = {}) {
439
+ export async function install({ source = 'auto', mode = 'basic' } = {}) {
421
440
  if (current?.running) throw new Error('安装已在进行中')
422
441
  current = { running: true, cancelled: false, startedAt: Date.now(), child: null, signal: null }
423
442
  const platform = detectPlatform()
424
443
  const engine = recommendEngine(platform)
425
444
  const artifacts = artifactFor(platform, engine)
426
- const targets = [artifacts.asr, artifacts.translate, artifacts.diarization].filter((artifact) => !modelState(artifact).ready)
445
+ const local = mode === 'local'
446
+ const targets = local ? [artifacts.asr, artifacts.translate, artifacts.diarization].filter((artifact) => !modelState(artifact).ready) : []
427
447
  const ffAssetPending = !ffmpegPath() ? ffmpegAsset(platform) : null
428
448
  const totalBytes =
429
449
  targets.reduce((sum, artifact) => sum + (artifact.approxBytes || 0), 0) + (ffAssetPending?.bytes || 0)
@@ -470,8 +490,8 @@ export async function install({ source = 'auto' } = {}) {
470
490
  }
471
491
  }
472
492
 
473
- // 1. Python 运行时
474
- if (!runtimeReady(platform)) {
493
+ // 1. Python 运行时(只有本地模型才需要;云端转写不碰 python)
494
+ if (local && !runtimeReady(platform)) {
475
495
  report({ stage: '安装 uv 与 Python 运行时' })
476
496
  const uv = await ensureUv(platform, source, () => {})
477
497
  const venv = venvDir()
@@ -493,7 +513,7 @@ export async function install({ source = 'auto' } = {}) {
493
513
  }
494
514
 
495
515
  // 2. llama.cpp 运行时
496
- if (engine.id !== 'mlx' && !llamaBinary(platform)) {
516
+ if (local && engine.id !== 'mlx' && !llamaBinary(platform)) {
497
517
  const asset = llamaAsset(platform)
498
518
  if (!asset) throw new Error('当前平台没有 llama.cpp 预编译包')
499
519
  report({ stage: '下载 llama.cpp 运行时' })
@@ -543,8 +563,8 @@ export async function install({ source = 'auto' } = {}) {
543
563
  }
544
564
  }
545
565
 
546
- // 4. 写配置
547
- updateSection('models', { asrModel: artifacts.asr.dir, translateModel: artifacts.translate.dir })
566
+ // 4. 写配置(只有真下了本地模型才写,否则会把 asrModel 指向一个不存在的目录)
567
+ if (local) updateSection('models', { asrModel: artifacts.asr.dir, translateModel: artifacts.translate.dir })
548
568
  writeSetup({ engine: engine.id, source, installedAt: Date.now(), lastSummary: summary })
549
569
  const warning = optionalFailures.length
550
570
  ? `可选组件失败,已跳过:${optionalFailures.map((item) => `${item.label}(${item.error})`).join(';')}。核心转写与翻译已就绪,可以正常使用。`