From 742c9f8b8fec42bbdb1c4633fa46d9805c30a9d4 Mon Sep 17 00:00:00 2001 From: Dylan Hogg Date: Wed, 31 Jan 2024 15:41:57 +1100 Subject: [PATCH] Enable a no-user-input mode for automation. --- Makefile | 4 ++++ gptauthor/console.py | 2 ++ gptauthor/library/engine.py | 27 ++++++++++++++++----------- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index bfa767b..be673b3 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,10 @@ run: # Executes the given command inside the virtualenv poetry run gptauthor --story openai-drama --total-chapters 3 --llm-model gpt-3.5-turbo --llm-temperature 0.1 --llm-top-p 1.0 +run-no-allow-user-input: + # Executes the given command inside the virtualenv + poetry run gptauthor --story openai-drama --total-chapters 3 --llm-model gpt-3.5-turbo --llm-temperature 0.1 --llm-top-p 1.0 --no-allow-user-input + build: # Build the source and wheels archives poetry build diff --git a/gptauthor/console.py b/gptauthor/console.py index 3be016b..6b8c1c3 100644 --- a/gptauthor/console.py +++ b/gptauthor/console.py @@ -32,6 +32,7 @@ def run( int, typer.Option(help="LLM use localhost:8081 instead of openai") ] = consts.default_llm_use_localhost, total_chapters: Annotated[int, typer.Option(help="Total chapters to write")] = consts.default_write_total_chapters, + allow_user_input: Annotated[bool, typer.Option(help="Allow command line user input")] = True, version: Annotated[ Optional[bool], typer.Option("--version", help=f"Display {consts.package_name} version", callback=version_callback), @@ -66,6 +67,7 @@ def run( "localhost_sleep": int(env.get("LLM_USE_LOCALHOST_SLEEP", 0)), "default_output_folder": consts.default_output_folder, "story_file": story_file, + "allow_user_input": allow_user_input, } ) diff --git a/gptauthor/library/engine.py b/gptauthor/library/engine.py index 39aa283..c27c158 100644 --- a/gptauthor/library/engine.py +++ b/gptauthor/library/engine.py @@ -42,7 +42,9 @@ def user_input_continue_processing(synopsis_response_user_edited_filename): def do_writing(llm_config): start = time.time() - p(f"Start {consts.package_name} {consts.version}, {llm_config.total_chapters=}, {llm_config.story_file=}...") + p( + f"Start {consts.package_name} {consts.version}, {llm_config.total_chapters=}, {llm_config.story_file=}, {llm_config.allow_user_input=}..." + ) # ------------------------------------------------------------------------------ # Create synopsis @@ -111,7 +113,9 @@ def do_writing(llm_config): # ------------------------------------------------------------------------------ # User input to continue or quit # ------------------------------------------------------------------------------ - if not user_input_continue_processing(str(output_folder / synopsis_response_user_edited_filename)): + if llm_config.allow_user_input and not user_input_continue_processing( + str(output_folder / synopsis_response_user_edited_filename) + ): with open(output_folder / "__aborted.txt", "w") as f: f.write(f"Aborted writing book: {synopsis_title}.\n\n") f.write(str(safe_llm_config)) @@ -242,12 +246,13 @@ def do_writing(llm_config): f"\n{took=:.2f}s, {total_process_tokens=:,}, ${rough_gpt4_8k_price_estimate=:.2f}, ${rough_gpt35_4k_price_estimate=:.2f}" ) - while True: - user_input = input("Press 'Y' to open the html book in a browser, or Enter/Return to finish: ") - if user_input.lower() == "y": - abs_file = (output_folder / "_whole_book.html").resolve() - webbrowser.open(f"file://{abs_file}") - elif user_input.lower() == "": - return - else: - print("Invalid input. Please try again.") + if llm_config.allow_user_input: + while True: + user_input = input("Press 'Y' to open the html book in a browser, or Enter/Return to finish: ") + if user_input.lower() == "y": + abs_file = (output_folder / "_whole_book.html").resolve() + webbrowser.open(f"file://{abs_file}") + elif user_input.lower() == "": + return + else: + print("Invalid input. Please try again.")