mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-05 15:44:28 +00:00
feat(transcript): detect Chinese script for Auto (#563)
This commit is contained in:
@@ -121,7 +121,7 @@ apps/web/e2e/
|
|||||||
| Upload a txt file, wait for transcode, and create a comment with draw | `tests/file/txt-draw-comment.spec.ts` |
|
| Upload a txt file, wait for transcode, and create a comment with draw | `tests/file/txt-draw-comment.spec.ts` |
|
||||||
| Upload an image file, wait for transcode, and create a comment with draw | `tests/file/image-draw-comment.spec.ts` |
|
| Upload an image file, wait for transcode, and create a comment with draw | `tests/file/image-draw-comment.spec.ts` |
|
||||||
| Upload a video file, wait for transcode, and create a comment with draw at a non-zero timestamp | `tests/file/video-draw-comment.spec.ts` |
|
| Upload a video file, wait for transcode, and create a comment with draw at a non-zero timestamp | `tests/file/video-draw-comment.spec.ts` |
|
||||||
| Read and search saved video transcripts, resize the desktop panel, jump to a segment, pause and resume playback following through manual scrolling and sync, and check mobile and version-stack behavior | `tests/file/view-transcript.spec.ts` |
|
| Read and search saved video transcripts, switch to Traditional Chinese, resize the desktop panel, jump to a segment, pause and resume playback following through manual scrolling and sync, and check mobile and version-stack behavior | `tests/file/view-transcript.spec.ts` |
|
||||||
| Edit video transcript text inline, verify playback pauses and timestamps survive a blank edit, retry a failed save, and check reviewer permissions | `tests/file/view-transcript.spec.ts` |
|
| Edit video transcript text inline, verify playback pauses and timestamps survive a blank edit, retry a failed save, and check reviewer permissions | `tests/file/view-transcript.spec.ts` |
|
||||||
| Upload an audio file, wait for transcode, and create a comment at a non-zero timestamp | `tests/file/audio-timestamp-comment.spec.ts` |
|
| Upload an audio file, wait for transcode, and create a comment at a non-zero timestamp | `tests/file/audio-timestamp-comment.spec.ts` |
|
||||||
| Create internal and public comments, verify internal comments are hidden on public share links | `tests/file/internal-comment-and-edit.spec.ts` |
|
| Create internal and public comments, verify internal comments are hidden on public share links | `tests/file/internal-comment-and-edit.spec.ts` |
|
||||||
|
|||||||
@@ -50,6 +50,15 @@ test('reads, searches and follows a saved transcript in the video player', async
|
|||||||
segments: { create: { position: 0, startMs: 0, endMs: 700, text: '你好。' } },
|
segments: { create: { position: 0, startMs: 0, endMs: 700, text: '你好。' } },
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
await prisma.assetTranscript.create({
|
||||||
|
data: {
|
||||||
|
assetId: fileId,
|
||||||
|
language: 'zh-Hant',
|
||||||
|
model: 'whisper-tiny',
|
||||||
|
workflowTaskId: 'auto-traditional-test',
|
||||||
|
segments: { create: { position: 0, startMs: 0, endMs: 700, text: '這是轉錄。' } },
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
await page.goto(`/projects/${projectId}/files/${fileId}`)
|
await page.goto(`/projects/${projectId}/files/${fileId}`)
|
||||||
const toggle = page.getByRole('button', { name: 'Show transcript' })
|
const toggle = page.getByRole('button', { name: 'Show transcript' })
|
||||||
@@ -218,9 +227,13 @@ test('reads, searches and follows a saved transcript in the video player', async
|
|||||||
await expect(closingWord).toHaveAttribute('aria-current', 'true')
|
await expect(closingWord).toHaveAttribute('aria-current', 'true')
|
||||||
await expect(video).toHaveJSProperty('paused', true)
|
await expect(video).toHaveJSProperty('paused', true)
|
||||||
|
|
||||||
await panel.getByRole('combobox', { name: 'Language' }).selectOption('zh')
|
const language = panel.getByRole('combobox', { name: 'Language' })
|
||||||
|
await language.selectOption('zh')
|
||||||
await expect(panel.getByText('你好。')).toBeVisible()
|
await expect(panel.getByText('你好。')).toBeVisible()
|
||||||
await expect(last).toHaveCount(0)
|
await expect(last).toHaveCount(0)
|
||||||
|
await language.selectOption({ label: 'Chinese (Traditional)' })
|
||||||
|
await expect(language).toHaveValue('zh-Hant')
|
||||||
|
await expect(panel.getByText('這是轉錄。')).toBeVisible()
|
||||||
await dragDivider(divider, 64)
|
await dragDivider(divider, 64)
|
||||||
await expect(panel).toHaveCSS('width', '448px')
|
await expect(panel).toHaveCSS('width', '448px')
|
||||||
await panel.getByRole('button', { name: 'Close transcript' }).click()
|
await panel.getByRole('button', { name: 'Close transcript' }).click()
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
import { describe, expect, it } from 'vitest'
|
||||||
|
import { inferChineseTranscriptScript } from './chinese-transcript-script'
|
||||||
|
|
||||||
|
describe('inferChineseTranscriptScript', () => {
|
||||||
|
it.each([
|
||||||
|
{ segments: ['你好。', '这是转录。'], expected: 'zh-Hans' },
|
||||||
|
{ segments: ['你好。', '這是轉錄。'], expected: 'zh-Hant' },
|
||||||
|
{ segments: ['了解這件事。', '這是台灣。'], expected: 'zh-Hant' },
|
||||||
|
{ segments: ['這是神。'], expected: 'zh-Hant' },
|
||||||
|
{ segments: ['你好。', 'Hello.'], expected: null },
|
||||||
|
{ segments: ['这是轉錄。'], expected: null },
|
||||||
|
{ segments: ['这是转录。', '下一段轉錄。'], expected: null },
|
||||||
|
{ segments: ['下一段轉錄。', '这是转录。'], expected: null },
|
||||||
|
])('infers $expected from $segments', ({ segments, expected }) => {
|
||||||
|
expect(inferChineseTranscriptScript(segments.map((text) => ({ text })))).toBe(expected)
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
import type { chineseTranscriptLanguageSchema } from '@shumai/dtos'
|
||||||
|
import { Converter } from 'opencc-js'
|
||||||
|
|
||||||
|
type ChineseTranscriptScript = (typeof chineseTranscriptLanguageSchema.options)[number]
|
||||||
|
|
||||||
|
// OpenCC's STCharacters and TSCharacters, opencc-data 1.4.2 (Apache-2.0).
|
||||||
|
// These characters have themselves as an alternative even though the default conversion changes them.
|
||||||
|
// https://github.com/nk2028/opencc-data/tree/4bcdc1064a298884d673dee8bbe91b2508cac6f5/data
|
||||||
|
const sharedScriptCharacters = new Set(
|
||||||
|
'㐹万丑丰于云亘仆价伙余佣党准几凶划占厂厘台叶吁吃后咨咸唇夸尸岩岳帘干广征愿挂据斗朴杠杰极柜涂涌淀游熏皂确种筑胜腊腌膻苹范荐虫蚝蜡蝎跖辟适郁采里雇乾剋劄吒夥徵扞於昇氾祕脩蒐薹袷谿釐陞麽',
|
||||||
|
)
|
||||||
|
|
||||||
|
export function createChineseTranscriptConverter(script: ChineseTranscriptScript) {
|
||||||
|
return script === 'zh-Hans'
|
||||||
|
? Converter({ from: 't', to: 'cn' })
|
||||||
|
: Converter({ from: 'cn', to: 't' })
|
||||||
|
}
|
||||||
|
|
||||||
|
export function inferChineseTranscriptScript(
|
||||||
|
segments: readonly { text: string }[],
|
||||||
|
): ChineseTranscriptScript | null {
|
||||||
|
const toSimplified = createChineseTranscriptConverter('zh-Hans')
|
||||||
|
const toTraditional = createChineseTranscriptConverter('zh-Hant')
|
||||||
|
let hasSimplifiedEvidence = false
|
||||||
|
let hasTraditionalEvidence = false
|
||||||
|
|
||||||
|
for (const { text } of segments) {
|
||||||
|
// Compare characters so phrase-level spelling changes do not count as script evidence.
|
||||||
|
for (const character of text.normalize('NFC')) {
|
||||||
|
if (sharedScriptCharacters.has(character)) continue
|
||||||
|
hasSimplifiedEvidence ||= toTraditional(character) !== character
|
||||||
|
hasTraditionalEvidence ||= toSimplified(character) !== character
|
||||||
|
if (hasSimplifiedEvidence && hasTraditionalEvidence) return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hasSimplifiedEvidence) return 'zh-Hans'
|
||||||
|
return hasTraditionalEvidence ? 'zh-Hant' : null
|
||||||
|
}
|
||||||
@@ -181,17 +181,23 @@ export const transcribeAudioActivity = defineStep(async function transcribeAudio
|
|||||||
export const saveTranscriptActivity = defineStep(async function saveTranscriptActivity(
|
export const saveTranscriptActivity = defineStep(async function saveTranscriptActivity(
|
||||||
params: Parameters<typeof transcriptService.saveResult>[0],
|
params: Parameters<typeof transcriptService.saveResult>[0],
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
let { segments } = params
|
let { language, segments } = params
|
||||||
const script = chineseTranscriptLanguageSchema.safeParse(params.language)
|
if (language === 'zh' && segments.length === 0) language = null
|
||||||
if (script.success) {
|
const script = chineseTranscriptLanguageSchema.safeParse(language)
|
||||||
const { Converter } = await import('opencc-js')
|
if (script.success || language === 'zh') {
|
||||||
const convert =
|
const { createChineseTranscriptConverter, inferChineseTranscriptScript } =
|
||||||
script.data === 'zh-Hans'
|
await import('./chinese-transcript-script')
|
||||||
? Converter({ from: 't', to: 'cn' })
|
const inferredScript = script.success ? null : inferChineseTranscriptScript(segments)
|
||||||
: Converter({ from: 'cn', to: 't' })
|
if (inferredScript) {
|
||||||
segments = segments.map((segment) => ({ ...segment, text: convert(segment.text) }))
|
language = inferredScript
|
||||||
|
} else {
|
||||||
|
const outputScript = script.success ? script.data : 'zh-Hans'
|
||||||
|
const convert = createChineseTranscriptConverter(outputScript)
|
||||||
|
language = outputScript
|
||||||
|
segments = segments.map((segment) => ({ ...segment, text: convert(segment.text) }))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (!(await transcriptService.saveResult({ ...params, segments }))) {
|
if (!(await transcriptService.saveResult({ ...params, language, segments }))) {
|
||||||
throw ApplicationFailure.create({ message: 'Video is no longer available', nonRetryable: true })
|
throw ApplicationFailure.create({ message: 'Video is no longer available', nonRetryable: true })
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -315,22 +315,23 @@ export class TranscriptService {
|
|||||||
where: { assetIdLanguage: { assetId, language } },
|
where: { assetIdLanguage: { assetId, language } },
|
||||||
select: transcriptSelection,
|
select: transcriptSelection,
|
||||||
})
|
})
|
||||||
return transcript ? { kind: 'existing', transcriptId: transcript.id, language } : null
|
if (transcript) return { kind: 'existing', transcriptId: transcript.id, language }
|
||||||
|
if (language !== 'zh') return null
|
||||||
}
|
}
|
||||||
|
|
||||||
const auto = await db.workflowTask.findFirst({
|
const task = await db.workflowTask.findFirst({
|
||||||
where: {
|
where: {
|
||||||
assetId,
|
assetId,
|
||||||
type,
|
type,
|
||||||
status: 'completed',
|
status: 'completed',
|
||||||
payload: { path: ['transcription', 'language'], equals: Prisma.JsonNull },
|
payload: { path: ['transcription', 'language'], equals: language ?? Prisma.JsonNull },
|
||||||
},
|
},
|
||||||
select: { output: true },
|
select: { output: true },
|
||||||
orderBy: { id: 'desc' },
|
orderBy: { id: 'desc' },
|
||||||
})
|
})
|
||||||
const result = auto?.output?.transcription
|
const result = task?.output?.transcription
|
||||||
if (!result) return null
|
if (!result) return null
|
||||||
if ('noSpeech' in result) return { kind: 'no_speech' }
|
if ('noSpeech' in result) return language === null ? { kind: 'no_speech' } : null
|
||||||
const transcript = await db.assetTranscript.findUnique({
|
const transcript = await db.assetTranscript.findUnique({
|
||||||
where: { id: result.transcriptId },
|
where: { id: result.transcriptId },
|
||||||
select: transcriptSelection,
|
select: transcriptSelection,
|
||||||
|
|||||||
@@ -297,7 +297,7 @@ describe('Transcribe video workflow', () => {
|
|||||||
)
|
)
|
||||||
}, 24_000)
|
}, 24_000)
|
||||||
|
|
||||||
it('saves both requested Chinese scripts alongside the original auto-detected transcript', async () => {
|
it('saves mixed Chinese from Auto as Simplified and lets Traditional coexist', async () => {
|
||||||
const mixed = '頭髮和头发,软件与軟體,Hello'
|
const mixed = '頭髮和头发,软件与軟體,Hello'
|
||||||
transcription = {
|
transcription = {
|
||||||
language: 'zh',
|
language: 'zh',
|
||||||
@@ -309,39 +309,97 @@ describe('Transcribe video workflow', () => {
|
|||||||
const { task, assetId } = await submitVideo('audio.mp4', null)
|
const { task, assetId } = await submitVideo('audio.mp4', null)
|
||||||
await workflowService.executeWait(task, 20_000)
|
await workflowService.executeWait(task, 20_000)
|
||||||
|
|
||||||
for (const [language, text] of [
|
const simplified = await transcriptService.getSavedTranscript(assetId, 'zh-Hans')
|
||||||
['zh-Hans', '头发和头发,软件与软体,Hello'],
|
expect(simplified).toMatchObject({
|
||||||
['zh-Hant', '頭髮和頭髮,軟件與軟體,Hello'],
|
language: 'zh-Hans',
|
||||||
]) {
|
segments: [
|
||||||
const { task } = await startTranscript(assetId, language)
|
{ startMs: 1012, endMs: 1875, text: '头发和头发,软件与软体,Hello' },
|
||||||
expect(task.payload?.transcription?.language).toBe(language)
|
{ startMs: 1875, endMs: 2000, text: 'Bye' },
|
||||||
await workflowService.executeWait(task, 20_000)
|
],
|
||||||
const saved = await transcriptService.getSavedTranscript(assetId, language)
|
})
|
||||||
expect(saved).toMatchObject({
|
for (const request of [null, 'zh-Hans']) {
|
||||||
language,
|
await expect(transcriptService.generate(assetId, request)).resolves.toEqual({
|
||||||
segments: [
|
|
||||||
{ startMs: 1012, endMs: 1875, text },
|
|
||||||
{ startMs: 1875, endMs: 2000, text: 'Bye' },
|
|
||||||
],
|
|
||||||
})
|
|
||||||
await expect(transcriptService.generate(assetId, language)).resolves.toEqual({
|
|
||||||
kind: 'existing',
|
kind: 'existing',
|
||||||
transcriptId: saved.id,
|
transcriptId: simplified.id,
|
||||||
language,
|
language: 'zh-Hans',
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
expect(uploads.map(({ language }) => language)).toEqual([null, 'zh', 'zh'])
|
const { task: traditionalTask } = await startTranscript(assetId, 'zh-Hant')
|
||||||
const { languages } = await transcriptService.listSavedLanguages(assetId)
|
expect(traditionalTask.payload?.transcription?.language).toBe('zh-Hant')
|
||||||
expect(languages.sort()).toEqual(['zh', 'zh-Hans', 'zh-Hant'])
|
await workflowService.executeWait(traditionalTask, 20_000)
|
||||||
const simplified = await transcriptService.getSavedTranscript(assetId, 'zh-Hans')
|
|
||||||
const traditional = await transcriptService.getSavedTranscript(assetId, 'zh-Hant')
|
const traditional = await transcriptService.getSavedTranscript(assetId, 'zh-Hant')
|
||||||
|
expect(traditional).toMatchObject({
|
||||||
|
language: 'zh-Hant',
|
||||||
|
segments: [
|
||||||
|
{ startMs: 1012, endMs: 1875, text: '頭髮和頭髮,軟件與軟體,Hello' },
|
||||||
|
{ startMs: 1875, endMs: 2000, text: 'Bye' },
|
||||||
|
],
|
||||||
|
})
|
||||||
|
expect(uploads.map(({ language }) => language)).toEqual([null, 'zh'])
|
||||||
|
const { languages } = await transcriptService.listSavedLanguages(assetId)
|
||||||
|
expect(languages.sort()).toEqual(['zh-Hans', 'zh-Hant'])
|
||||||
|
|
||||||
await transcriptService.updateSegmentText(assetId, 'zh-Hans', simplified.segments[0].id, '你好')
|
await transcriptService.updateSegmentText(assetId, 'zh-Hans', simplified.segments[0].id, '你好')
|
||||||
await expect(transcriptService.getSavedTranscript(assetId, 'zh-Hant')).resolves.toEqual(
|
await expect(transcriptService.getSavedTranscript(assetId, 'zh-Hant')).resolves.toEqual(
|
||||||
traditional,
|
traditional,
|
||||||
)
|
)
|
||||||
await expect(transcriptService.getSavedTranscript(assetId, 'zh')).resolves.toMatchObject({
|
}, 45_000)
|
||||||
segments: [{ text: mixed }, { text: 'Bye' }],
|
|
||||||
|
it.each([
|
||||||
|
{ selection: 'Auto', request: null, text: '这是转录。', language: 'zh-Hans' },
|
||||||
|
{ selection: 'Auto', request: null, text: '了解台灣的這段轉錄。', language: 'zh-Hant' },
|
||||||
|
{ selection: 'Auto', request: null, text: '你好。', language: 'zh-Hans' },
|
||||||
|
{ selection: 'Chinese', request: 'zh', text: '這是轉錄。', language: 'zh-Hant' },
|
||||||
|
])(
|
||||||
|
'saves $text as $language when requested through $selection',
|
||||||
|
async ({ request, text, language }) => {
|
||||||
|
transcription = { language: 'zh', segments: [{ start: 1.012, end: 1.875, text }] }
|
||||||
|
const { task, assetId } = await submitVideo('audio.mp4', request)
|
||||||
|
await workflowService.executeWait(task, 20_000)
|
||||||
|
|
||||||
|
expect(uploads.map((upload) => upload.language)).toEqual([request])
|
||||||
|
await expect(transcriptService.listSavedLanguages(assetId)).resolves.toEqual({
|
||||||
|
languages: [language],
|
||||||
|
})
|
||||||
|
const saved = await transcriptService.getSavedTranscript(assetId, language)
|
||||||
|
expect(saved).toMatchObject({ language, segments: [{ startMs: 1012, endMs: 1875, text }] })
|
||||||
|
await expect(transcriptService.generate(assetId, request)).resolves.toEqual({
|
||||||
|
kind: 'existing',
|
||||||
|
transcriptId: saved.id,
|
||||||
|
language,
|
||||||
|
})
|
||||||
|
},
|
||||||
|
24_000,
|
||||||
|
)
|
||||||
|
|
||||||
|
it('reuses an edited explicit transcript when Auto produces the same script', async () => {
|
||||||
|
transcription = { language: 'zh', segments: [{ start: 1.012, end: 1.875, text: '這是轉錄。' }] }
|
||||||
|
const { task, assetId } = await submitVideo('audio.mp4', 'zh-Hans')
|
||||||
|
await workflowService.executeWait(task, 20_000)
|
||||||
|
const explicit = await transcriptService.getSavedTranscript(assetId, 'zh-Hans')
|
||||||
|
expect(explicit.segments[0].text).toBe('这是转录。')
|
||||||
|
await transcriptService.updateSegmentText(
|
||||||
|
assetId,
|
||||||
|
'zh-Hans',
|
||||||
|
explicit.segments[0].id,
|
||||||
|
'这是修改。',
|
||||||
|
)
|
||||||
|
|
||||||
|
transcription = { language: 'zh', segments: [{ start: 1.012, end: 1.875, text: '这是转录。' }] }
|
||||||
|
const { task: autoTask } = await startTranscript(assetId, null)
|
||||||
|
await workflowService.executeWait(autoTask, 20_000)
|
||||||
|
|
||||||
|
await expect(transcriptService.listSavedLanguages(assetId)).resolves.toEqual({
|
||||||
|
languages: ['zh-Hans'],
|
||||||
|
})
|
||||||
|
await expect(transcriptService.generate(assetId, null)).resolves.toEqual({
|
||||||
|
kind: 'existing',
|
||||||
|
transcriptId: explicit.id,
|
||||||
|
language: 'zh-Hans',
|
||||||
|
})
|
||||||
|
await expect(transcriptService.getSavedTranscript(assetId, 'zh-Hans')).resolves.toMatchObject({
|
||||||
|
segments: [{ text: '这是修改。' }],
|
||||||
})
|
})
|
||||||
}, 45_000)
|
}, 45_000)
|
||||||
|
|
||||||
@@ -357,6 +415,27 @@ describe('Transcribe video workflow', () => {
|
|||||||
expect(await transcriptService.listSavedLanguages(assetId)).toEqual({ languages: [] })
|
expect(await transcriptService.listSavedLanguages(assetId)).toEqual({ languages: [] })
|
||||||
}, 24_000)
|
}, 24_000)
|
||||||
|
|
||||||
|
it('reports no speech for empty Chinese Auto output even when Simplified already exists', async () => {
|
||||||
|
const asset = await createVideo('audio.mp4')
|
||||||
|
await prisma.assetTranscript.create({
|
||||||
|
data: {
|
||||||
|
assetId: asset.id,
|
||||||
|
language: 'zh-Hans',
|
||||||
|
model: 'whisper-tiny',
|
||||||
|
workflowTaskId: 'old',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
transcription = { language: 'zh', segments: [] }
|
||||||
|
const { task } = await startTranscript(asset.id, null)
|
||||||
|
await workflowService.executeWait(task, 20_000)
|
||||||
|
|
||||||
|
expect(await transcriptService.getStatus(asset.id)).toEqual({
|
||||||
|
taskId: task.id,
|
||||||
|
status: 'no_speech',
|
||||||
|
})
|
||||||
|
await expect(transcriptService.generate(asset.id, null)).resolves.toEqual({ kind: 'no_speech' })
|
||||||
|
}, 24_000)
|
||||||
|
|
||||||
it.each([
|
it.each([
|
||||||
['en', 'Transcription returned an unexpected language'],
|
['en', 'Transcription returned an unexpected language'],
|
||||||
['zh-Hant', 'Invalid transcription response'],
|
['zh-Hant', 'Invalid transcription response'],
|
||||||
|
|||||||
Reference in New Issue
Block a user