Skip to content

Commit

Permalink
optimize: Re-phase the flow, skip translating if no chunks detected
Browse files Browse the repository at this point in the history
  • Loading branch information
joyqi committed Jan 5, 2024
1 parent ad4f4a8 commit 26c9a4e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const argv = yargs(hideBin(process.argv))
alias: 'f',
type: 'string',
description: 'The type of the file formatter',
choices: ['auto', 'json', 'yaml', 'markdown'],
choices: ['auto', 'json', 'yaml', 'markdown', 'srt'],
demandOption: true,
default: 'auto'
})
Expand Down
55 changes: 29 additions & 26 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,6 @@ export async function translate<T extends StructureType, F extends FormatterType
const [srcEnc, srcLangAuto] = await detectFile(srcFile);
const [dstEnc] = existsSync(dstFile) ? await detectFile(dstFile) : [srcEnc];

srcLang = await rephraseLanguage(chat, srcLang === 'auto' ? srcLangAuto : srcLang);
dstLang = await rephraseLanguage(chat, dstLang);
prompt = await rephrasePrompt(chat, prompt);

const srcText = readFileSync(srcFile, srcEnc);
const dstText = existsSync(dstFile) ? readFileSync(dstFile, dstEnc) : '';
const srcIndent = Math.max(2, detectIndent(srcText).amount);
Expand All @@ -163,34 +159,41 @@ export async function translate<T extends StructureType, F extends FormatterType
const chunks = structure.split(patch, enc, chunkSize);
const translated = [];

console.table({
'Model': model,
'Format': format,
'Structure': type,
'Source File': srcFile + ` (encoding:${srcEnc} indent:${srcIndent})`,
'Destination File': dstFile + ` (encoding:${dstEnc} indent:${dstIndent})`,
'Source Language': srcLang,
'Destination Language': dstLang,
'Chunk Size': chunkSize,
'Chunks': chunks.length,
});
if (chunks.length > 0) {
srcLang = await rephraseLanguage(chat, srcLang === 'auto' ? srcLangAuto : srcLang);
dstLang = await rephraseLanguage(chat, dstLang);
prompt = await rephrasePrompt(chat, prompt);

console.table({
'Model': model,
'Format': format,
'Structure': type,
'Source File': srcFile + ` (encoding:${srcEnc} indent:${srcIndent})`,
'Destination File': dstFile + ` (encoding:${dstEnc} indent:${dstIndent})`,
'Source Language': srcLang,
'Destination Language': dstLang,
'Chunk Size': chunkSize,
'Chunks': chunks.length,
});

const spinner = ora('Start translating').start();
const spinner = ora('Start translating').start();

for (const chunk of chunks) {
spinner.text = `Translating ${translated.length + 1}/${chunks.length} chunks`;
for (const chunk of chunks) {
spinner.text = `Translating ${translated.length + 1}/${chunks.length} chunks`;

const { text } = await chain.call({
input_language: srcLang,
output_language: dstLang,
prompt,
text: JSON.stringify(chunk)
});
const { text } = await chain.call({
input_language: srcLang,
output_language: dstLang,
prompt,
text: JSON.stringify(chunk)
});

translated.push(JSON.parse(text));
}

translated.push(JSON.parse(text));
spinner.succeed(`Translated ${chunks.length} chunks`);
}

spinner.succeed(`Translated ${chunks.length} chunks`);
const translatedPatch = structure.join(translated);
const translatedData = structure.merge(keep, translatedPatch);

Expand Down

0 comments on commit 26c9a4e

Please sign in to comment.