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 15 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 @@ -129,6 +129,13 @@ def main():
metavar="PROMPT_TEMPLATE",
help="used for customizing the prompt. It can be the prompt template string, or a path to the template file. The valid placeholders are `{text}` and `{language}`.",
)
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 @@ -183,6 +190,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,
prompt_template=parse_prompt_arg(options.prompt_template),
)
e.make_bilingual_book()
Expand Down
113 changes: 100 additions & 13 deletions book_maker/loader/epub_loader.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import re
import pickle
import sys
from copy import copy
Expand All @@ -12,6 +13,21 @@
from .base_loader import BaseBookLoader


def isLink(text):
url_pattern = re.compile(
r"http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+"
)
return bool(url_pattern.match(text.strip()))


def isSourceLink(text):
text = text.strip()
return text.startswith("Source: ") and re.search(
r"http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+",
text,
)


class EPUBBookLoader(BaseBookLoader):
def __init__(
self,
Expand All @@ -25,6 +41,7 @@ def __init__(
test_num=5,
translate_tags="p",
allow_navigable_strings=False,
accumulated_num=1,
prompt_template=None,
):
self.epub_name = epub_name
Expand All @@ -36,6 +53,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 @@ -63,7 +81,7 @@ def _load_spine(self):

@staticmethod
def _is_special_text(text):
return text.isdigit() or text.isspace()
return text.isdigit() or text.isspace() or isLink(text)

def _make_new_book(self, book):
new_book = epub.EpubBook()
Expand All @@ -73,6 +91,28 @@ def _make_new_book(self, book):
return new_book

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

def deal_old(wait_p_list):
if len(wait_p_list) == 0:
return []

result_txt_list = self.translate_model.translate_list(wait_p_list)

for i in range(0, len(wait_p_list)):
hleft marked this conversation as resolved.
Show resolved Hide resolved
if i < len(result_txt_list):
p = wait_p_list[i]
new_p = copy(p)
new_p.string = result_txt_list[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 @@ -92,12 +132,56 @@ 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):
# if item.file_name != "OEBPS/ch01.xhtml":
# continue

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

send_num = self.accumulated_num
if send_num > 1:
print("------------------------------------------------------")
print(f"dealing {item.file_name} ...")
count = 0
wait_p_list = []
for i in range(0, len(p_list)):
hleft marked this conversation as resolved.
Show resolved Hide resolved
p = p_list[i]
temp_p = copy(p)
for sup in temp_p.find_all("sup"):
sup.extract()
if (
not p.text
or self._is_special_text(temp_p.text)
or isSourceLink(temp_p.text)
):
continue
length = len(p.text)
if length > send_num:
wait_p_list = deal_new(p, wait_p_list)
continue
if i == len(p_list) - 1:
if count + length < send_num:
wait_p_list.append(p)
wait_p_list = deal_old(wait_p_list)
else:
wait_p_list = deal_new(p, wait_p_list)
break
if count + length < send_num:
count += length
wait_p_list.append(p)
else:
wait_p_list = deal_old(wait_p_list)
wait_p_list.append(p)
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 @@ -118,16 +202,19 @@ 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, {})
pbar.close()
name, _ = os.path.splitext(self.epub_name)
epub.write_epub(f"{name}_bilingual.epub", new_book, {})
if self.accumulated_num == 1:
pbar.close()
except (KeyboardInterrupt, Exception) as e:
print(e)
print("you can resume it next time")
self._save_progress()
self._save_temp_book()
if self.accumulated_num == 1:
print("you can resume it next time")
self._save_progress()
self._save_temp_book()
sys.exit(0)

def load_state(self):
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,
prompt_template=None,
):
self.txt_name = txt_name
Expand Down
100 changes: 93 additions & 7 deletions book_maker/translator/chatgptapi_translator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import time
import re
from copy import copy

