diff --git a/README.md b/README.md index 4780012..926a281 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/main.py b/main.py index 801feac..96dc180 100644 --- a/main.py +++ b/main.py @@ -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 @@ -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 @@ -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() @@ -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()