forked from yihong0618/bilingual_book_maker
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(yihong0618#93) feat: add caiyun translator (yihong0618#160)
* 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
1 parent
0a1991d
commit 839c2ce
Showing
5 changed files
with
76 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |