Giter Site home page Giter Site logo

aliceric27 / chatgpt-subtitle-translator Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cerlancism/chatgpt-subtitle-translator

0.0 0.0 0.0 413 KB

Efficient translation tool based on ChatGPT API

Home Page: https://cerlancism.github.io/chatgpt-subtitle-translator/

License: MIT License

JavaScript 99.79% CSS 0.21%

chatgpt-subtitle-translator's Introduction

ChatGPT API SRT Subtitle Translator

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.

Features

  • New work in progress: Web UI
  • Line-based batching: avoiding token limit per request, reducing overhead token wastage, maintaining translation context to certain extent
  • Checking with the free OpenAI Moderation tool: prevent token wastage if the model is highly likely to refuse to translate
  • Streaming process output
  • Request per minute (RPM) rate limits
  • TODO: Tokens per minute rate limits (TPM)
  • Progress resumption (CLI Only) - mitigation for frequent API gateway errors and downtimes
  • TODO: Retry translation parts

Setup

Reference: https://github.com/openai/openai-quickstart-node#setup

  • Node.js version >= 16.13.0 required. This README assumes bash shell environment
  • Clone this repository and navigate into the directory
    git clone https://github.com/Cerlancism/chatgpt-subtitle-translator && cd chatgpt-subtitle-translator
  • Install the requirements
    npm install
  • Give executable permission
    chmod +x cli/translator.mjs
  • Copy .example.env to .env
    cp .env.example .env
  • Add your API key to the newly created .env file

CLI

cli/translator.mjs --help

Usage: translator [options]

Translation tool based on ChatGPT API

Options:

  • -f, --from <language>
    Source language (default: "")

  • -t, --to <language>
    Target language (default: "English")

  • -f, --file <file>
    Input source text with the content of this file, in .srt format or plain text

  • -p, --plain-text <text>
    Input source text with this plain text argument

  • -s, --system-instruction <instruction>
    Override the prompt system instruction template Translate ${from} to ${to} with this plain text, ignoring --from and --to options

  • --initial-prompts <prompts>
    Initial prompts for the translation in JSON (default: "[]")

  • --no-use-moderator
    Don't use the OpenAI API Moderation endpoint

  • --no-prefix-number
    Don't prefix lines with numerical indices

  • --no-line-matching
    Don't enforce one to one line quantity input output matching

  • -l, --history-prompt-length <length>
    Length of prompt history to retain for next request batch (default: 10)

  • -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:

Examples

Plain text

cli/translator.mjs --plain-text "你好"

Standard Output

Hello.

Emojis

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

👨‍🦰💪🚶‍♂️🦜🐒🐘🐅🐆🐎🐖🐄🐑🦏🐊🐢🐍🐿️🐇🐿️❗️🌳💬😲👉🤵👨‍🦰👊=🐕🐑🐐🦌🐘🦏🦍🦧🦓🐅🦌🦌🦌🐆🦍🐘🐘🐗🦓=👍🤵.

Scrambling

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;

Unscrabling

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;

Plain text file

cli/translator.mjs --stream --temperature 0 --file test/data/test_cn.txt

Input file: test/data/test_cn.txt

你好。
拜拜!

Standard Output

Hello.  
Goodbye!

SRT file

cli/translator.mjs --stream --temperature 0 --file 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.

How it works

Token Reductions

System Instruction
Tokens: 5

Translate Japanese to English
Input Prompt Transform Output

Tokens: 164

Tokens: 83

Tokens: 46

Tokens: 130

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
はい、とてもいい天気です。
1. おはようございます。
2. お元気ですか?
3. はい、元気です。
4. 今日は天気がいいですね。
5. はい、とてもいい天気です。
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.
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.

Results

TODO: More analysis

5 SRT lines:
test/data/test_ja_small.srt

  • None (Plain text SRT input output):
    Tokens: 299
  • No batching, with SRT stripping but one line per prompt with System Instruction overhead, including up to 10 historical prompt context:
    Tokens: 362
  • SRT stripping and line batching of 2:
    Tokens: 276

30 SRT lines:
test/data/test_ja.srt

  • None (Plain text SRT input output):
    Tokens: 1625
  • No batching, with SRT stripping but one line per prompt with System Instruction overhead, including up to 10 historical prompt context:
    Tokens: 6719
  • SRT stripping and line batching of [5, 10], including up to 10 historical prompt context:
    Tokens: 1036

chatgpt-subtitle-translator's People

Contributors

cerlancism avatar aliceric27 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.