From 4219f9c31d04d99e109c5bae8404783ae5ced961 Mon Sep 17 00:00:00 2001 From: Kevin Date: Sun, 5 Mar 2023 09:45:02 +0800 Subject: [PATCH] Help improve progress bar on the book translation progress (#17) * Update make_book.py * Update requirements.txt * Create main.yml * fix error for linting * fix error * fix: support test of bar --------- Co-authored-by: yihong0618 --- make_book.py | 10 ++++++++-- requirements.txt | 3 ++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/make_book.py b/make_book.py index e3fbcd9..de039a0 100644 --- a/make_book.py +++ b/make_book.py @@ -6,6 +6,7 @@ from abc import abstractmethod from copy import copy from os import environ as env from pathlib import Path +from tqdm import tqdm import openai import requests @@ -118,7 +119,7 @@ class ChatGPT(Base): messages=[ { "role": "user", - "content": f"Please help me to translate,`{text}` to Simplified Chinese, please return only translated content not include the origin text", + "content": f"Please help me to translate,`{text}` to Simplified Chinese, please return only translated content not include the origin text", } ], ) @@ -159,11 +160,15 @@ class BEPUB: all_p_length = sum( [len(bs(i.content, "html.parser").findAll("p")) for i in all_items] ) - print("TODO need process bar here: " + str(all_p_length)) + if IS_TEST: + pbar = tqdm(total=TEST_NUM) + else: + pbar = tqdm(total=all_p_length) index = 0 p_to_save_len = len(self.p_to_save) try: for i in self.origin_book.get_items(): + pbar.update(index) if i.get_type() == 9: soup = bs(i.content, "html.parser") p_list = soup.findAll("p") @@ -187,6 +192,7 @@ class BEPUB: new_book.add_item(i) name = self.epub_name.split(".")[0] epub.write_epub(f"{name}_bilingual.epub", new_book, {}) + pbar.close() except (KeyboardInterrupt, Exception) as e: print(e) print("you can resume it next time") diff --git a/requirements.txt b/requirements.txt index 34b74e6..3cca106 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,5 @@ bs4 openai requests ebooklib -rich \ No newline at end of file +rich +tqdm