**[中文](./README-CN.md) | English** [](https://github.com/BerriAI/litellm) # bilingual_book_maker The bilingual_book_maker is an AI translation tool that uses ChatGPT to assist users in creating multi-language versions of epub/txt/srt files and books. This tool is exclusively designed for translating epub books that have entered the public domain and is not intended for copyrighted works. Before using this tool, please review the project's **[disclaimer](./disclaimer.md)**.  ## Supported Models gpt-4, gpt-3.5-turbo, claude-2, palm, llama-2, azure-openai, command-nightly, gemini For using Non-OpenAI models, use class `liteLLM()` - liteLLM supports all models above. Find more info here for using liteLLM: https://github.com/BerriAI/litellm/blob/main/setup.py ## Preparation 1. ChatGPT or OpenAI token [^token] 2. epub/txt books 3. Environment with internet access or proxy 4. Python 3.8+ ## Quick Start A sample book, `test_books/animal_farm.epub`, is provided for testing purposes. ```shell pip install -r requirements.txt python3 make_book.py --book_name test_books/animal_farm.epub --openai_key ${openai_key} --test OR pip install -U bbook_maker bbook --book_name test_books/animal_farm.epub --openai_key ${openai_key} --test ``` ## Translate Service - Use `--openai_key` option to specify OpenAI API key. If you have multiple keys, separate them by commas (xxx,xxx,xxx) to reduce errors caused by API call limits. Or, just set environment variable `BBM_OPENAI_API_KEY` instead. - A sample book, `test_books/animal_farm.epub`, is provided for testing purposes. - The default underlying model is [GPT-3.5-turbo](https://openai.com/blog/introducing-chatgpt-and-whisper-apis), which is used by ChatGPT currently. Use `--model gpt4` to change the underlying model to `GPT4`. You can also use `GPT4omini`. - Important to note that `gpt-4` is significantly more expensive than `gpt-4-turbo`, but to avoid bumping into rate limits, we automatically balance queries across `gpt-4-1106-preview`, `gpt-4`, `gpt-4-32k`, `gpt-4-0613`,`gpt-4-32k-0613`. - If you want to use a specific model alias with OpenAI (eg `gpt-4-1106-preview` or `gpt-3.5-turbo-0125`), you can use `--model openai --model_list gpt-4-1106-preview,gpt-3.5-turbo-0125`. `--model_list` takes a comma-separated list of model aliases. - If using chatgptapi, you can add `--use_context` to add a context paragraph to each passage sent to the model for translation (see below). * DeepL Support DeepL model [DeepL Translator](https://rapidapi.com/splintPRO/api/dpl-translator) need pay to get the token ``` python3 make_book.py --book_name test_books/animal_farm.epub --model deepl --deepl_key ${deepl_key} ``` * DeepL free ```shell python3 make_book.py --book_name test_books/animal_farm.epub --model deeplfree ``` * [Claude](https://console.anthropic.com/docs) Use [Claude](https://console.anthropic.com/docs) model to translate ```shell python3 make_book.py --book_name test_books/animal_farm.epub --model claude --claude_key ${claude_key} ``` * Google Translate ```shell python3 make_book.py --book_name test_books/animal_farm.epub --model google ``` * Caiyun Translate ```shell python3 make_book.py --book_name test_books/animal_farm.epub --model caiyun --caiyun_key ${caiyun_key} ``` * Gemini Support Google [Gemini](https://aistudio.google.com/app/apikey) model, use `--model gemini` for Gemini Flash or `--model geminipro` for Gemini Pro. If you want to use a specific model alias with Gemini (eg `gemini-1.5-flash-002` or `gemini-1.5-flash-8b-exp-0924`), you can use `--model gemini --model_list gemini-1.5-flash-002,gemini-1.5-flash-8b-exp-0924`. `--model_list` takes a comma-separated list of model aliases. ```shell python3 make_book.py --book_name test_books/animal_farm.epub --model gemini --gemini_key ${gemini_key} ``` * [Tencent TranSmart](https://transmart.qq.com) ```shell python3 make_book.py --book_name test_books/animal_farm.epub --model tencentransmart ``` * [xAI](https://x.ai) ```shell python3 make_book.py --book_name test_books/animal_farm.epub --model xai --xai_key ${xai_key} ``` * [Ollama](https://github.com/ollama/ollama) Support [Ollama](https://github.com/ollama/ollama) self-host models, If ollama server is not running on localhost, use `--api_base http://x.x.x.x:port/v1` to point to the ollama server address ```shell python3 make_book.py --book_name test_books/animal_farm.epub --ollama_model ${ollama_model_name} ``` * [groq](https://console.groq.com/keys) ```shell python3 make_book.py --book_name test_books/animal_farm.epub --groq_key [your_key] --model groq --model_list llama3-8b-8192 ``` ## Use - Once the translation is complete, a bilingual book named `${book_name}_bilingual.epub` would be generated. - If there are any errors or you wish to interrupt the translation by pressing `CTRL+C`. A book named `{book_name}_bilingual_temp.epub` would be generated. You can simply rename it to any desired name. ## Params - `--test`: Use `--test` option to preview the result if you haven't paid for the service. Note that there is a limit and it may take some time. - `--language`: Set the target language like `--language "Simplified Chinese"`. Default target language is `"Simplified Chinese"`. Read available languages by helper message: `python make_book.py --help` - `--proxy`: Use `--proxy` option to specify proxy server for internet access. Enter a string such as `http://127.0.0.1:7890`. - `--resume`: Use `--resume` option to manually resume the process after an interruption. ```shell python3 make_book.py --book_name test_books/animal_farm.epub --model google --resume ``` - `--translate-tags`: epub is made of html files. By default, we only translate contents in `
`.
Use `--translate-tags` to specify tags need for translation. Use comma to separate multiple tags.
For example: `--translate-tags h1,h2,h3,p,div`
- `--book_from`:
Use `--book_from` option to specify e-reader type (Now only `kobo` is available), and use `--device_path` to specify the mounting point.
- `--api_base`:
If you want to change api_base like using Cloudflare Workers, use `--api_base
python3 make_book.py --book_name test_books/animal_farm.epub --translate-tags div,p
# Tweaking the prompt
python3 make_book.py --book_name test_books/animal_farm.epub --prompt prompt_template_sample.txt
# or
python3 make_book.py --book_name test_books/animal_farm.epub --prompt prompt_template_sample.json
# or
python3 make_book.py --book_name test_books/animal_farm.epub --prompt "Please translate \`{text}\` to {language}"
# Translate books download from Rakuten Kobo on kobo e-reader
python3 make_book.py --book_from kobo --device_path /tmp/kobo
# translate txt file
python3 make_book.py --book_name test_books/the_little_prince.txt --test --language zh-hans
# aggregated translation txt file
python3 make_book.py --book_name test_books/the_little_prince.txt --test --batch_size 20
# Using Caiyun model to translate
# (the api currently only support: simplified chinese <-> english, simplified chinese <-> japanese)
# the official Caiyun has provided a test token (3975l6lr5pcbvidl6jl2)
# you can apply your own token by following this tutorial(https://bobtranslate.com/service/translate/caiyun.html)
python3 make_book.py --model caiyun --caiyun_key 3975l6lr5pcbvidl6jl2 --book_name test_books/animal_farm.epub
# Set env BBM_CAIYUN_API_KEY to ignore option --openai_key
export BBM_CAIYUN_API_KEY=${your_api_key}
```
More understandable example
```shell
python3 make_book.py --book_name 'animal_farm.epub' --openai_key sk-XXXXX --api_base 'https://xxxxx/v1'
# Or python3 is not in your PATH
python make_book.py --book_name 'animal_farm.epub' --openai_key sk-XXXXX --api_base 'https://xxxxx/v1'
```
Microsoft Azure Endpoints
```shell
python3 make_book.py --book_name 'animal_farm.epub' --openai_key XXXXX --api_base 'https://example-endpoint.openai.azure.com' --deployment_id 'deployment-name'
# Or python3 is not in your PATH
python make_book.py --book_name 'animal_farm.epub' --openai_key XXXXX --api_base 'https://example-endpoint.openai.azure.com' --deployment_id 'deployment-name'
```
## Docker
You can use [Docker](https://www.docker.com/) if you don't want to deal with setting up the environment.
```shell
# Build image
docker build --tag bilingual_book_maker .
# Run container
# "$folder_path" represents the folder where your book file locates. Also, it is where the processed file will be stored.
# Windows PowerShell
$folder_path=your_folder_path # $folder_path="C:\Users\user\mybook\"
$book_name=your_book_name # $book_name="animal_farm.epub"
$openai_key=your_api_key # $openai_key="sk-xxx"
$language=your_language # see utils.py
docker run --rm --name bilingual_book_maker --mount type=bind,source=$folder_path,target='/app/test_books' bilingual_book_maker --book_name "/app/test_books/$book_name" --openai_key $openai_key --language $language
# Linux
export folder_path=${your_folder_path}
export book_name=${your_book_name}
export openai_key=${your_api_key}
export language=${your_language}
docker run --rm --name bilingual_book_maker --mount type=bind,source=${folder_path},target='/app/test_books' bilingual_book_maker --book_name "/app/test_books/${book_name}" --openai_key ${openai_key} --language "${language}"
```
For example:
```shell
# Linux
docker run --rm --name bilingual_book_maker --mount type=bind,source=/home/user/my_books,target='/app/test_books' bilingual_book_maker --book_name /app/test_books/animal_farm.epub --openai_key sk-XXX --test --test_num 1 --language zh-hant
```
## Notes
1. API token from free trial has limit. If you want to speed up the process, consider paying for the service or use multiple OpenAI tokens
2. PR is welcome
# Thanks
- @[yetone](https://github.com/yetone)
# Contribution
- Any issues or PRs are welcome.
- TODOs in the issue can also be selected.
- Please run `black make_book.py`[^black] before submitting the code.
# Others better
- 书译 iOS -> [AI 全书翻译工具](https://apps.apple.com/cn/app/%E4%B9%A6%E8%AF%91-ai-%E5%85%A8%E4%B9%A6%E7%BF%BB%E8%AF%91%E5%B7%A5%E5%85%B7/id6447665417)
## Appreciation
Thank you, that's enough.

[^token]: https://platform.openai.com/account/api-keys
[^black]: https://github.com/psf/black