bbook-maker 0.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (28) hide show
  1. bbook_maker-0.0.0/LICENSE +21 -0
  2. bbook_maker-0.0.0/PKG-INFO +238 -0
  3. bbook_maker-0.0.0/README.md +212 -0
  4. bbook_maker-0.0.0/book_maker/__init__.py +0 -0
  5. bbook_maker-0.0.0/book_maker/__main__.py +4 -0
  6. bbook_maker-0.0.0/book_maker/cli.py +449 -0
  7. bbook_maker-0.0.0/book_maker/loader/__init__.py +10 -0
  8. bbook_maker-0.0.0/book_maker/loader/base_loader.py +27 -0
  9. bbook_maker-0.0.0/book_maker/loader/epub_loader.py +588 -0
  10. bbook_maker-0.0.0/book_maker/loader/helper.py +131 -0
  11. bbook_maker-0.0.0/book_maker/loader/srt_loader.py +296 -0
  12. bbook_maker-0.0.0/book_maker/loader/txt_loader.py +140 -0
  13. bbook_maker-0.0.0/book_maker/obok.py +851 -0
  14. bbook_maker-0.0.0/book_maker/translator/__init__.py +24 -0
  15. bbook_maker-0.0.0/book_maker/translator/base_translator.py +19 -0
  16. bbook_maker-0.0.0/book_maker/translator/caiyun_translator.py +72 -0
  17. bbook_maker-0.0.0/book_maker/translator/chatgptapi_translator.py +340 -0
  18. bbook_maker-0.0.0/book_maker/translator/claude_translator.py +50 -0
  19. bbook_maker-0.0.0/book_maker/translator/custom_api_translator.py +31 -0
  20. bbook_maker-0.0.0/book_maker/translator/deepl_free_translator.py +67 -0
  21. bbook_maker-0.0.0/book_maker/translator/deepl_translator.py +89 -0
  22. bbook_maker-0.0.0/book_maker/translator/gemini_translator.py +91 -0
  23. bbook_maker-0.0.0/book_maker/translator/google_translator.py +59 -0
  24. bbook_maker-0.0.0/book_maker/translator/litellm_translator.py +44 -0
  25. bbook_maker-0.0.0/book_maker/translator/tencent_transmart_translator.py +86 -0
  26. bbook_maker-0.0.0/book_maker/utils.py +164 -0
  27. bbook_maker-0.0.0/pyproject.toml +53 -0
  28. bbook_maker-0.0.0/tests/test_integration.py +338 -0
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 yihong
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,238 @@
1
+ Metadata-Version: 2.1
2
+ Name: bbook-maker
3
+ Version: 0.0.0
4
+ Summary: The bilingual_book_maker is an AI translation tool that uses ChatGPT to assist users in creating multi-language versions of epub/txt files and books.
5
+ Author-Email: yihong0618 <zouzou0208@gmail.com>
6
+ License: MIT
7
+ Classifier: License :: OSI Approved :: MIT License
8
+ Classifier: Operating System :: OS Independent
9
+ Classifier: Programming Language :: Python :: 3
10
+ Project-URL: Homepage, https://github.com/yihong0618/bilingual_book_maker
11
+ Requires-Python: >=3.9
12
+ Requires-Dist: anthropic
13
+ Requires-Dist: backoff
14
+ Requires-Dist: bs4
15
+ Requires-Dist: ebooklib
16
+ Requires-Dist: google-generativeai
17
+ Requires-Dist: langdetect
18
+ Requires-Dist: litellm
19
+ Requires-Dist: openai>=1.1.1
20
+ Requires-Dist: PyDeepLX
21
+ Requires-Dist: requests
22
+ Requires-Dist: rich
23
+ Requires-Dist: tiktoken
24
+ Requires-Dist: tqdm
25
+ Description-Content-Type: text/markdown
26
+
27
+ **[中文](./README-CN.md) | English**
28
+ [![litellm](https://img.shields.io/badge/%20%F0%9F%9A%85%20liteLLM-OpenAI%7CAzure%7CAnthropic%7CPalm%7CCohere%7CReplicate%7CHugging%20Face-blue?color=green)](https://github.com/BerriAI/litellm)
29
+
30
+ # bilingual_book_maker
31
+ 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)**.
32
+
33
+ ![image](https://user-images.githubusercontent.com/15976103/222317531-a05317c5-4eee-49de-95cd-04063d9539d9.png)
34
+
35
+ ## Supported Models
36
+ gpt-4, gpt-3.5-turbo, claude-2, palm, llama-2, azure-openai, command-nightly, gemini
37
+ For using Non-OpenAI models, use class `liteLLM()` - liteLLM supports all models above.
38
+ Find more info here for using liteLLM: https://github.com/BerriAI/litellm/blob/main/setup.py
39
+
40
+ ## Preparation
41
+
42
+ 1. ChatGPT or OpenAI token [^token]
43
+ 2. epub/txt books
44
+ 3. Environment with internet access or proxy
45
+ 4. Python 3.8+
46
+
47
+ ## Use
48
+
49
+ - `pip install -r requirements.txt` or `pip install -U bbook_maker`(you can use)
50
+ - 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.
51
+ Or, just set environment variable `BBM_OPENAI_API_KEY` instead.
52
+ - A sample book, `test_books/animal_farm.epub`, is provided for testing purposes.
53
+ - 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`.
54
+ - 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`.
55
+ - 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.
56
+ - If using `GPT4`, you can add `--use_context` to add a context paragraph to each passage sent to the model for translation (see below).- support DeepL model [DeepL Translator](https://rapidapi.com/splintPRO/api/dpl-translator) need pay to get the token use `--model deepl --deepl_key ${deepl_key}`
57
+ - support DeepL free model `--model deeplfree`
58
+ - support Google [Gemini](https://makersuite.google.com/app/apikey) model `--model gemini --gemini_key ${gemini_key}`
59
+ - Support [Claude](https://console.anthropic.com/docs) model, use `--model claude --claude_key ${claude_key}`
60
+ - Support [Tencent TranSmart](https://transmart.qq.com) model (Free), use `--model tencentransmart`
61
+ - 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.
62
+ - Set the target language like `--language "Simplified Chinese"`. Default target language is `"Simplified Chinese"`.
63
+ Read available languages by helper message: `python make_book.py --help`
64
+ - Use `--proxy` option to specify proxy server for internet access. Enter a string such as `http://127.0.0.1:7890`.
65
+ - Use `--resume` option to manually resume the process after an interruption.
66
+ - epub is made of html files. By default, we only translate contents in `<p>`.
67
+ Use `--translate-tags` to specify tags need for translation. Use comma to separate multiple tags. For example:
68
+ `--translate-tags h1,h2,h3,p,div`
69
+ - Use `--book_from` option to specify e-reader type (Now only `kobo` is available), and use `--device_path` to specify the mounting point.
70
+ - If you want to change api_base like using Cloudflare Workers, use `--api_base <URL>` to support it.
71
+ **Note: the api url should be '`https://xxxx/v1`'. Quotation marks are required.**
72
+ - Once the translation is complete, a bilingual book named `${book_name}_bilingual.epub` would be generated.
73
+ - 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.
74
+ - If you want to translate strings in an e-book that aren't labeled with any tags, you can use the `--allow_navigable_strings` parameter. This will add the strings to the translation queue. **Note that it's best to look for e-books that are more standardized if possible.**
75
+ - To tweak the prompt, use the `--prompt` parameter. Valid placeholders for the `user` role template include `{text}` and `{language}`. It supports a few ways to configure the prompt:
76
+ If you don't need to set the `system` role content, you can simply set it up like this: `--prompt "Translate {text} to {language}."` or `--prompt prompt_template_sample.txt` (example of a text file can be found at [./prompt_template_sample.txt](./prompt_template_sample.txt)).
77
+ If you need to set the `system` role content, you can use the following format: `--prompt '{"user":"Translate {text} to {language}", "system": "You are a professional translator."}'` or `--prompt prompt_template_sample.json` (example of a JSON file can be found at [./prompt_template_sample.json](./prompt_template_sample.json)).
78
+ You can also set the `user` and `system` role prompt by setting environment variables: `BBM_CHATGPTAPI_USER_MSG_TEMPLATE` and `BBM_CHATGPTAPI_SYS_MSG`.
79
+ - Use the `--batch_size` parameter to specify the number of lines for batch translation (default is 10, currently only effective for txt files).
80
+ - `--accumulated_num` Wait for how many tokens have been accumulated before starting the translation. gpt3.5 limits the total_token to 4090. For example, if you use --accumulated_num 1600, maybe openai will
81
+ output 2200 tokens and maybe 200 tokens for other messages in the system messages user messages, 1600+2200+200=4000, So you are close to reaching the limit. You have to choose your own
82
+ value, there is no way to know if the limit is reached before sending
83
+ - `--use_context` prompts the GPT4 model to create a one-paragraph summary. If it's the beginning of the translation, it will summarize the entire passage sent (the size depending on `--accumulated_num`), but if it's any proceeding passage, it will amend the summary to include details from the most recent passage, creating a running one-paragraph context payload of the important details of the entire translated work, which improves consistency of flow and tone of each translation.
84
+ - `--translation_style` example: `--translation_style "color: #808080; font-style: italic;"`
85
+ - `--retranslate` `--retranslate "$translated_filepath" "file_name_in_epub" "start_str" "end_str"(optional)`<br>
86
+ Retranslate from start_str to end_str's tag:
87
+ `python3 "make_book.py" --book_name "test_books/animal_farm.epub" --retranslate 'test_books/animal_farm_bilingual.epub' 'index_split_002.html' 'in spite of the present book shortage which' 'This kind of thing is not a good symptom. Obviously'`<br>
88
+ Retranslate start_str's tag:
89
+ `python3 "make_book.py" --book_name "test_books/animal_farm.epub" --retranslate 'test_books/animal_farm_bilingual.epub' 'index_split_002.html' 'in spite of the present book shortage which'`
90
+ ### Examples
91
+
92
+ **Note if use `pip install bbook_maker` all commands can change to `bbook_maker args`**
93
+
94
+ ```shell
95
+ # Test quickly
96
+ python3 make_book.py --book_name test_books/animal_farm.epub --openai_key ${openai_key} --test --language zh-hans
97
+
98
+ # Test quickly for src
99
+ python3 make_book.py --book_name test_books/Lex_Fridman_episode_322.srt --openai_key ${openai_key} --test
100
+
101
+ # Or translate the whole book
102
+ python3 make_book.py --book_name test_books/animal_farm.epub --openai_key ${openai_key} --language zh-hans
103
+
104
+ # Or translate the whole book using Gemini
105
+ python3 make_book.py --book_name test_books/animal_farm.epub --gemini_key ${gemini_key} --model gemini
106
+
107
+ # Set env OPENAI_API_KEY to ignore option --openai_key
108
+ export OPENAI_API_KEY=${your_api_key}
109
+
110
+ # Use the GPT-4 model with context to Japanese
111
+ python3 make_book.py --book_name test_books/animal_farm.epub --model gpt4 --use_context --language ja
112
+
113
+ # Use a specific OpenAI model alias
114
+ python3 make_book.py --book_name test_books/animal_farm.epub --model openai --model_list gpt-4-1106-preview --openai_key ${openai_key}
115
+
116
+ **Note** you can use other `openai like` model in this way
117
+ python3 make_book.py --book_name test_books/animal_farm.epub --model openai --model_list yi-34b-chat-0205 --openai_key ${openai_key} --api_base "https://api.lingyiwanwu.com/v1"
118
+
119
+ # Use a specific list of OpenAI model aliases
120
+ python3 make_book.py --book_name test_books/animal_farm.epub --model openai --model_list gpt-4-1106-preview,gpt-4-0125-preview,gpt-3.5-turbo-0125 --openai_key ${openai_key}
121
+
122
+ # Use the DeepL model with Japanese
123
+ python3 make_book.py --book_name test_books/animal_farm.epub --model deepl --deepl_key ${deepl_key} --language ja
124
+
125
+ # Use the Claude model with Japanese
126
+ python3 make_book.py --book_name test_books/animal_farm.epub --model claude --claude_key ${claude_key} --language ja
127
+
128
+ # Use the CustomAPI model with Japanese
129
+ python3 make_book.py --book_name test_books/animal_farm.epub --model customapi --custom_api ${custom_api} --language ja
130
+
131
+ # Translate contents in <div> and <p>
132
+ python3 make_book.py --book_name test_books/animal_farm.epub --translate-tags div,p
133
+
134
+ # Tweaking the prompt
135
+ python3 make_book.py --book_name test_books/animal_farm.epub --prompt prompt_template_sample.txt
136
+ # or
137
+ python3 make_book.py --book_name test_books/animal_farm.epub --prompt prompt_template_sample.json
138
+ # or
139
+ python3 make_book.py --book_name test_books/animal_farm.epub --prompt "Please translate \`{text}\` to {language}"
140
+
141
+ # Translate books download from Rakuten Kobo on kobo e-reader
142
+ python3 make_book.py --book_from kobo --device_path /tmp/kobo
143
+
144
+ # translate txt file
145
+ python3 make_book.py --book_name test_books/the_little_prince.txt --test --language zh-hans
146
+ # aggregated translation txt file
147
+ python3 make_book.py --book_name test_books/the_little_prince.txt --test --batch_size 20
148
+
149
+ # Using Caiyun model to translate
150
+ # (the api currently only support: simplified chinese <-> english, simplified chinese <-> japanese)
151
+ # the official Caiyun has provided a test token (3975l6lr5pcbvidl6jl2)
152
+ # you can apply your own token by following this tutorial(https://bobtranslate.com/service/translate/caiyun.html)
153
+ python3 make_book.py --model caiyun --caiyun_key 3975l6lr5pcbvidl6jl2 --book_name test_books/animal_farm.epub
154
+
155
+
156
+ # Set env BBM_CAIYUN_API_KEY to ignore option --openai_key
157
+ export BBM_CAIYUN_API_KEY=${your_api_key}
158
+
159
+ ```
160
+
161
+ More understandable example
162
+ ```shell
163
+ python3 make_book.py --book_name 'animal_farm.epub' --openai_key sk-XXXXX --api_base 'https://xxxxx/v1'
164
+
165
+ # Or python3 is not in your PATH
166
+ python make_book.py --book_name 'animal_farm.epub' --openai_key sk-XXXXX --api_base 'https://xxxxx/v1'
167
+ ```
168
+
169
+ Microsoft Azure Endpoints
170
+ ```shell
171
+ python3 make_book.py --book_name 'animal_farm.epub' --openai_key XXXXX --api_base 'https://example-endpoint.openai.azure.com' --deployment_id 'deployment-name'
172
+
173
+ # Or python3 is not in your PATH
174
+ python make_book.py --book_name 'animal_farm.epub' --openai_key XXXXX --api_base 'https://example-endpoint.openai.azure.com' --deployment_id 'deployment-name'
175
+ ```
176
+
177
+ ## Docker
178
+
179
+ You can use [Docker](https://www.docker.com/) if you don't want to deal with setting up the environment.
180
+
181
+ ```shell
182
+ # Build image
183
+ docker build --tag bilingual_book_maker .
184
+
185
+ # Run container
186
+ # "$folder_path" represents the folder where your book file locates. Also, it is where the processed file will be stored.
187
+
188
+ # Windows PowerShell
189
+ $folder_path=your_folder_path # $folder_path="C:\Users\user\mybook\"
190
+ $book_name=your_book_name # $book_name="animal_farm.epub"
191
+ $openai_key=your_api_key # $openai_key="sk-xxx"
192
+ $language=your_language # see utils.py
193
+
194
+ 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
195
+
196
+ # Linux
197
+ export folder_path=${your_folder_path}
198
+ export book_name=${your_book_name}
199
+ export openai_key=${your_api_key}
200
+ export language=${your_language}
201
+
202
+ 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}"
203
+ ```
204
+
205
+ For example:
206
+
207
+ ```shell
208
+ # Linux
209
+ 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
210
+ ```
211
+
212
+ ## Notes
213
+
214
+ 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
215
+ 2. PR is welcome
216
+
217
+ # Thanks
218
+
219
+ - @[yetone](https://github.com/yetone)
220
+
221
+ # Contribution
222
+
223
+ - Any issues or PRs are welcome.
224
+ - TODOs in the issue can also be selected.
225
+ - Please run `black make_book.py`[^black] before submitting the code.
226
+
227
+ # Others better
228
+
229
+ - 书译 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)
230
+
231
+ ## Appreciation
232
+
233
+ Thank you, that's enough.
234
+
235
+ ![image](https://user-images.githubusercontent.com/15976103/222407199-1ed8930c-13a8-402b-9993-aaac8ee84744.png)
236
+
237
+ [^token]: https://platform.openai.com/account/api-keys
238
+ [^black]: https://github.com/psf/black
@@ -0,0 +1,212 @@
1
+ **[中文](./README-CN.md) | English**
2
+ [![litellm](https://img.shields.io/badge/%20%F0%9F%9A%85%20liteLLM-OpenAI%7CAzure%7CAnthropic%7CPalm%7CCohere%7CReplicate%7CHugging%20Face-blue?color=green)](https://github.com/BerriAI/litellm)
3
+
4
+ # bilingual_book_maker
5
+ 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)**.
6
+
7
+ ![image](https://user-images.githubusercontent.com/15976103/222317531-a05317c5-4eee-49de-95cd-04063d9539d9.png)
8
+
9
+ ## Supported Models
10
+ gpt-4, gpt-3.5-turbo, claude-2, palm, llama-2, azure-openai, command-nightly, gemini
11
+ For using Non-OpenAI models, use class `liteLLM()` - liteLLM supports all models above.
12
+ Find more info here for using liteLLM: https://github.com/BerriAI/litellm/blob/main/setup.py
13
+
14
+ ## Preparation
15
+
16
+ 1. ChatGPT or OpenAI token [^token]
17
+ 2. epub/txt books
18
+ 3. Environment with internet access or proxy
19
+ 4. Python 3.8+
20
+
21
+ ## Use
22
+
23
+ - `pip install -r requirements.txt` or `pip install -U bbook_maker`(you can use)
24
+ - 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.
25
+ Or, just set environment variable `BBM_OPENAI_API_KEY` instead.
26
+ - A sample book, `test_books/animal_farm.epub`, is provided for testing purposes.
27
+ - 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`.
28
+ - 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`.
29
+ - 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.
30
+ - If using `GPT4`, you can add `--use_context` to add a context paragraph to each passage sent to the model for translation (see below).- support DeepL model [DeepL Translator](https://rapidapi.com/splintPRO/api/dpl-translator) need pay to get the token use `--model deepl --deepl_key ${deepl_key}`
31
+ - support DeepL free model `--model deeplfree`
32
+ - support Google [Gemini](https://makersuite.google.com/app/apikey) model `--model gemini --gemini_key ${gemini_key}`
33
+ - Support [Claude](https://console.anthropic.com/docs) model, use `--model claude --claude_key ${claude_key}`
34
+ - Support [Tencent TranSmart](https://transmart.qq.com) model (Free), use `--model tencentransmart`
35
+ - 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.
36
+ - Set the target language like `--language "Simplified Chinese"`. Default target language is `"Simplified Chinese"`.
37
+ Read available languages by helper message: `python make_book.py --help`
38
+ - Use `--proxy` option to specify proxy server for internet access. Enter a string such as `http://127.0.0.1:7890`.
39
+ - Use `--resume` option to manually resume the process after an interruption.
40
+ - epub is made of html files. By default, we only translate contents in `<p>`.
41
+ Use `--translate-tags` to specify tags need for translation. Use comma to separate multiple tags. For example:
42
+ `--translate-tags h1,h2,h3,p,div`
43
+ - Use `--book_from` option to specify e-reader type (Now only `kobo` is available), and use `--device_path` to specify the mounting point.
44
+ - If you want to change api_base like using Cloudflare Workers, use `--api_base <URL>` to support it.
45
+ **Note: the api url should be '`https://xxxx/v1`'. Quotation marks are required.**
46
+ - Once the translation is complete, a bilingual book named `${book_name}_bilingual.epub` would be generated.
47
+ - 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.
48
+ - If you want to translate strings in an e-book that aren't labeled with any tags, you can use the `--allow_navigable_strings` parameter. This will add the strings to the translation queue. **Note that it's best to look for e-books that are more standardized if possible.**
49
+ - To tweak the prompt, use the `--prompt` parameter. Valid placeholders for the `user` role template include `{text}` and `{language}`. It supports a few ways to configure the prompt:
50
+ If you don't need to set the `system` role content, you can simply set it up like this: `--prompt "Translate {text} to {language}."` or `--prompt prompt_template_sample.txt` (example of a text file can be found at [./prompt_template_sample.txt](./prompt_template_sample.txt)).
51
+ If you need to set the `system` role content, you can use the following format: `--prompt '{"user":"Translate {text} to {language}", "system": "You are a professional translator."}'` or `--prompt prompt_template_sample.json` (example of a JSON file can be found at [./prompt_template_sample.json](./prompt_template_sample.json)).
52
+ You can also set the `user` and `system` role prompt by setting environment variables: `BBM_CHATGPTAPI_USER_MSG_TEMPLATE` and `BBM_CHATGPTAPI_SYS_MSG`.
53
+ - Use the `--batch_size` parameter to specify the number of lines for batch translation (default is 10, currently only effective for txt files).
54
+ - `--accumulated_num` Wait for how many tokens have been accumulated before starting the translation. gpt3.5 limits the total_token to 4090. For example, if you use --accumulated_num 1600, maybe openai will
55
+ output 2200 tokens and maybe 200 tokens for other messages in the system messages user messages, 1600+2200+200=4000, So you are close to reaching the limit. You have to choose your own
56
+ value, there is no way to know if the limit is reached before sending
57
+ - `--use_context` prompts the GPT4 model to create a one-paragraph summary. If it's the beginning of the translation, it will summarize the entire passage sent (the size depending on `--accumulated_num`), but if it's any proceeding passage, it will amend the summary to include details from the most recent passage, creating a running one-paragraph context payload of the important details of the entire translated work, which improves consistency of flow and tone of each translation.
58
+ - `--translation_style` example: `--translation_style "color: #808080; font-style: italic;"`
59
+ - `--retranslate` `--retranslate "$translated_filepath" "file_name_in_epub" "start_str" "end_str"(optional)`<br>
60
+ Retranslate from start_str to end_str's tag:
61
+ `python3 "make_book.py" --book_name "test_books/animal_farm.epub" --retranslate 'test_books/animal_farm_bilingual.epub' 'index_split_002.html' 'in spite of the present book shortage which' 'This kind of thing is not a good symptom. Obviously'`<br>
62
+ Retranslate start_str's tag:
63
+ `python3 "make_book.py" --book_name "test_books/animal_farm.epub" --retranslate 'test_books/animal_farm_bilingual.epub' 'index_split_002.html' 'in spite of the present book shortage which'`
64
+ ### Examples
65
+
66
+ **Note if use `pip install bbook_maker` all commands can change to `bbook_maker args`**
67
+
68
+ ```shell
69
+ # Test quickly
70
+ python3 make_book.py --book_name test_books/animal_farm.epub --openai_key ${openai_key} --test --language zh-hans
71
+
72
+ # Test quickly for src
73
+ python3 make_book.py --book_name test_books/Lex_Fridman_episode_322.srt --openai_key ${openai_key} --test
74
+
75
+ # Or translate the whole book
76
+ python3 make_book.py --book_name test_books/animal_farm.epub --openai_key ${openai_key} --language zh-hans
77
+
78
+ # Or translate the whole book using Gemini
79
+ python3 make_book.py --book_name test_books/animal_farm.epub --gemini_key ${gemini_key} --model gemini
80
+
81
+ # Set env OPENAI_API_KEY to ignore option --openai_key
82
+ export OPENAI_API_KEY=${your_api_key}
83
+
84
+ # Use the GPT-4 model with context to Japanese
85
+ python3 make_book.py --book_name test_books/animal_farm.epub --model gpt4 --use_context --language ja
86
+
87
+ # Use a specific OpenAI model alias
88
+ python3 make_book.py --book_name test_books/animal_farm.epub --model openai --model_list gpt-4-1106-preview --openai_key ${openai_key}
89
+
90
+ **Note** you can use other `openai like` model in this way
91
+ python3 make_book.py --book_name test_books/animal_farm.epub --model openai --model_list yi-34b-chat-0205 --openai_key ${openai_key} --api_base "https://api.lingyiwanwu.com/v1"
92
+
93
+ # Use a specific list of OpenAI model aliases
94
+ python3 make_book.py --book_name test_books/animal_farm.epub --model openai --model_list gpt-4-1106-preview,gpt-4-0125-preview,gpt-3.5-turbo-0125 --openai_key ${openai_key}
95
+
96
+ # Use the DeepL model with Japanese
97
+ python3 make_book.py --book_name test_books/animal_farm.epub --model deepl --deepl_key ${deepl_key} --language ja
98
+
99
+ # Use the Claude model with Japanese
100
+ python3 make_book.py --book_name test_books/animal_farm.epub --model claude --claude_key ${claude_key} --language ja
101
+
102
+ # Use the CustomAPI model with Japanese
103
+ python3 make_book.py --book_name test_books/animal_farm.epub --model customapi --custom_api ${custom_api} --language ja
104
+
105
+ # Translate contents in <div> and <p>
106
+ python3 make_book.py --book_name test_books/animal_farm.epub --translate-tags div,p
107
+
108
+ # Tweaking the prompt
109
+ python3 make_book.py --book_name test_books/animal_farm.epub --prompt prompt_template_sample.txt
110
+ # or
111
+ python3 make_book.py --book_name test_books/animal_farm.epub --prompt prompt_template_sample.json
112
+ # or
113
+ python3 make_book.py --book_name test_books/animal_farm.epub --prompt "Please translate \`{text}\` to {language}"
114
+
115
+ # Translate books download from Rakuten Kobo on kobo e-reader
116
+ python3 make_book.py --book_from kobo --device_path /tmp/kobo
117
+
118
+ # translate txt file
119
+ python3 make_book.py --book_name test_books/the_little_prince.txt --test --language zh-hans
120
+ # aggregated translation txt file
121
+ python3 make_book.py --book_name test_books/the_little_prince.txt --test --batch_size 20
122
+
123
+ # Using Caiyun model to translate
124
+ # (the api currently only support: simplified chinese <-> english, simplified chinese <-> japanese)
125
+ # the official Caiyun has provided a test token (3975l6lr5pcbvidl6jl2)
126
+ # you can apply your own token by following this tutorial(https://bobtranslate.com/service/translate/caiyun.html)
127
+ python3 make_book.py --model caiyun --caiyun_key 3975l6lr5pcbvidl6jl2 --book_name test_books/animal_farm.epub
128
+
129
+
130
+ # Set env BBM_CAIYUN_API_KEY to ignore option --openai_key
131
+ export BBM_CAIYUN_API_KEY=${your_api_key}
132
+
133
+ ```
134
+
135
+ More understandable example
136
+ ```shell
137
+ python3 make_book.py --book_name 'animal_farm.epub' --openai_key sk-XXXXX --api_base 'https://xxxxx/v1'
138
+
139
+ # Or python3 is not in your PATH
140
+ python make_book.py --book_name 'animal_farm.epub' --openai_key sk-XXXXX --api_base 'https://xxxxx/v1'
141
+ ```
142
+
143
+ Microsoft Azure Endpoints
144
+ ```shell
145
+ python3 make_book.py --book_name 'animal_farm.epub' --openai_key XXXXX --api_base 'https://example-endpoint.openai.azure.com' --deployment_id 'deployment-name'
146
+
147
+ # Or python3 is not in your PATH
148
+ python make_book.py --book_name 'animal_farm.epub' --openai_key XXXXX --api_base 'https://example-endpoint.openai.azure.com' --deployment_id 'deployment-name'
149
+ ```
150
+
151
+ ## Docker
152
+
153
+ You can use [Docker](https://www.docker.com/) if you don't want to deal with setting up the environment.
154
+
155
+ ```shell
156
+ # Build image
157
+ docker build --tag bilingual_book_maker .
158
+
159
+ # Run container
160
+ # "$folder_path" represents the folder where your book file locates. Also, it is where the processed file will be stored.
161
+
162
+ # Windows PowerShell
163
+ $folder_path=your_folder_path # $folder_path="C:\Users\user\mybook\"
164
+ $book_name=your_book_name # $book_name="animal_farm.epub"
165
+ $openai_key=your_api_key # $openai_key="sk-xxx"
166
+ $language=your_language # see utils.py
167
+
168
+ 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
169
+
170
+ # Linux
171
+ export folder_path=${your_folder_path}
172
+ export book_name=${your_book_name}
173
+ export openai_key=${your_api_key}
174
+ export language=${your_language}
175
+
176
+ 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}"
177
+ ```
178
+
179
+ For example:
180
+
181
+ ```shell
182
+ # Linux
183
+ 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
184
+ ```
185
+
186
+ ## Notes
187
+
188
+ 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
189
+ 2. PR is welcome
190
+
191
+ # Thanks
192
+
193
+ - @[yetone](https://github.com/yetone)
194
+
195
+ # Contribution
196
+
197
+ - Any issues or PRs are welcome.
198
+ - TODOs in the issue can also be selected.
199
+ - Please run `black make_book.py`[^black] before submitting the code.
200
+
201
+ # Others better
202
+
203
+ - 书译 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)
204
+
205
+ ## Appreciation
206
+
207
+ Thank you, that's enough.
208
+
209
+ ![image](https://user-images.githubusercontent.com/15976103/222407199-1ed8930c-13a8-402b-9993-aaac8ee84744.png)
210
+
211
+ [^token]: https://platform.openai.com/account/api-keys
212
+ [^black]: https://github.com/psf/black
File without changes
@@ -0,0 +1,4 @@
1
+ from cli import main
2
+
3
+ if __name__ == "__main__":
4
+ main()