Skip to content

Commit

Permalink
Add options for books on kobo e-reader (yihong0618#138)
Browse files Browse the repository at this point in the history
Add obok.py as file selector for kobo device

---------

Co-authored-by: Hsieh Chin Fan <pham@topo.tw>
  • Loading branch information
typebrook and Hsieh Chin Fan authored Mar 11, 2023
1 parent c0b3e0c commit 2384fb3
Show file tree
Hide file tree
Showing 4 changed files with 907 additions and 10 deletions.
14 changes: 9 additions & 5 deletions README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ bilingual_book_maker 是一个 AI 翻译工具,使用 ChatGPT 帮助用户制
9. epub 由 html 文件组成。默认情况下,我们只翻译 `<p>` 中的内容。
使用 `--translate-tags` 指定需要翻译的标签。使用逗号分隔多个标签。例如:
`--translate-tags h1,h2,h3,p,div`
10. 如果你遇到了墙需要用 Cloudflare Workers 替换 api_base 请使用 `--api_base ${url}` 来替换。
10. 请使用 --book_from 选项指定电子阅读器类型(现在只有 kobo 可用),并使用 --device_path 指定挂载点。
11. 如果你遇到了墙需要用 Cloudflare Workers 替换 api_base 请使用 `--api_base ${url}` 来替换。
**请注意,此处你输入的api应该是'`https://xxxx/v1`'的字样,域名需要用引号包裹**
11. 翻译完会生成一本 ${book_name}_bilingual.epub 的双语书
12. 如果出现了错误或使用 `CTRL+C` 中断命令,不想接下来继续翻译了,会生成一本 ${book_name}_bilingual_temp.epub 的书,直接改成你想要的名字就可以了
13. 如果你想要翻译电子书中的无标签字符串,可以使用 `--allow_navigable_strings` 参数,会将可遍历字符串加入翻译队列,**注意,在条件允许情况下,请寻找更规范的电子书**
12. 翻译完会生成一本 ${book_name}_bilingual.epub 的双语书
13. 如果出现了错误或使用 `CTRL+C` 中断命令,不想接下来继续翻译了,会生成一本 ${book_name}_bilingual_temp.epub 的书,直接改成你想要的名字就可以了
14. 如果你想要翻译电子书中的无标签字符串,可以使用 `--allow_navigable_strings` 参数,会将可遍历字符串加入翻译队列,**注意,在条件允许情况下,请寻找更规范的电子书**

e.g.
```shell
Expand All @@ -51,8 +52,11 @@ python3 make_book.py --book_name test_books/animal_farm.epub --model gpt3 --lang
# Translate contents in <div> and <p>
python3 make_book.py --book_name test_books/animal_farm.epub --translate-tags div,p

# 翻译 kobo e-reader 中,來自 Rakuten Kobo 的书籍
python3 make_book.py --book_from kobo --device_path /tmp/kobo

# 翻译 txt 文件
python3 make_book.py --book_name test_books/the_little_prince.txt -openai_key ${openai_key} --test
python3 make_book.py --book_name test_books/the_little_prince.txt --test
```

更加小白的示例
Expand Down
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ The bilingual_book_maker is an AI translation tool that uses ChatGPT to assist u
9. epub is made of html files. By default, we only translate contents in `<p>`.
Use `--translate-tags` to specify tags need for translation. Use comma to seperate multiple tags. For example:
`--translate-tags h1,h2,h3,p,div`
10. If you want to change api_base like using Cloudflare Workers, use `--api_base <URL>` to support it.
10. Use `--book_from` option to specify e-reader type (Now only `kobo` is available), and use `--device_path` to specify the mounting point.
11. If you want to change api_base like using Cloudflare Workers, use `--api_base <URL>` to support it.
**Note: the api url should be '`https://xxxx/v1`'. Quotation marks are required.**
11. Once the translation is complete, a bilingual book named `${book_name}_bilingual.epub` would be generated.
12. 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.
13. 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.**
12. Once the translation is complete, a bilingual book named `${book_name}_bilingual.epub` would be generated.
13. 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.
14. 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.**

### Eamples

Expand All @@ -56,8 +57,11 @@ python3 make_book.py --book_name test_books/animal_farm.epub --model gpt3 --lang
# Translate contents in <div> and <p>
python3 make_book.py --book_name test_books/animal_farm.epub --translate-tags div,p

# 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 -openai_key ${openai_key} --test --language zh-hans
python3 make_book.py --book_name test_books/the_little_prince.txt --test --language zh-hans
```

More understandable example
Expand Down
23 changes: 23 additions & 0 deletions book_maker/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from book_maker.loader import BOOK_LOADER_DICT
from book_maker.translator import MODEL_DICT
from book_maker.utils import LANGUAGES, TO_LANGUAGE_CODE
import book_maker.obok as obok


def main():
Expand All @@ -15,6 +16,20 @@ def main():
type=str,
help="path of the epub file to be translated",
)
parser.add_argument(
"--book_from",
dest="book_from",
type=str,
choices=["kobo"], # support kindle later
metavar="E-READER",
help="e-reader type, available: {%(choices)s}",
)
parser.add_argument(
"--device_path",
dest="device_path",
type=str,
help="Path of e-reader device",
)
parser.add_argument(
"--openai_key",
dest="openai_key",
Expand Down Expand Up @@ -108,6 +123,14 @@ def main():
else:
OPENAI_API_KEY = ""

if options.book_from == "kobo":
device_path = options.device_path
if device_path is None:
raise Exception(
"Device path is not given, please specify the path by --device_path <DEVICE_PATH>"
)
options.book_name = obok.cli_main(device_path)

book_type = options.book_name.split(".")[-1]
support_type_list = list(BOOK_LOADER_DICT.keys())
if book_type not in support_type_list:
Expand Down
Loading

0 comments on commit 2384fb3

Please sign in to comment.