mirror of
https://github.com/yihong0618/bilingual_book_maker.git
synced 2025-06-05 19:15:34 +00:00
feat: MakeFile easy to test local (#254)
This commit is contained in:
parent
45bfdf7907
commit
484a98b48f
40
.github/workflows/make_test_ebook.yaml
vendored
40
.github/workflows/make_test_ebook.yaml
vendored
@ -34,42 +34,8 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
pip install .
|
pip install .
|
||||||
|
|
||||||
- name: make normal ebook test using google translate and cli
|
- name: Run tests
|
||||||
run: |
|
run: |
|
||||||
bbook_maker --book_name "test_books/Liber_Esther.epub" --test --test_num 10 --model google --translate-tags div,p
|
pip install pytest
|
||||||
bbook_maker --book_name "test_books/Liber_Esther.epub" --test --test_num 20 --model google
|
pytest tests
|
||||||
|
|
||||||
- name: make txt book test using google translate
|
|
||||||
run: |
|
|
||||||
python3 make_book.py --book_name "test_books/the_little_prince.txt" --test --test_num 20 --model google
|
|
||||||
|
|
||||||
- name: make txt book test with batch_size
|
|
||||||
run: |
|
|
||||||
python3 make_book.py --book_name "test_books/the_little_prince.txt" --test --batch_size 30 --test_num 20 --model google
|
|
||||||
|
|
||||||
- name: make caiyun translator test
|
|
||||||
if: env.BBM_CAIYUN_API_KEY != null
|
|
||||||
run: |
|
|
||||||
python3 make_book.py --book_name "test_books/the_little_prince.txt" --test --batch_size 30 --test_num 100 --model caiyun
|
|
||||||
|
|
||||||
- name: make deepl translator test
|
|
||||||
if: env.BBM_CAIYUN_API_KEY != null
|
|
||||||
run: |
|
|
||||||
python3 make_book.py --book_name "test_books/the_little_prince.txt" --test --batch_size 30 --test_num 20 --model deepl
|
|
||||||
python3 make_book.py --book_name test_books/Lex_Fridman_episode_322.srt --test
|
|
||||||
|
|
||||||
- name: make openai key ebook test
|
|
||||||
if: env.BBM_DEEPL_API_KEY != null
|
|
||||||
run: |
|
|
||||||
python3 make_book.py --book_name "test_books/lemo.epub" --test --test_num 5 --language zh-hans
|
|
||||||
python3 make_book.py --book_name "test_books/animal_farm.epub" --test --test_num 5 --language ja --model gpt3 --prompt prompt_template_sample.txt
|
|
||||||
python3 make_book.py --book_name "test_books/animal_farm.epub" --test --test_num 5 --language ja --prompt prompt_template_sample.json
|
|
||||||
python3 make_book.py --book_name test_books/Lex_Fridman_episode_322.srt --test --test_num 20
|
|
||||||
|
|
||||||
- name: Rename and Upload ePub
|
|
||||||
if: env.OPENAI_API_KEY != null
|
|
||||||
uses: actions/upload-artifact@v2
|
|
||||||
with:
|
|
||||||
name: epub_output
|
|
||||||
path: "test_books/lemo_bilingual.epub"
|
|
||||||
|
|
||||||
|
10
Makefile
Normal file
10
Makefile
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
SHELL := /bin/bash
|
||||||
|
|
||||||
|
fmt:
|
||||||
|
@echo "Running formatter ..."
|
||||||
|
venv/bin/black .
|
||||||
|
|
||||||
|
.PHONY:tests
|
||||||
|
tests:
|
||||||
|
@echo "Running tests ..."
|
||||||
|
venv/bin/pytest tests/test_integration.py
|
20
setup.py
20
setup.py
@ -1,6 +1,16 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
from setuptools import find_packages, setup
|
from setuptools import find_packages, setup
|
||||||
|
|
||||||
|
|
||||||
|
def get_required_packges():
|
||||||
|
packages = []
|
||||||
|
with open("requirements.txt") as filep:
|
||||||
|
for line in filep:
|
||||||
|
packages.append(line.rstrip())
|
||||||
|
|
||||||
|
return packages
|
||||||
|
|
||||||
|
|
||||||
setup(
|
setup(
|
||||||
name="bbook_maker",
|
name="bbook_maker",
|
||||||
description="The bilingual_book_maker is an AI translation tool that uses ChatGPT to assist users in creating multi-language versions of epub/txt files and books.",
|
description="The bilingual_book_maker is an AI translation tool that uses ChatGPT to assist users in creating multi-language versions of epub/txt files and books.",
|
||||||
@ -11,15 +21,7 @@ setup(
|
|||||||
packages=find_packages(),
|
packages=find_packages(),
|
||||||
url="https://github.com/yihong0618/bilingual_book_maker",
|
url="https://github.com/yihong0618/bilingual_book_maker",
|
||||||
python_requires=">=3.7",
|
python_requires=">=3.7",
|
||||||
install_requires=[
|
install_requires=get_required_packges(),
|
||||||
"bs4",
|
|
||||||
"openai",
|
|
||||||
"requests",
|
|
||||||
"ebooklib",
|
|
||||||
"rich",
|
|
||||||
"tqdm",
|
|
||||||
"tiktoken",
|
|
||||||
],
|
|
||||||
classifiers=[
|
classifiers=[
|
||||||
"Programming Language :: Python :: 3",
|
"Programming Language :: Python :: 3",
|
||||||
"License :: OSI Approved :: MIT License",
|
"License :: OSI Approved :: MIT License",
|
||||||
|
312
tests/test_integration.py
Normal file
312
tests/test_integration.py
Normal file
@ -0,0 +1,312 @@
|
|||||||
|
import os
|
||||||
|
import shutil
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture()
|
||||||
|
def test_book_dir() -> str:
|
||||||
|
"""Return test book dir"""
|
||||||
|
# TODO: Can move this to conftest.py if there will be more unittests
|
||||||
|
return str(Path(__file__).parent.parent / "test_books")
|
||||||
|
|
||||||
|
|
||||||
|
def test_google_translate_epub(test_book_dir, tmpdir):
|
||||||
|
"""Test google translate epub"""
|
||||||
|
shutil.copyfile(
|
||||||
|
os.path.join(test_book_dir, "Liber_Esther.epub"),
|
||||||
|
os.path.join(tmpdir, "Liber_Esther.epub"),
|
||||||
|
)
|
||||||
|
|
||||||
|
subprocess.run(
|
||||||
|
[
|
||||||
|
sys.executable,
|
||||||
|
"make_book.py",
|
||||||
|
"--book_name",
|
||||||
|
os.path.join(tmpdir, "Liber_Esther.epub"),
|
||||||
|
"--test",
|
||||||
|
"--test_num",
|
||||||
|
"20",
|
||||||
|
"--model",
|
||||||
|
"google",
|
||||||
|
],
|
||||||
|
env=os.environ.copy(),
|
||||||
|
)
|
||||||
|
|
||||||
|
assert os.path.isfile(os.path.join(tmpdir, "Liber_Esther_bilingual.epub"))
|
||||||
|
assert os.path.getsize(os.path.join(tmpdir, "Liber_Esther_bilingual.epub")) != 0
|
||||||
|
|
||||||
|
|
||||||
|
def test_google_translate_epub_cli():
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def test_google_translate_txt(test_book_dir, tmpdir):
|
||||||
|
"""Test google translate txt"""
|
||||||
|
shutil.copyfile(
|
||||||
|
os.path.join(test_book_dir, "the_little_prince.txt"),
|
||||||
|
os.path.join(tmpdir, "the_little_prince.txt"),
|
||||||
|
)
|
||||||
|
|
||||||
|
subprocess.run(
|
||||||
|
[
|
||||||
|
sys.executable,
|
||||||
|
"make_book.py",
|
||||||
|
"--book_name",
|
||||||
|
os.path.join(tmpdir, "the_little_prince.txt"),
|
||||||
|
"--test",
|
||||||
|
"--test_num",
|
||||||
|
"20",
|
||||||
|
"--model",
|
||||||
|
"google",
|
||||||
|
],
|
||||||
|
env=os.environ.copy(),
|
||||||
|
)
|
||||||
|
assert os.path.isfile(os.path.join(tmpdir, "the_little_prince_bilingual.txt"))
|
||||||
|
assert os.path.getsize(os.path.join(tmpdir, "the_little_prince_bilingual.txt")) != 0
|
||||||
|
|
||||||
|
|
||||||
|
def test_google_translate_txt_batch_size(test_book_dir, tmpdir):
|
||||||
|
"""Test google translate txt with batch_size"""
|
||||||
|
shutil.copyfile(
|
||||||
|
os.path.join(test_book_dir, "the_little_prince.txt"),
|
||||||
|
os.path.join(tmpdir, "the_little_prince.txt"),
|
||||||
|
)
|
||||||
|
|
||||||
|
subprocess.run(
|
||||||
|
[
|
||||||
|
sys.executable,
|
||||||
|
"make_book.py",
|
||||||
|
"--book_name",
|
||||||
|
os.path.join(tmpdir, "the_little_prince.txt"),
|
||||||
|
"--test",
|
||||||
|
"--batch_size",
|
||||||
|
"30",
|
||||||
|
"--test_num",
|
||||||
|
"20",
|
||||||
|
"--model",
|
||||||
|
"google",
|
||||||
|
],
|
||||||
|
env=os.environ.copy(),
|
||||||
|
)
|
||||||
|
|
||||||
|
assert os.path.isfile(os.path.join(tmpdir, "the_little_prince_bilingual.txt"))
|
||||||
|
assert os.path.getsize(os.path.join(tmpdir, "the_little_prince_bilingual.txt")) != 0
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.skipif(
|
||||||
|
not os.environ.get("BBM_CAIYUN_API_KEY"),
|
||||||
|
reason="No BBM_CAIYUN_API_KEY in environment variable.",
|
||||||
|
)
|
||||||
|
def test_caiyun_translate_txt(test_book_dir, tmpdir):
|
||||||
|
"""Test caiyun tranlate txt"""
|
||||||
|
shutil.copyfile(
|
||||||
|
os.path.join(test_book_dir, "the_little_prince.txt"),
|
||||||
|
os.path.join(tmpdir, "the_little_prince.txt"),
|
||||||
|
)
|
||||||
|
subprocess.run(
|
||||||
|
[
|
||||||
|
sys.executable,
|
||||||
|
"make_book.py",
|
||||||
|
"--book_name",
|
||||||
|
os.path.join(tmpdir, "the_little_prince.txt"),
|
||||||
|
"--test",
|
||||||
|
"--batch_size",
|
||||||
|
"30",
|
||||||
|
"--test_num",
|
||||||
|
"100",
|
||||||
|
"--model",
|
||||||
|
"caiyun",
|
||||||
|
],
|
||||||
|
env=os.environ.copy(),
|
||||||
|
)
|
||||||
|
|
||||||
|
assert os.path.isfile(os.path.join(tmpdir, "the_little_prince_bilingual.txt"))
|
||||||
|
assert os.path.getsize(os.path.join(tmpdir, "the_little_prince_bilingual.txt")) != 0
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.skipif(
|
||||||
|
not os.environ.get("BBM_DEEPL_API_KEY"),
|
||||||
|
reason="No BBM_DEEPL_API_KEY in environment variable.",
|
||||||
|
)
|
||||||
|
def test_deepl_translate_txt(test_book_dir, tmpdir):
|
||||||
|
shutil.copyfile(
|
||||||
|
os.path.join(test_book_dir, "the_little_prince.txt"),
|
||||||
|
os.path.join(tmpdir, "the_little_prince.txt"),
|
||||||
|
)
|
||||||
|
|
||||||
|
subprocess.run(
|
||||||
|
[
|
||||||
|
sys.executable,
|
||||||
|
"make_book.py",
|
||||||
|
"--book_name",
|
||||||
|
os.path.join(tmpdir, "the_little_prince.txt"),
|
||||||
|
"--test",
|
||||||
|
"--batch_size",
|
||||||
|
"30",
|
||||||
|
"--test_num",
|
||||||
|
"20",
|
||||||
|
"--model",
|
||||||
|
"deepl",
|
||||||
|
],
|
||||||
|
env=os.environ.copy(),
|
||||||
|
)
|
||||||
|
|
||||||
|
assert os.path.isfile(os.path.join(tmpdir, "the_little_prince_bilingual.txt"))
|
||||||
|
assert os.path.getsize(os.path.join(tmpdir, "the_little_prince_bilingual.txt")) != 0
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.skipif(
|
||||||
|
not os.environ.get("BBM_DEEPL_API_KEY"),
|
||||||
|
reason="No BBM_DEEPL_API_KEY in environment variable.",
|
||||||
|
)
|
||||||
|
def test_deepl_translate_srt(test_book_dir, tmpdir):
|
||||||
|
shutil.copyfile(
|
||||||
|
os.path.join(test_book_dir, "Lex_Fridman_episode_322.srt"),
|
||||||
|
os.path.join(tmpdir, "Lex_Fridman_episode_322.srt"),
|
||||||
|
)
|
||||||
|
|
||||||
|
subprocess.run(
|
||||||
|
[
|
||||||
|
sys.executable,
|
||||||
|
"make_book.py",
|
||||||
|
"--book_name",
|
||||||
|
os.path.join(tmpdir, "Lex_Fridman_episode_322.srt"),
|
||||||
|
"--test",
|
||||||
|
"--batch_size",
|
||||||
|
"30",
|
||||||
|
"--test_num",
|
||||||
|
"20",
|
||||||
|
"--model",
|
||||||
|
"deepl",
|
||||||
|
],
|
||||||
|
env=os.environ.copy(),
|
||||||
|
)
|
||||||
|
|
||||||
|
assert os.path.isfile(os.path.join(tmpdir, "Lex_Fridman_episode_322_bilingual.srt"))
|
||||||
|
assert (
|
||||||
|
os.path.getsize(os.path.join(tmpdir, "Lex_Fridman_episode_322_bilingual.srt"))
|
||||||
|
!= 0
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.skipif(
|
||||||
|
not os.environ.get("OPENAI_API_KEY"),
|
||||||
|
reason="No OPENAI_API_KEY in environment variable.",
|
||||||
|
)
|
||||||
|
def test_openai_translate_epub_zh_hans(test_book_dir, tmpdir):
|
||||||
|
shutil.copyfile(
|
||||||
|
os.path.join(test_book_dir, "lemo.epub"),
|
||||||
|
os.path.join(tmpdir, "lemo.epub"),
|
||||||
|
)
|
||||||
|
|
||||||
|
subprocess.run(
|
||||||
|
[
|
||||||
|
sys.executable,
|
||||||
|
"make_book.py",
|
||||||
|
"--book_name",
|
||||||
|
os.path.join(tmpdir, "lemo.epub"),
|
||||||
|
"--test",
|
||||||
|
"--test_num",
|
||||||
|
"5",
|
||||||
|
"--language",
|
||||||
|
"zh-hans",
|
||||||
|
],
|
||||||
|
env=os.environ.copy(),
|
||||||
|
)
|
||||||
|
assert os.path.isfile(os.path.join(tmpdir, "lemo_bilingual.epub"))
|
||||||
|
assert os.path.getsize(os.path.join(tmpdir, "lemo_bilingual.epub")) != 0
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.skipif(
|
||||||
|
not os.environ.get("OPENAI_API_KEY"),
|
||||||
|
reason="No OPENAI_API_KEY in environment variable.",
|
||||||
|
)
|
||||||
|
def test_openai_translate_epub_ja_prompt_txt(test_book_dir, tmpdir):
|
||||||
|
shutil.copyfile(
|
||||||
|
os.path.join(test_book_dir, "animal_farm.epub"),
|
||||||
|
os.path.join(tmpdir, "animal_farm.epub"),
|
||||||
|
)
|
||||||
|
|
||||||
|
subprocess.run(
|
||||||
|
[
|
||||||
|
sys.executable,
|
||||||
|
"make_book.py",
|
||||||
|
"--book_name",
|
||||||
|
os.path.join(tmpdir, "animal_farm.epub"),
|
||||||
|
"--test",
|
||||||
|
"--test_num",
|
||||||
|
"5",
|
||||||
|
"--language",
|
||||||
|
"ja",
|
||||||
|
"--model",
|
||||||
|
"gpt3",
|
||||||
|
"--prompt",
|
||||||
|
"prompt_template_sample.txt",
|
||||||
|
],
|
||||||
|
env=os.environ.copy(),
|
||||||
|
)
|
||||||
|
assert os.path.isfile(os.path.join(tmpdir, "animal_farm_bilingual.epub"))
|
||||||
|
assert os.path.getsize(os.path.join(tmpdir, "animal_farm_bilingual.epub")) != 0
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.skipif(
|
||||||
|
not os.environ.get("OPENAI_API_KEY"),
|
||||||
|
reason="No OPENAI_API_KEY in environment variable.",
|
||||||
|
)
|
||||||
|
def test_openai_translate_epub_ja_prompt_json(test_book_dir, tmpdir):
|
||||||
|
shutil.copyfile(
|
||||||
|
os.path.join(test_book_dir, "animal_farm.epub"),
|
||||||
|
os.path.join(tmpdir, "animal_farm.epub"),
|
||||||
|
)
|
||||||
|
|
||||||
|
subprocess.run(
|
||||||
|
[
|
||||||
|
sys.executable,
|
||||||
|
"make_book.py",
|
||||||
|
"--book_name",
|
||||||
|
os.path.join(tmpdir, "animal_farm.epub"),
|
||||||
|
"--test",
|
||||||
|
"--test_num",
|
||||||
|
"5",
|
||||||
|
"--language",
|
||||||
|
"ja",
|
||||||
|
"--prompt",
|
||||||
|
"prompt_template_sample.json",
|
||||||
|
],
|
||||||
|
env=os.environ.copy(),
|
||||||
|
)
|
||||||
|
assert os.path.isfile(os.path.join(tmpdir, "animal_farm_bilingual.epub"))
|
||||||
|
assert os.path.getsize(os.path.join(tmpdir, "animal_farm_bilingual.epub")) != 0
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.skipif(
|
||||||
|
not os.environ.get("OPENAI_API_KEY"),
|
||||||
|
reason="No OPENAI_API_KEY in environment variable.",
|
||||||
|
)
|
||||||
|
def test_openai_translate_srt(test_book_dir, tmpdir):
|
||||||
|
shutil.copyfile(
|
||||||
|
os.path.join(test_book_dir, "Lex_Fridman_episode_322.srt"),
|
||||||
|
os.path.join(tmpdir, "Lex_Fridman_episode_322.srt"),
|
||||||
|
)
|
||||||
|
|
||||||
|
subprocess.run(
|
||||||
|
[
|
||||||
|
sys.executable,
|
||||||
|
"make_book.py",
|
||||||
|
"--book_name",
|
||||||
|
os.path.join("Lex_Fridman_episode_322.srt"),
|
||||||
|
"--test",
|
||||||
|
"--test_num",
|
||||||
|
"20",
|
||||||
|
],
|
||||||
|
env=os.environ.copy(),
|
||||||
|
)
|
||||||
|
assert os.path.isfile(os.path.join(tmpdir, "Lex_Fridman_episode_322_bilingual.srt"))
|
||||||
|
assert (
|
||||||
|
os.path.getsize(os.path.join(tmpdir, "Lex_Fridman_episode_322_bilingual.srt"))
|
||||||
|
!= 0
|
||||||
|
)
|
Loading…
x
Reference in New Issue
Block a user