import openai
from os import environ
Expand All @@ -16,24 +18,26 @@ def __init__(self, key, language, api_base=None, prompt_template=None):
prompt_template
or "Please help me to translate,`{text}` to {language}, please return only translated content not include the origin text"
)
self.system_content = environ.get("OPENAI_API_SYS_MSG") or ""

max_num_token = -1

def rotate_key(self):
openai.api_key = next(self.keys)

def get_translation(self, text):
self.rotate_key()
content = self.prompt_template.format(text=text, language=self.language)
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "system",
"content": environ.get("OPENAI_API_SYS_MSG") or "",
"content": self.system_content,
},
{
"role": "user",
"content": self.prompt_template.format(
text=text, language=self.language
),
"content": content,
},
],
)
Expand All @@ -44,11 +48,19 @@ def get_translation(self, text):
.encode("utf8")
.decode()
)
print("=================================================")
self.max_num_token = max(
self.max_num_token, int(completion["usage"]["total_tokens"])
)
print(
f"{completion['usage']['total_tokens']} {completion['usage']['prompt_tokens']} {completion['usage']['completion_tokens']} {self.max_num_token} (total_token, prompt_token, completion_tokens, max_history_total_token)"
)
return t_text

def translate(self, text):
def translate(self, text, needprint=True):
# todo: Determine whether to print according to the cli option
print(text)
if needprint:
print(re.sub("\n{3,}", "\n\n", text))

try:
t_text = self.get_translation(text)
Expand All @@ -64,5 +76,79 @@ def translate(self, text):
t_text = self.get_translation(text)

# todo: Determine whether to print according to the cli option
print(t_text.strip())
if needprint:
print(re.sub("\n{3,}", "\n\n", t_text))
return t_text

def translate_and_split_lines(self, text):
result_str = self.translate(text, False)
lines = result_str.split("\n")
lines = [line.strip() for line in lines if line.strip() != ""]
return lines

def translate_list(self, plist):
sep = "\n\n\n\n\n"
# new_str = sep.join([item.text for item in plist])

new_str = ""
for p in plist:
temp_p = copy(p)
for sup in temp_p.find_all("sup"):
sup.extract()
new_str += temp_p.get_text().strip() + sep

if new_str.endswith(sep):
new_str = new_str[: -len(sep)]

plist_len = len(plist)
self.system_content += f"""Please translate the following paragraphs individually while preserving their original structure(This time it should be exactly {plist_len} paragraphs, no more or less). Only translate the paragraphs provided below:

[Insert first paragraph here]

[Insert second paragraph here]

[Insert third paragraph here]"""

retry_count = 0
result_list = self.translate_and_split_lines(new_str)

hleft marked this conversation as resolved.
Show resolved Hide resolved
while len(result_list) != plist_len and retry_count < 3:
print(
f"bug: {plist_len} -> {len(result_list)} : Number of paragraphs before and after translation"
)
sleep_dur = 6
hleft marked this conversation as resolved.
Show resolved Hide resolved
print(f"sleep for {sleep_dur}s and retry {retry_count+1} ...")
time.sleep(sleep_dur)
result_list = self.translate_and_split_lines(new_str)
retry_count += 1

state = "success"
if len(result_list) != plist_len:
state = "fail"

hleft marked this conversation as resolved.
Show resolved Hide resolved
if retry_count > 0:
print(f"retry {state}")
with open("buglog.txt", "a") as f:
print(
f"retry {state}, count = {retry_count}",
file=f,
)

if len(result_list) != plist_len:
newlist = new_str.split(sep)
with open("buglog.txt", "a") as f:
hleft marked this conversation as resolved.
Show resolved Hide resolved
for i in range(0, len(newlist)):
hleft marked this conversation as resolved.
Show resolved Hide resolved
print(newlist[i], file=f)
print(file=f)
if i < len(result_list):
print(result_list[i], file=f)
print(file=f)
print("=============================", file=f)
print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")

print(
f"bug: {plist_len} paragraphs of text translated into {len(result_list)} paragraphs"
)
print("continue")

return result_list