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:
@@ -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(
|
||||
params: Parameters<typeof transcriptService.saveResult>[0],
|
||||
): Promise<void> {
|
||||
let { segments } = params
|
||||
const script = chineseTranscriptLanguageSchema.safeParse(params.language)
|
||||
if (script.success) {
|
||||
const { Converter } = await import('opencc-js')
|
||||
const convert =
|
||||
script.data === 'zh-Hans'
|
||||
? Converter({ from: 't', to: 'cn' })
|
||||
: Converter({ from: 'cn', to: 't' })
|
||||
segments = segments.map((segment) => ({ ...segment, text: convert(segment.text) }))
|
||||
let { language, segments } = params
|
||||
if (language === 'zh' && segments.length === 0) language = null
|
||||
const script = chineseTranscriptLanguageSchema.safeParse(language)
|
||||
if (script.success || language === 'zh') {
|
||||
const { createChineseTranscriptConverter, inferChineseTranscriptScript } =
|
||||
await import('./chinese-transcript-script')
|
||||
const inferredScript = script.success ? null : inferChineseTranscriptScript(segments)
|
||||
if (inferredScript) {
|
||||
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 })
|
||||
}
|
||||
})
|
||||
|
||||
@@ -315,22 +315,23 @@ export class TranscriptService {
|
||||
where: { assetIdLanguage: { assetId, language } },
|
||||
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: {
|
||||
assetId,
|
||||
type,
|
||||
status: 'completed',
|
||||
payload: { path: ['transcription', 'language'], equals: Prisma.JsonNull },
|
||||
payload: { path: ['transcription', 'language'], equals: language ?? Prisma.JsonNull },
|
||||
},
|
||||
select: { output: true },
|
||||
orderBy: { id: 'desc' },
|
||||
})
|
||||
const result = auto?.output?.transcription
|
||||
const result = task?.output?.transcription
|
||||
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({
|
||||
where: { id: result.transcriptId },
|
||||
select: transcriptSelection,
|
||||
|
||||
@@ -297,7 +297,7 @@ describe('Transcribe video workflow', () => {
|
||||
)
|
||||
}, 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'
|
||||
transcription = {
|
||||
language: 'zh',
|
||||
@@ -309,39 +309,97 @@ describe('Transcribe video workflow', () => {
|
||||
const { task, assetId } = await submitVideo('audio.mp4', null)
|
||||
await workflowService.executeWait(task, 20_000)
|
||||
|
||||
for (const [language, text] of [
|
||||
['zh-Hans', '头发和头发,软件与软体,Hello'],
|
||||
['zh-Hant', '頭髮和頭髮,軟件與軟體,Hello'],
|
||||
]) {
|
||||
const { task } = await startTranscript(assetId, language)
|
||||
expect(task.payload?.transcription?.language).toBe(language)
|
||||
await workflowService.executeWait(task, 20_000)
|
||||
const saved = await transcriptService.getSavedTranscript(assetId, language)
|
||||
expect(saved).toMatchObject({
|
||||
language,
|
||||
segments: [
|
||||
{ startMs: 1012, endMs: 1875, text },
|
||||
{ startMs: 1875, endMs: 2000, text: 'Bye' },
|
||||
],
|
||||
})
|
||||
await expect(transcriptService.generate(assetId, language)).resolves.toEqual({
|
||||
const simplified = await transcriptService.getSavedTranscript(assetId, 'zh-Hans')
|
||||
expect(simplified).toMatchObject({
|
||||
language: 'zh-Hans',
|
||||
segments: [
|
||||
{ startMs: 1012, endMs: 1875, text: '头发和头发,软件与软体,Hello' },
|
||||
{ startMs: 1875, endMs: 2000, text: 'Bye' },
|
||||
],
|
||||
})
|
||||
for (const request of [null, 'zh-Hans']) {
|
||||
await expect(transcriptService.generate(assetId, request)).resolves.toEqual({
|
||||
kind: 'existing',
|
||||
transcriptId: saved.id,
|
||||
language,
|
||||
transcriptId: simplified.id,
|
||||
language: 'zh-Hans',
|
||||
})
|
||||
}
|
||||
|
||||
expect(uploads.map(({ language }) => language)).toEqual([null, 'zh', 'zh'])
|
||||
const { languages } = await transcriptService.listSavedLanguages(assetId)
|
||||
expect(languages.sort()).toEqual(['zh', 'zh-Hans', 'zh-Hant'])
|
||||
const simplified = await transcriptService.getSavedTranscript(assetId, 'zh-Hans')
|
||||
const { task: traditionalTask } = await startTranscript(assetId, 'zh-Hant')
|
||||
expect(traditionalTask.payload?.transcription?.language).toBe('zh-Hant')
|
||||
await workflowService.executeWait(traditionalTask, 20_000)
|
||||
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 expect(transcriptService.getSavedTranscript(assetId, 'zh-Hant')).resolves.toEqual(
|
||||
traditional,
|
||||
)
|
||||
await expect(transcriptService.getSavedTranscript(assetId, 'zh')).resolves.toMatchObject({
|
||||
segments: [{ text: mixed }, { text: 'Bye' }],
|
||||
}, 45_000)
|
||||
|
||||
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)
|
||||
|
||||
@@ -357,6 +415,27 @@ describe('Transcribe video workflow', () => {
|
||||
expect(await transcriptService.listSavedLanguages(assetId)).toEqual({ languages: [] })
|
||||
}, 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([
|
||||
['en', 'Transcription returned an unexpected language'],
|
||||
['zh-Hant', 'Invalid transcription response'],
|
||||
|
||||
Reference in New Issue
Block a user