Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support translating chapter beginnings #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ python main.py show-chapters --input yourbook.epub

This command will display all the chapters, helping you to plan your translation process effectively.

You can also translate the beginning of each chapter to you target language like so:
```bash
python main.py show-chapters --input yourbook.epub --config config.yaml --from-lang EN --to-lang PL
```

### Translate Mode

#### Basic Usage
Expand Down
22 changes: 20 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def translate(client, input_epub_path, output_epub_path, from_chapter=0, to_chap
epub.write_epub(output_epub_path, book, {})


def show_chapters(input_epub_path):
def show_chapters(input_epub_path, client=None, from_lang=None, to_lang=None):
book = epub.read_epub(input_epub_path)

current_chapter = 1
Expand All @@ -105,6 +105,11 @@ def show_chapters(input_epub_path):
soup = BeautifulSoup(item.content, 'html.parser')
chapter_beginning = soup.text[0:250]
chapter_beginning = re.sub(r'\n{2,}', '\n', chapter_beginning)

if client and from_lang and to_lang:
print("\tTranslating preview of chapter %d..." % current_chapter)
chapter_beginning = translate_text(client, chapter_beginning, from_lang, to_lang)

print(chapter_beginning + "\n\n")

current_chapter += 1
Expand All @@ -129,6 +134,9 @@ def show_chapters(input_epub_path):
# Create the parser for the "show-chapters" mode
parser_show = subparsers.add_parser('show-chapters', help='Show the list of chapters.')
parser_show.add_argument('--input', required=True, help='Input file path.')
parser_show.add_argument('--config', help='Configuration file path.')
parser_show.add_argument('--from-lang', help='Source language.')
parser_show.add_argument('--to-lang', help='Target language.')

# Parse the arguments
args = parser.parse_args()
Expand All @@ -145,7 +153,17 @@ def show_chapters(input_epub_path):
translate(openai_client, args.input, args.output, from_chapter, to_chapter, from_lang, to_lang)

elif args.mode == 'show-chapters':
show_chapters(args.input)
client = None
from_lang = None
to_lang = None

if args.config and args.from_lang and args.to_lang:
config = read_config(args.config)
client = OpenAI(api_key=config['openai']['api_key'])
from_lang = args.from_lang
to_lang = args.to_lang

show_chapters(args.input, client=client, from_lang=from_lang, to_lang=to_lang)

else:
parser.print_help()