Skip to content

Commit

Permalink
supprt openai batch api (#423)
Browse files Browse the repository at this point in the history
* feat: support batch api
  • Loading branch information
mkXultra authored Aug 20, 2024
1 parent f155e20 commit 9e4e7b5
Show file tree
Hide file tree
Showing 4 changed files with 298 additions and 7 deletions.
16 changes: 16 additions & 0 deletions book_maker/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,18 @@ def main():
dest="model_list",
help="Rather than using our preset lists of models, specify exactly the models you want as a comma separated list `gpt-4-32k,gpt-3.5-turbo-0125` (Currently only supports: `openai`)",
)
parser.add_argument(
"--batch",
dest="batch_flag",
action="store_true",
help="Enable batch translation using ChatGPT's batch API for improved efficiency",
)
parser.add_argument(
"--batch-use",
dest="batch_use_flag",
action="store_true",
help="Use pre-generated batch translations to create files. Run with --batch first before using this option",
)

options = parser.parse_args()

Expand Down Expand Up @@ -461,6 +473,10 @@ def main():
e.translate_model.set_gpt4omini_models()
if options.block_size > 0:
e.block_size = options.block_size
if options.batch_flag:
e.batch_flag = options.batch_flag
if options.batch_use_flag:
e.batch_use_flag = options.batch_use_flag

e.make_bilingual_book()

Expand Down
8 changes: 8 additions & 0 deletions book_maker/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
config = {
"translator": {
"chatgptapi": {
"context_paragraph_limit": 3,
"batch_context_update_interval": 50,
}
},
}
32 changes: 29 additions & 3 deletions book_maker/loader/epub_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pickle
import string
import sys
import time
from copy import copy
from pathlib import Path

Expand Down Expand Up @@ -65,6 +66,8 @@ def __init__(
self.only_filelist = ""
self.single_translate = single_translate
self.block_size = -1
self.batch_use_flag = False
self.batch_flag = False

# monkey patch for # 173
def _write_items_patch(obj):
Expand Down Expand Up @@ -142,11 +145,18 @@ def _process_paragraph(self, p, new_p, index, p_to_save_len):
if self.resume and index < p_to_save_len:
p.string = self.p_to_save[index]
else:
t_text = ""
if self.batch_flag:
self.translate_model.add_to_batch_translate_queue(index, new_p.text)
elif self.batch_use_flag:
t_text = self.translate_model.batch_translate(index)
else:
t_text = self.translate_model.translate(new_p.text)
if type(p) == NavigableString:
new_p = self.translate_model.translate(new_p.text)
new_p = t_text
self.p_to_save.append(new_p)
else:
new_p.string = self.translate_model.translate(new_p.text)
new_p.string = t_text
self.p_to_save.append(new_p.text)

self.helper.insert_trans(
Expand Down Expand Up @@ -456,13 +466,26 @@ def process_item(

return index

def batch_init_then_wait(self):
name, _ = os.path.splitext(self.epub_name)
if self.batch_flag or self.batch_use_flag:
self.translate_model.batch_init(name)
if self.batch_use_flag:
start_time = time.time()
while not self.translate_model.is_completed_batch():
print("Batch translation is not completed yet")
time.sleep(2)
if time.time() - start_time > 300: # 5 minutes
raise Exception("Batch translation timed out after 5 minutes")

def make_bilingual_book(self):
self.helper = EPUBBookLoaderHelper(
self.translate_model,
self.accumulated_num,
self.translation_style,
self.context_flag,
)
self.batch_init_then_wait()
new_book = self._make_new_book(self.origin_book)
all_items = list(self.origin_book.get_items())
trans_taglist = self.translate_tags.split(",")
Expand Down Expand Up @@ -520,7 +543,10 @@ def make_bilingual_book(self):
name, _ = os.path.splitext(self.epub_name)
epub.write_epub(f"{name}_bilingual.epub", new_book, {})
name, _ = os.path.splitext(self.epub_name)
epub.write_epub(f"{name}_bilingual.epub", new_book, {})
if self.batch_flag:
self.translate_model.batch()
else:
epub.write_epub(f"{name}_bilingual.epub", new_book, {})
if self.accumulated_num == 1:
pbar.close()
except (KeyboardInterrupt, Exception) as e:
Expand Down
Loading

0 comments on commit 9e4e7b5

Please sign in to comment.