Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cumulative translation #148

Merged
merged 37 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
502d909
support config tags to translate
hleft Mar 8, 2023
7505d59
Merge branch 'main' of https://github.com/yihong0618/bilingual_book_m…
hleft Mar 10, 2023
c31b01c
Merge branch 'main' of https://github.com/yihong0618/bilingual_book_m…
hleft Mar 10, 2023
3277eef
support system meesage in envirment
hleft Mar 11, 2023
c137fec
Merge branch 'main' of https://github.com/yihong0618/bilingual_book_m…
hleft Mar 11, 2023
f39b926
cumulative translation
hleft Mar 11, 2023
58d7939
Merge branch 'main' of https://github.com/yihong0618/bilingual_book_m…
hleft Mar 11, 2023
59ed6d9
fix
hleft Mar 11, 2023
7c91331
Merge branch 'main' of https://github.com/yihong0618/bilingual_book_m…
hleft Mar 11, 2023
2baf359
fix
hleft Mar 11, 2023
21478d9
Merge branch 'main' of https://github.com/yihong0618/bilingual_book_m…
hleft Mar 11, 2023
3523469
clean
hleft Mar 11, 2023
9aade4b
prompt and retry
hleft Mar 11, 2023
8badb41
improve prompt and fix <sup>
hleft Mar 12, 2023
b0d4f86
clean, fix link translate
hleft Mar 12, 2023
46320b4
clean
hleft Mar 12, 2023
0546647
more prompt, exclude Listing, change output
hleft Mar 12, 2023
4be8dc4
deal exception: ["finish_reason"] == "length"
hleft Mar 13, 2023
8174ebe
shorter prompt
hleft Mar 13, 2023
b424b3f
Cumulative tokens instead of characters
hleft Mar 13, 2023
b93fd44
reduce err
hleft Mar 13, 2023
8baf990
deal figure, change output for test
hleft Mar 14, 2023
e7b6c27
If there will be errors in the end, choose the least erroneous
hleft Mar 14, 2023
97da3fe
Merge branch 'main' of https://github.com/yihong0618/bilingual_book_m…
hleft Mar 14, 2023
b325621
revert write book
hleft Mar 14, 2023
226da38
clean
hleft Mar 15, 2023
6e714d3
refactor epub_loader by gpt4
hleft Mar 15, 2023
4e42860
refactor
hleft Mar 15, 2023
cc85d6b
update readme and help
hleft Mar 15, 2023
c859c69
fix
hleft Mar 15, 2023
173b756
improve exception
hleft Mar 15, 2023
e24a1c5
improve exception
hleft Mar 15, 2023
32cadfa
Merge branch 'main' of https://github.com/yihong0618/bilingual_book_m…
hleft Mar 16, 2023
cf92918
Merge branch 'yihong0618:main' into cumulative-translation
hleft Mar 16, 2023
c535041
use ordinals to ensure order instead of prompts
hleft Mar 16, 2023
34ed9dc
Merge branch 'cumulative-translation' of https://github.com/hleft/bil…
hleft Mar 16, 2023
b73240a
comment debug output for merge
hleft Mar 16, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions book_maker/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ def main():
default=False,
help="allow NavigableStrings to be translated",
)
parser.add_argument(
"--accumulated_num",
dest="accumulated_num",
type=int,
default=1,
help="Wait for how many characters have been accumulated before starting the translation",
)

options = parser.parse_args()
PROXY = options.proxy
Expand Down Expand Up @@ -159,6 +166,7 @@ def main():
test_num=options.test_num,
translate_tags=options.translate_tags,
allow_navigable_strings=options.allow_navigable_strings,
accumulated_num=options.accumulated_num,
)
e.make_bilingual_book()

