mirror of
https://github.com/yihong0618/bilingual_book_maker.git
synced 2025-06-05 19:15:34 +00:00
* feat: add caiyun translator * format code and update README-CN.md * fix: add caiyun_key args * fix: add raise --------- Co-authored-by: yihong0618 <zouzou0208@gmail.com>
This commit is contained in:
parent
0a1991d8ad
commit
839c2ce6ad
@ -75,6 +75,13 @@ python3 make_book.py --book_name test_books/the_little_prince.txt --test
|
||||
# 聚合多行翻译 txt 文件
|
||||
python3 make_book.py --book_name test_books/the_little_prince.txt --test --batch_size 20
|
||||
|
||||
# 使用彩云小译翻译(彩云api目前只支持: 简体中文 <-> 英文, 简体中文 <-> 日语)
|
||||
# 彩云提供了测试token(3975l6lr5pcbvidl6jl2)
|
||||
# 你可以参考这个教程申请自己的token (https://bobtranslate.com/service/translate/caiyun.html)
|
||||
python3 make_book.py --model caiyun --openai_key 3975l6lr5pcbvidl6jl2 --book_name test_books/animal_farm.epub
|
||||
# 可以在环境变量中设置BBM_CAIYUN_API_KEY,略过--openai_key
|
||||
export BBM_CAIYUN_API_KEY=${your_api_key}
|
||||
|
||||
```
|
||||
|
||||
更加小白的示例
|
||||
|
@ -77,6 +77,15 @@ python3 make_book.py --book_from kobo --device_path /tmp/kobo
|
||||
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 --openai_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
|
||||
|
@ -100,7 +100,7 @@ def main():
|
||||
dest="model",
|
||||
type=str,
|
||||
default="chatgptapi",
|
||||
choices=["chatgptapi", "gpt3", "google"], # support DeepL later
|
||||
choices=["chatgptapi", "gpt3", "google", "caiyun"], # support DeepL later
|
||||
metavar="MODEL",
|
||||
help="model to use, available: {%(choices)s}",
|
||||
)
|
||||
@ -162,6 +162,12 @@ def main():
|
||||
default=10,
|
||||
help="how many lines will be translated by aggregated translation(This options currently only applies to txt files)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--caiyun_key",
|
||||
dest="caiyun_key",
|
||||
type=str,
|
||||
help="you can apply caiyun key from here (https://dashboard.caiyunapp.com/user/sign_in/)",
|
||||
)
|
||||
|
||||
options = parser.parse_args()
|
||||
PROXY = options.proxy
|
||||
@ -185,8 +191,13 @@ def main():
|
||||
raise Exception(
|
||||
"OpenAI API key not provided, please google how to obtain it"
|
||||
)
|
||||
API_KEY = OPENAI_API_KEY
|
||||
elif options.model == "caiyun":
|
||||
API_KEY = options.caiyun_key or env.get("BBM_CAIYUN_API_KEY")
|
||||
if not API_KEY:
|
||||
raise Exception("Please provid caiyun key")
|
||||
else:
|
||||
OPENAI_API_KEY = ""
|
||||
API_KEY = ""
|
||||
|
||||
if options.book_from == "kobo":
|
||||
import book_maker.obok as obok
|
||||
@ -218,7 +229,7 @@ def main():
|
||||
e = book_loader(
|
||||
options.book_name,
|
||||
translate_model,
|
||||
OPENAI_API_KEY,
|
||||
API_KEY,
|
||||
options.resume,
|
||||
language=language,
|
||||
model_api_base=model_api_base,
|
||||
|
@ -1,10 +1,12 @@
|
||||
from book_maker.translator.chatgptapi_translator import ChatGPTAPI
|
||||
from book_maker.translator.google_translator import Google
|
||||
from book_maker.translator.gpt3_translator import GPT3
|
||||
from book_maker.translator.caiyun_translator import Caiyun
|
||||
|
||||
MODEL_DICT = {
|
||||
"chatgptapi": ChatGPTAPI,
|
||||
"gpt3": GPT3,
|
||||
"google": Google
|
||||
"google": Google,
|
||||
"caiyun": Caiyun
|
||||
# add more here
|
||||
}
|
||||
|
43
book_maker/translator/caiyun_translator.py
Normal file
43
book_maker/translator/caiyun_translator.py
Normal file
@ -0,0 +1,43 @@
|
||||
import json
|
||||
|
||||
import requests
|
||||
|
||||
from .base_translator import Base
|
||||
|
||||
|
||||
class Caiyun(Base):
|
||||
"""
|
||||
caiyun translator
|
||||
"""
|
||||
|
||||
def __init__(self, key, language, **kwargs):
|
||||
super().__init__(key, language)
|
||||
self.api_url = "http://api.interpreter.caiyunai.com/v1/translator"
|
||||
self.headers = {
|
||||
"content-type": "application/json",
|
||||
"x-authorization": "token " + key,
|
||||
}
|
||||
# caiyun api only supports: zh2en, zh2ja, en2zh, ja2zh
|
||||
self.translate_type = "auto2zh"
|
||||
if self.language == "english":
|
||||
self.translate_type = "auto2en"
|
||||
elif self.language == "japanese":
|
||||
self.translate_type = "auto2ja"
|
||||
|
||||
def rotate_key(self):
|
||||
pass
|
||||
|
||||
def translate(self, text):
|
||||
print(text)
|
||||
payload = {
|
||||
"source": text,
|
||||
"trans_type": self.translate_type,
|
||||
"request_id": "demo",
|
||||
"detect": True,
|
||||
}
|
||||
response = requests.request(
|
||||
"POST", self.api_url, data=json.dumps(payload), headers=self.headers
|
||||
)
|
||||
t_text = json.loads(response.text)["target"]
|
||||
print(t_text)
|
||||
return t_text
|
Loading…
x
Reference in New Issue
Block a user