fix(file-transcription): stop a Whisper repetition loop from spreading to every following chunk
Each 30-second chunk received the previous chunk's text as initial_prompt, so once one chunk looped on a word the loop became the next chunk's hint and the rest of the file was the same word. A looping chunk is now re-transcribed without the hint, looped text is never carried forward, and runs of 4+ identical phrases or segments are collapsed to one.
This commit is contained in:
parent
a14bb52b7f
commit
45d8b1ddb1
4 changed files with 236 additions and 6 deletions
|
|
@ -0,0 +1,103 @@
|
|||
// 파일 전사: 한 조각이 반복 루프에 빠져도 그 반복이 다음 조각으로 번지지 않는다.
|
||||
// (앞 조각 결과를 다음 조각 initial_prompt 로 넘겨, 초반 뒤로는 같은 단어만 되풀이되던 버그)
|
||||
|
||||
import { EventEmitter } from 'events'
|
||||
import fs from 'fs'
|
||||
import os from 'os'
|
||||
import path from 'path'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
const LOOP = Array(20).fill('감사합니다').join(' ')
|
||||
|
||||
const hoisted = vi.hoisted(() => ({
|
||||
spawn: vi.fn(),
|
||||
transcribe: vi.fn(),
|
||||
}))
|
||||
vi.mock('child_process', () => ({ spawn: hoisted.spawn }))
|
||||
vi.mock('../../../src/main/utils/paths', () => ({ getFfmpegPath: () => '/bundled/ffmpeg.exe' }))
|
||||
vi.mock('../../../src/main/services/RuntimeProvisioner', () => ({ getRuntimeProvisioner: () => ({ ensure: vi.fn() }) }))
|
||||
vi.mock('../../../src/main/services/LoggerService', () => ({
|
||||
getLogger: () => ({ info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() }),
|
||||
}))
|
||||
vi.mock('../../../src/main/services/ConfigService', () => ({ configGet: vi.fn(() => undefined) }))
|
||||
vi.mock('../../../src/main/services/stt/STTManager', () => ({
|
||||
getSTTManager: () => ({ transcribe: hoisted.transcribe }),
|
||||
}))
|
||||
vi.mock('../../../src/main/services/HistoryService', () => ({ getHistoryService: () => ({ create: vi.fn() }) }))
|
||||
vi.mock('../../../src/main/services/LicenseService', () => ({
|
||||
getLicenseService: () => ({ canUse: () => ({ allowed: true }), promptUpgrade: vi.fn() }),
|
||||
}))
|
||||
vi.mock('../../../src/main/windows/WindowManager', () => ({ getMainWindow: () => null }))
|
||||
|
||||
function fakeSpawn(_command: string, args: string[]): EventEmitter {
|
||||
const proc = Object.assign(new EventEmitter(), {
|
||||
stdout: new EventEmitter(),
|
||||
stderr: new EventEmitter(),
|
||||
exitCode: null as number | null,
|
||||
killed: false,
|
||||
kill: () => true,
|
||||
})
|
||||
queueMicrotask(() => {
|
||||
// 90초 = 30초 조각 3개
|
||||
if (args.includes('null')) proc.stderr.emit('data', Buffer.from('Duration: 00:01:30.00'))
|
||||
if (args.includes('pipe:1')) proc.stdout.emit('data', Buffer.from('pcm'))
|
||||
proc.exitCode = 0
|
||||
proc.emit('close', 0)
|
||||
})
|
||||
return proc
|
||||
}
|
||||
|
||||
describe('FileTranscriptionService 반복 루프', () => {
|
||||
let inputFile: string
|
||||
|
||||
beforeEach(() => {
|
||||
vi.resetModules()
|
||||
vi.clearAllMocks()
|
||||
hoisted.spawn.mockImplementation(fakeSpawn)
|
||||
inputFile = path.join(os.tmpdir(), `d3ro-ft-loop-${Date.now()}.mp3`)
|
||||
fs.writeFileSync(inputFile, 'x')
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
fs.rmSync(inputFile, { force: true })
|
||||
})
|
||||
|
||||
it('힌트 때문에 반복하면 힌트 없이 다시 전사하고, 반복은 다음 조각 힌트로 넘기지 않는다', async () => {
|
||||
const prompts: Array<string | undefined> = []
|
||||
const replies = [
|
||||
'첫 번째 내용입니다.', // 조각 1
|
||||
LOOP, // 조각 2 — 앞 문맥 힌트 때문에 루프
|
||||
'두 번째 내용입니다.', // 조각 2 재시도(힌트 없음)
|
||||
'세 번째 내용입니다.', // 조각 3
|
||||
]
|
||||
hoisted.transcribe.mockImplementation(async (_buf: Buffer, opts: { initialPrompt?: string }) => {
|
||||
prompts.push(opts.initialPrompt)
|
||||
const text = replies[prompts.length - 1]
|
||||
return { text, segments: [{ text, start: 0, end: 3 }] }
|
||||
})
|
||||
|
||||
const mod = await import('../../../src/main/services/FileTranscriptionService')
|
||||
mod.resetFileTranscriptionServiceForTests()
|
||||
const result = await mod.getFileTranscriptionService().startTranscription(inputFile, 'ko')
|
||||
|
||||
expect(result.fullText).toBe('첫 번째 내용입니다. 두 번째 내용입니다. 세 번째 내용입니다.')
|
||||
expect(result.fullText).not.toContain('감사합니다')
|
||||
// 재시도는 힌트 없이, 다음 조각은 정상 결과를 힌트로 받는다
|
||||
expect(prompts).toEqual([undefined, '첫 번째 내용입니다.', undefined, '두 번째 내용입니다.'])
|
||||
})
|
||||
|
||||
it('다시 들어도 반복이면 한 번으로 줄이고, 다음 조각에는 힌트를 주지 않는다', async () => {
|
||||
const prompts: Array<string | undefined> = []
|
||||
hoisted.transcribe.mockImplementation(async (_buf: Buffer, opts: { initialPrompt?: string }) => {
|
||||
prompts.push(opts.initialPrompt)
|
||||
return { text: `회의 시작 ${LOOP}`, segments: [] }
|
||||
})
|
||||
|
||||
const mod = await import('../../../src/main/services/FileTranscriptionService')
|
||||
mod.resetFileTranscriptionServiceForTests()
|
||||
const result = await mod.getFileTranscriptionService().startTranscription(inputFile, 'ko')
|
||||
|
||||
expect(result.fullText).toBe('회의 시작 감사합니다 회의 시작 감사합니다 회의 시작 감사합니다')
|
||||
expect(prompts.every((p) => p === undefined)).toBe(true)
|
||||
})
|
||||
})
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
// Whisper 반복 루프 정리 — 파일 전사에서 같은 단어만 되풀이되던 버그
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import {
|
||||
collapseRepeatedSegments,
|
||||
collapseRepeats,
|
||||
isDegenerateTranscript,
|
||||
} from '../../../../src/main/services/file-transcription/repetition'
|
||||
|
||||
describe('collapseRepeats', () => {
|
||||
it('4번 이상 이어진 같은 단어·구절을 한 번으로 줄인다', () => {
|
||||
expect(collapseRepeats('회의를 시작합니다. 감사합니다 감사합니다 감사합니다 감사합니다 감사합니다')).toBe(
|
||||
'회의를 시작합니다. 감사합니다',
|
||||
)
|
||||
expect(collapseRepeats('그래서 우리는 그래서 우리는 그래서 우리는 그래서 우리는 다음 주에')).toBe('그래서 우리는 다음 주에')
|
||||
expect(collapseRepeats('네.네.네.네.네.네.')).toBe('네.')
|
||||
})
|
||||
|
||||
it('실제 말에 나올 만한 짧은 반복(3번 이하)은 남긴다', () => {
|
||||
expect(collapseRepeats('네 네 네 알겠습니다')).toBe('네 네 네 알겠습니다')
|
||||
expect(collapseRepeats('정말 정말 좋았어요')).toBe('정말 정말 좋았어요')
|
||||
})
|
||||
})
|
||||
|
||||
describe('isDegenerateTranscript', () => {
|
||||
it('반복이 대부분이면 망가진 결과로 본다', () => {
|
||||
expect(isDegenerateTranscript(Array(20).fill('자막 제공').join(' '))).toBe(true)
|
||||
expect(isDegenerateTranscript('오늘 안건은 예산과 일정입니다. 먼저 예산부터 보겠습니다.')).toBe(false)
|
||||
expect(isDegenerateTranscript('')).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('collapseRepeatedSegments', () => {
|
||||
it('같은 문장의 연속 구간을 하나로 합치고 끝 시각을 늘린다', () => {
|
||||
const segments = [
|
||||
{ text: '시작하겠습니다', start: 0, end: 2 },
|
||||
{ text: '감사합니다', start: 2, end: 4 },
|
||||
{ text: '감사합니다.', start: 4, end: 6 },
|
||||
{ text: '감사합니다', start: 6, end: 8 },
|
||||
{ text: '다음 안건입니다', start: 8, end: 10 },
|
||||
]
|
||||
expect(collapseRepeatedSegments(segments)).toEqual([
|
||||
{ text: '시작하겠습니다', start: 0, end: 2 },
|
||||
{ text: '감사합니다', start: 2, end: 8 },
|
||||
{ text: '다음 안건입니다', start: 8, end: 10 },
|
||||
])
|
||||
})
|
||||
})
|
||||
Loading…
Add table
Add a link
Reference in a new issue