Expand Down
69 changes: 63 additions & 6 deletions book_maker/loader/epub_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def __init__(
test_num=5,
translate_tags="p",
allow_navigable_strings=False,
accumulated_num=1,
):
self.epub_name = epub_name
self.new_epub = epub.EpubBook()
Expand All @@ -33,6 +34,7 @@ def __init__(
self.test_num = test_num
self.translate_tags = translate_tags
self.allow_navigable_strings = allow_navigable_strings
self.accumulated_num = accumulated_num

try:
self.origin_book = epub.read_epub(self.epub_name)
Expand Down Expand Up @@ -70,6 +72,28 @@ def _make_new_book(self, book):
return new_book

def make_bilingual_book(self):
def deal_new(p, waitPList):
ret = deal_old(waitPList)
new_p = copy(p)
new_p.string = self.translate_model.translate(p.text)
p.insert_after(new_p)
return ret

def deal_old(waitPList):
hleft marked this conversation as resolved.
Show resolved Hide resolved
if len(waitPList) == 0:
return []

resultTxtList = self.translate_model.translate_list(waitPList)
hleft marked this conversation as resolved.
Show resolved Hide resolved

for i in range(0, len(waitPList)):
if i < len(resultTxtList):
p = waitPList[i]
new_p = copy(p)
new_p.string = resultTxtList[i]
p.insert_after(new_p)

return []
hleft marked this conversation as resolved.
Show resolved Hide resolved
hleft marked this conversation as resolved.
Show resolved Hide resolved

new_book = self._make_new_book(self.origin_book)
all_items = list(self.origin_book.get_items())
trans_taglist = self.translate_tags.split(",")
Expand All @@ -89,12 +113,44 @@ def make_bilingual_book(self):
index = 0
p_to_save_len = len(self.p_to_save)
try:
# Add the things that don't need to be translated first, so that you can see the img after the interruption
for item in self.origin_book.get_items():
if item.get_type() == ITEM_DOCUMENT:
soup = bs(item.content, "html.parser")
p_list = soup.findAll(trans_taglist)
if self.allow_navigable_strings:
p_list.extend(soup.findAll(text=True))
if item.get_type() != ITEM_DOCUMENT:
new_book.add_item(item)

for item in self.origin_book.get_items_of_type(ITEM_DOCUMENT):
soup = bs(item.content, "html.parser")
p_list = soup.findAll(trans_taglist)
if self.allow_navigable_strings:
p_list.extend(soup.findAll(text=True))

sendNum = self.accumulated_num
hleft marked this conversation as resolved.
Show resolved Hide resolved
if sendNum > 1:
count = 0
waitPList = []
hleft marked this conversation as resolved.
Show resolved Hide resolved
for i in range(0, len(p_list)):
hleft marked this conversation as resolved.
Show resolved Hide resolved
p = p_list[i]
if not p.text or self._is_special_text(p.text):
continue
length = len(p.text)
if length > sendNum:
waitPList = deal_new(p, waitPList)
continue
if i == len(p_list) - 1:
if count + length < sendNum:
waitPList.append(p)
waitPList = deal_old(waitPList)
else:
waitPList = deal_new(p, waitPList)
break
if count + length < sendNum:
count += length
waitPList.append(p)
else:
waitPList = deal_old(waitPList)
waitPList.append(p)
hleft marked this conversation as resolved.
Show resolved Hide resolved
yihong0618 marked this conversation as resolved.
Show resolved Hide resolved
count = len(p.text)
else:
is_test_done = self.is_test and index > self.test_num
for p in p_list:
if is_test_done or not p.text or self._is_special_text(p.text):
Expand All @@ -115,7 +171,8 @@ def make_bilingual_book(self):
pbar.update(1)
if self.is_test and index >= self.test_num:
break
item.content = soup.prettify().encode()

item.content = soup.prettify().encode()
new_book.add_item(item)
name, _ = os.path.splitext(self.epub_name)
epub.write_epub(f"{name}_bilingual.epub", new_book, {})
Expand Down
1 change: 1 addition & 0 deletions book_maker/loader/txt_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def __init__(
model_api_base=None,
is_test=False,
test_num=5,
accumulated_num=1,
):
self.txt_name = txt_name
self.translate_model = model(key, language, model_api_base)
Expand Down
12 changes: 11 additions & 1 deletion book_maker/translator/chatgptapi_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def get_translation(self, text):
messages=[
{
"role": "system",
"content": environ.get("OPENAI_API_SYS_MSG"),
"content": environ.get("OPENAI_API_SYS_MSG") or "",
},
{
"role": "user",
Expand Down Expand Up @@ -60,3 +60,13 @@ def translate(self, text):
# todo: Determine whether to print according to the cli option
print(t_text)
return t_text

def translate_list(self, plist):
sep = "\n\n\n\n\n"
new_str = sep.join([item.text for item in plist])
resultStr = self.translate(new_str)
hleft marked this conversation as resolved.
Show resolved Hide resolved

lines = resultStr.split("\n")
lines = [line.strip() for line in lines if line.strip() != ""]

return lines