mirror of
https://github.com/yihong0618/bilingual_book_maker.git
synced 2025-06-04 02:20:18 +00:00
feat: add ChatGPT Account translate method and input options to __init… (#43)
add ChatGPT Account translate method --------- Co-authored-by: yihong0618 <zouzou0208@gmail.com>
This commit is contained in:
parent
e7ab4c7c81
commit
91ef9fa788
1
.gitignore
vendored
1
.gitignore
vendored
@ -133,3 +133,4 @@ dmypy.json
|
||||
|
||||
/test_books/*.epub
|
||||
log/
|
||||
.chatgpt_cache.json
|
||||
|
@ -43,7 +43,7 @@ bilingual_book_maker 是一个 AI 翻译工具,使用 ChatGPT 帮助用户制
|
||||
你也可以用环境以下环境变量来配置 `system` 和 `user` 角色 prompt:`BBM_CHATGPTAPI_USER_MSG_TEMPLATE` 和 `BBM_CHATGPTAPI_SYS_MSG`。
|
||||
该参数可以是提示模板字符串,也可以是模板 `.txt` 文件的路径。
|
||||
- 使用`--batch_size` 参数,指定批量翻译的行数(默认行数为10,目前只对txt生效)
|
||||
|
||||
- 新增 model chatgptaccount,使用`--chatgptaccount`、`--chatgptpassword`传入 ChatGPT PLUS 账号密码进行翻译,不需要传key。
|
||||
|
||||
### 示范用例
|
||||
|
||||
@ -81,6 +81,9 @@ 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
|
||||
|
||||
# chatgpt account model 翻译
|
||||
python3 make_book.py --book_name test_books/animal_farm.epub --model chatgptaccount --chatgptaccount=xx--chatgptpassword xx
|
||||
|
||||
# 使用彩云小译翻译(彩云api目前只支持: 简体中文 <-> 英文, 简体中文 <-> 日语)
|
||||
# 彩云提供了测试token(3975l6lr5pcbvidl6jl2)
|
||||
# 你可以参考这个教程申请自己的token (https://bobtranslate.com/service/translate/caiyun.html)
|
||||
|
@ -49,7 +49,7 @@ Retranslate from start_str to end_str's tag:
|
||||
`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>
|
||||
Retranslate start_str's tag:
|
||||
`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'`
|
||||
|
||||
- - Add model chatgptaccount, use `--chatgptaccount`, `--chatgptpassword` to pass in ChatGPT PLUS account password for translation, no need to pass key.
|
||||
### Examples
|
||||
|
||||
**Note if use `pip install bbook_maker` all commands can change to `bbook args`**
|
||||
@ -94,6 +94,11 @@ python3 make_book.py --book_name test_books/the_little_prince.txt --test --batch
|
||||
# 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
|
||||
|
||||
#chatgptaccount model translate
|
||||
python3 make_book.py --book_name test_books/animal_farm.epub --model chatgptaccount --chatgptaccount=xx--chatgptpassword xx
|
||||
|
||||
|
||||
# Set env BBM_CAIYUN_API_KEY to ignore option --openai_key
|
||||
export BBM_CAIYUN_API_KEY=${your_api_key}
|
||||
|
||||
|
@ -210,6 +210,21 @@ So you are close to reaching the limit. You have to choose your own value, there
|
||||
""",
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
"--chatgptaccount",
|
||||
dest="chatgptaccount",
|
||||
type=str,
|
||||
help="your chatgpt account",
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
"--chatgptpassword",
|
||||
dest="chatgptpassword",
|
||||
type=str,
|
||||
help="your chatgpt password",
|
||||
)
|
||||
chatgpt_account = ""
|
||||
chatgpt_password = ""
|
||||
options = parser.parse_args()
|
||||
|
||||
if not os.path.isfile(options.book_name):
|
||||
@ -223,6 +238,7 @@ So you are close to reaching the limit. You have to choose your own value, there
|
||||
|
||||
translate_model = MODEL_DICT.get(options.model)
|
||||
assert translate_model is not None, "unsupported model"
|
||||
API_KEY = ""
|
||||
if options.model in ["gpt3", "chatgptapi"]:
|
||||
if OPENAI_API_KEY := (
|
||||
options.openai_key
|
||||
@ -246,6 +262,11 @@ So you are close to reaching the limit. You have to choose your own value, there
|
||||
API_KEY = options.deepl_key or env.get("BBM_DEEPL_API_KEY")
|
||||
if not API_KEY:
|
||||
raise Exception("Please provid deepl key")
|
||||
elif options.model == "chatgptaccount":
|
||||
chatgpt_account = options.chatgptaccount
|
||||
chatgpt_password = options.chatgptpassword
|
||||
if chatgpt_account is None or chatgpt_password is None:
|
||||
raise Exception("Please provid chatgptaccount and chatgptpassword")
|
||||
else:
|
||||
API_KEY = ""
|
||||
|
||||
@ -286,6 +307,8 @@ So you are close to reaching the limit. You have to choose your own value, there
|
||||
is_test=options.test,
|
||||
test_num=options.test_num,
|
||||
prompt_config=parse_prompt_arg(options.prompt_arg),
|
||||
chatgptaccount=chatgpt_account,
|
||||
chatgptpassword=chatgpt_password,
|
||||
)
|
||||
# other options
|
||||
if options.allow_navigable_strings:
|
||||
|
@ -29,6 +29,8 @@ class EPUBBookLoader(BaseBookLoader):
|
||||
is_test=False,
|
||||
test_num=5,
|
||||
prompt_config=None,
|
||||
chatgptaccount=None,
|
||||
chatgptpassword=None,
|
||||
):
|
||||
self.epub_name = epub_name
|
||||
self.new_epub = epub.EpubBook()
|
||||
@ -36,6 +38,8 @@ class EPUBBookLoader(BaseBookLoader):
|
||||
key,
|
||||
language,
|
||||
api_base=model_api_base,
|
||||
chatgptaccount=chatgptaccount,
|
||||
chatgptpassword=chatgptpassword,
|
||||
**prompt_config_to_kwargs(prompt_config),
|
||||
)
|
||||
self.is_test = is_test
|
||||
|
@ -18,12 +18,16 @@ class TXTBookLoader(BaseBookLoader):
|
||||
is_test=False,
|
||||
test_num=5,
|
||||
prompt_config=None,
|
||||
chatgptaccount=None,
|
||||
chatgptpassword=None,
|
||||
) -> None:
|
||||
self.txt_name = txt_name
|
||||
self.translate_model = model(
|
||||
key,
|
||||
language,
|
||||
api_base=model_api_base,
|
||||
chatgptaccount=chatgptaccount,
|
||||
chatgptpassword=chatgptpassword,
|
||||
**prompt_config_to_kwargs(prompt_config),
|
||||
)
|
||||
self.is_test = is_test
|
||||
|
@ -1,4 +1,5 @@
|
||||
from book_maker.translator.caiyun_translator import Caiyun
|
||||
from book_maker.translator.chatgpt_account_translator import ChatGPTAccount
|
||||
from book_maker.translator.chatgptapi_translator import ChatGPTAPI
|
||||
from book_maker.translator.deepl_translator import DeepL
|
||||
from book_maker.translator.google_translator import Google
|
||||
@ -10,5 +11,6 @@ MODEL_DICT = {
|
||||
"google": Google,
|
||||
"caiyun": Caiyun,
|
||||
"deepl": DeepL,
|
||||
"chatgptaccount": ChatGPTAccount,
|
||||
# add more here
|
||||
}
|
||||
|
33
book_maker/translator/chatgpt_account_translator.py
Normal file
33
book_maker/translator/chatgpt_account_translator.py
Normal file
@ -0,0 +1,33 @@
|
||||
import requests
|
||||
from revChatGPT.V1 import Chatbot
|
||||
from rich import print
|
||||
|
||||
from .base_translator import Base
|
||||
|
||||
|
||||
class ChatGPTAccount(Base):
|
||||
def __init__(self, key, language, **kwargs):
|
||||
super().__init__(key, language)
|
||||
self.language = language
|
||||
self.chatgpt_account = kwargs.get("chatgptaccount")
|
||||
self.chatgpt_password = kwargs.get("chatgptpassword")
|
||||
|
||||
def rotate_key(self):
|
||||
pass
|
||||
|
||||
def translate(self, text):
|
||||
print(text)
|
||||
chatbot = Chatbot(
|
||||
config={
|
||||
"email": self.chatgpt_account,
|
||||
"password": self.chatgpt_password,
|
||||
}
|
||||
)
|
||||
prompt = f"Please help me to translate,`{text}` to {self.language}"
|
||||
response = ""
|
||||
|
||||
for data in chatbot.ask(prompt):
|
||||
response = data["message"].encode("utf8").decode()
|
||||
|
||||
print(response)
|
||||
return response
|
@ -5,3 +5,4 @@ ebooklib
|
||||
rich
|
||||
tqdm
|
||||
tiktoken
|
||||
revChatGPT
|
Loading…
x
Reference in New Issue
Block a user