diff --git a/.github/workflows/make_test_ebook.yaml b/.github/workflows/make_test_ebook.yaml index 153c0ae..022bb35 100644 --- a/.github/workflows/make_test_ebook.yaml +++ b/.github/workflows/make_test_ebook.yaml @@ -1,4 +1,4 @@ -name: Pull Request +name: CI on: push: branches: [ main ] @@ -11,19 +11,17 @@ jobs: testing: runs-on: ubuntu-latest steps: - - name: install python 3.9 - uses: actions/setup-python@v4 - with: - python-version: '3.9' - cache: 'pip' # caching pip dependencies - - name: checkout code - uses: actions/checkout@v3 - - name: Check formatting (black) - run: black . --check - - name: install python requirements - run: pip install -r requirements.txt - - name: make test ebook - env: - KEY: ${{ secrets.OPENAI_KEY }} - run: python3 make.py --book_name test_books/animal_farm.epub --openai_key $KEY --no_limit --test - \ No newline at end of file + - uses: actions/checkout@v3 + - name: install python 3.9 + uses: actions/setup-python@v3 + with: + python-version: '3.9' + cache: 'pip' # caching pip dependencies + - name: Check formatting (black) + run: black . --check + - name: install python requirements + run: pip install -r requirements.txt + - name: make test ebook + env: + OPENAI_API_KEY=: ${{ secrets.OPENAI_API_KEY }} + run: python3 make.py --book_name test_books/animal_farm.epub --no_limit --test --test_num 3 diff --git a/make.py b/make.py index 6aab1f7..2126015 100644 --- a/make.py +++ b/make.py @@ -120,6 +120,10 @@ class BEPUB: self.translate_model = model(key) self.origin_book = epub.read_epub(self.epub_name) + @staticmethod + def _is_special_text(text): + return text.isdigit() or text.isspace() + def make_bilingual_book(self): new_book = epub.EpubBook() new_book.metadata = self.origin_book.metadata @@ -136,16 +140,18 @@ class BEPUB: if i.get_type() == 9: soup = bs(i.content, "html.parser") p_list = soup.findAll("p") - is_test_done = IS_TEST and index > 20 + is_test_done = IS_TEST and index > TEST_NUM for p in p_list: if not is_test_done: - if p.text and not p.text.isdigit(): + if p.text and not self._is_special_text(p.text): new_p = copy(p) # TODO banch of p to translate then combine # PR welcome here new_p.string = self.translate_model.translate(p.text) p.insert_after(new_p) index += 1 + if IS_TEST and index > TEST_NUM: + break i.content = soup.prettify().encode() new_book.add_item(i) name = self.epub_name.split(".")[0] @@ -180,6 +186,14 @@ if __name__ == "__main__": action="store_true", help="if test we only translat 20 contents you can easily check", ) + parser.add_argument( + "--test_num", + dest="test_num", + type=int, + default=10, + help="test num for the test", + ) + parser.add_argument( "-m", "--model", @@ -192,6 +206,8 @@ if __name__ == "__main__": options = parser.parse_args() NO_LIMIT = options.no_limit IS_TEST = options.test + print(options.test_num) + TEST_NUM = options.test_num OPENAI_API_KEY = options.openai_key or env.get("OPENAI_API_KEY") if not OPENAI_API_KEY: raise Exception("Need openai API key, please google how to")