Skip to content

Commit

Permalink
(yihong0618#93) feat: add caiyun translator (yihong0618#160)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
Voyageyang and yihong0618 authored Mar 13, 2023
1 parent 0a1991d commit 839c2ce
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 4 deletions.
7 changes: 7 additions & 0 deletions README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}

```

更加小白的示例
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 14 additions & 3 deletions book_maker/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion book_maker/translator/__init__.py
Original file line number Diff line number Diff line change
@@ -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 changes: 43 additions & 0 deletions book_maker/translator/caiyun_translator.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 839c2ce

Please sign in to comment.