fix: actions

This commit is contained in:
yihong0618 2023-03-03 20:42:45 +08:00
parent c27efda1dc
commit bc27e9900c
2 changed files with 33 additions and 19 deletions

View File

@ -1,4 +1,4 @@
name: Pull Request name: CI
on: on:
push: push:
branches: [ main ] branches: [ main ]
@ -11,19 +11,17 @@ jobs:
testing: testing:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- name: install python 3.9 - uses: actions/checkout@v3
uses: actions/setup-python@v4 - name: install python 3.9
with: uses: actions/setup-python@v3
python-version: '3.9' with:
cache: 'pip' # caching pip dependencies python-version: '3.9'
- name: checkout code cache: 'pip' # caching pip dependencies
uses: actions/checkout@v3 - name: Check formatting (black)
- name: Check formatting (black) run: black . --check
run: black . --check - name: install python requirements
- name: install python requirements run: pip install -r requirements.txt
run: pip install -r requirements.txt - name: make test ebook
- name: make test ebook env:
env: OPENAI_API_KEY=: ${{ secrets.OPENAI_API_KEY }}
KEY: ${{ secrets.OPENAI_KEY }} run: python3 make.py --book_name test_books/animal_farm.epub --no_limit --test --test_num 3
run: python3 make.py --book_name test_books/animal_farm.epub --openai_key $KEY --no_limit --test

20
make.py
View File

@ -120,6 +120,10 @@ class BEPUB:
self.translate_model = model(key) self.translate_model = model(key)
self.origin_book = epub.read_epub(self.epub_name) 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): def make_bilingual_book(self):
new_book = epub.EpubBook() new_book = epub.EpubBook()
new_book.metadata = self.origin_book.metadata new_book.metadata = self.origin_book.metadata
@ -136,16 +140,18 @@ class BEPUB:
if i.get_type() == 9: if i.get_type() == 9:
soup = bs(i.content, "html.parser") soup = bs(i.content, "html.parser")
p_list = soup.findAll("p") 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: for p in p_list:
if not is_test_done: 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) new_p = copy(p)
# TODO banch of p to translate then combine # TODO banch of p to translate then combine
# PR welcome here # PR welcome here
new_p.string = self.translate_model.translate(p.text) new_p.string = self.translate_model.translate(p.text)
p.insert_after(new_p) p.insert_after(new_p)
index += 1 index += 1
if IS_TEST and index > TEST_NUM:
break
i.content = soup.prettify().encode() i.content = soup.prettify().encode()
new_book.add_item(i) new_book.add_item(i)
name = self.epub_name.split(".")[0] name = self.epub_name.split(".")[0]
@ -180,6 +186,14 @@ if __name__ == "__main__":
action="store_true", action="store_true",
help="if test we only translat 20 contents you can easily check", 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( parser.add_argument(
"-m", "-m",
"--model", "--model",
@ -192,6 +206,8 @@ if __name__ == "__main__":
options = parser.parse_args() options = parser.parse_args()
NO_LIMIT = options.no_limit NO_LIMIT = options.no_limit
IS_TEST = options.test 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") OPENAI_API_KEY = options.openai_key or env.get("OPENAI_API_KEY")
if not OPENAI_API_KEY: if not OPENAI_API_KEY:
raise Exception("Need openai API key, please google how to") raise Exception("Need openai API key, please google how to")