ChatGPT has also demonstrated its capabilities as a robust translator, capable of handling not just common languages, but also unconventional forms of writing like emojis and word scrambling. However, it may not always produce a deterministic output and adhere to line-to-line correlation, potentially disrupting the timing of subtitles, even when instructed to follow precise instructions and setting the model temperature
parameter to 0
.
This utility uses the OpenAI ChatGPT API to translate text, with a specific focus on line-based translation, especially for SRT subtitles. The translator optimizes token usage by removing SRT overhead, grouping text into batches, resulting in arbitrary length translations without excessive token consumption while ensuring a one-to-one match between line input and output.
Reference: https://github.com/openai/openai-quickstart-node#setup
>= 16.13.0
required. This README assumes bash
shell environmentgit clone https://github.com/Cerlancism/chatgpt-subtitle-translator && cd chatgpt-subtitle-translator
npm install
chmod +x cli/translator.mjs
.example.env
to .env
cp .env.example .env
.env
file
cli/translator.mjs --help
Usage: translator [options]
Translation tool based on ChatGPT API
Options:
--from <language>
--to <language>
-i, --input <file>
.srt
format or plain text-o, --output <file>
-p, --plain-text <text>
-s, --system-instruction <instruction>
Translate ${from} to ${to}
with this plain text, ignoring --from
and --to
options--initial-prompts <prompts>
"[]"
) --no-use-moderator
--no-prefix-number
--no-line-matching
-l, --history-prompt-length <length>
-b, --batch-sizes <sizes>
Batch sizes of increasing order for translation prompt slices in JSON Array (default: "[10, 100]"
)
The number of lines to include in each translation prompt, provided that they are estimated to within the token limit. In case of mismatched output line quantities, this number will be decreased step-by-step according to the values in the array, ultimately reaching one.
Larger batch sizes generally lead to more efficient token utilization and potentially better contextual translation. However, mismatched output line quantities or exceeding the token limit will cause token wastage, requiring resubmission of the batch with a smaller batch size.
Additional Options for ChatAPT:
-m, --model <model>
"gpt-3.5-turbo"
) https://platform.openai.com/docs/api-reference/chat/create#chat/create-model--stream
-t, --temperature <temperature>
0.3
to be more deterministic for translation (default: 1
) https://platform.openai.com/docs/api-reference/chat/create#chat/create-temperature--top_p <top_p>
--presence_penalty <presence_penalty>
--frequency_penalty <frequency_penalty
--logit_bias <logit_bias>
cli/translator.mjs --plain-text "δ½ ε₯½"
Standard Output
Hello.
cli/translator.mjs --stream --to "Emojis" --temperature 0 --plain-text "$(curl 'https://api.chucknorris.io/jokes/0ECUwLDTTYSaeFCq6YMa5A' | jq .value)"
Input Argument
Chuck Norris can walk with the animals, talk with the animals; grunt and squeak and squawk with the animals... and the animals, without fail, always say 'yessir Mr. Norris'.
Standard Output
π¨βπ¦°πͺπΆββοΈπ¦πππ
ππππππ¦ππ’ππΏοΈππΏοΈβοΈπ³π¬π²ππ€΅π¨βπ¦°π=ππππ¦ππ¦π¦π¦§π¦π
π¦π¦π¦ππ¦ππππ¦=ππ€΅.
cli/translator.mjs --stream --system-instruction "Scramble characters of words while only keeping the start and end letter" --no-prefix-number --no-line-matching --temperature 0 --plain-text "Chuck Norris can walk with the animals, talk with the animals;"
Standard Output
Cuhck Nroris can wakl wtih the aiamnls, talk wtih the aiamnls;
cli/translator.mjs --stream --system-instruction "Unscramble characters back to English" --no-prefix-number --no-line-matching --temperature 0 --plain-text "Cuhck Nroris can wakl wtih the aiamnls, talk wtih the aiamnls;"
Standard Output
Chuck Norris can walk with the animals, talk with the animals;
cli/translator.mjs --stream --temperature 0 --input test/data/test_cn.txt
Input file: test/data/test_cn.txt
δ½ ε₯½γ
ζζοΌ
Standard Output
Hello.
Goodbye!
cli/translator.mjs --stream --temperature 0 --input test/data/test_ja_small.srt
Input file: test/data/test_ja_small.srt
1
00:00:00,000 --> 00:00:02,000
γγ―γγγγγγΎγγ
2
00:00:02,000 --> 00:00:05,000
γε
ζ°γ§γγοΌ
3
00:00:05,000 --> 00:00:07,000
γ―γγε
ζ°γ§γγ
4
00:00:08,000 --> 00:00:12,000
δ»ζ₯γ―倩ζ°γγγγ§γγγ
5
00:00:12,000 --> 00:00:16,000
γ―γγγ¨γ¦γγγ倩ζ°γ§γγ
Output file: test/data/test_ja_small.srt.out_English.srt
1
00:00:00,000 --> 00:00:02,000
Good morning.
2
00:00:02,000 --> 00:00:05,000
How are you?
3
00:00:05,000 --> 00:00:07,000
Yes, I'm doing well.
4
00:00:08,000 --> 00:00:12,000
The weather is nice today, isn't it?
5
00:00:12,000 --> 00:00:16,000
Yes, it's very nice weather.
System Instruction
Tokens: 5
Translate Japanese to English
Input | Prompt | Transform | Output |
---|---|---|---|
Tokens: `164` | Tokens: `83` | Tokens: `46` | Tokens: `130` |
```srt 1 00:00:00,000 --> 00:00:02,000 γγ―γγγγγγΎγγ 2 00:00:02,000 --> 00:00:05,000 γε ζ°γ§γγοΌ 3 00:00:05,000 --> 00:00:07,000 γ―γγε ζ°γ§γγ 4 00:00:08,000 --> 00:00:12,000 δ»ζ₯γ―倩ζ°γγγγ§γγγ 5 00:00:12,000 --> 00:00:16,000 γ―γγγ¨γ¦γγγ倩ζ°γ§γγ ``` | ```log 1. γγ―γγγγγγΎγγ 2. γε ζ°γ§γγοΌ 3. γ―γγε ζ°γ§γγ 4. δ»ζ₯γ―倩ζ°γγγγ§γγγ 5. γ―γγγ¨γ¦γγγ倩ζ°γ§γγ ``` | ```log 1. Good morning. 2. How are you? 3. Yes, I'm doing well. 4. The weather is nice today, isn't it? 5. Yes, it's very nice weather. ``` | ```srt 1 00:00:00,000 --> 00:00:02,000 Good morning. 2 00:00:02,000 --> 00:00:05,000 How are you? 3 00:00:05,000 --> 00:00:07,000 Yes, I'm doing well. 4 00:00:08,000 --> 00:00:12,000 The weather is nice today, isn't it? 5 00:00:12,000 --> 00:00:16,000 Yes, it's very nice weather. ``` |
TODO: More analysis
5 SRT lines:
test/data/test_ja_small.srt
299
362
276
30 SRT lines:
test/data/test_ja.srt
1625
6719
[5, 10]
, including up to 10 historical prompt context:1036