From 064e95b46b10664731e27435f1612396f045a788 Mon Sep 17 00:00:00 2001 From: k-boikov <64261260+k-boikov@users.noreply.github.com> Date: Sat, 27 May 2023 00:01:34 +0300 Subject: [PATCH] Allow spinning to be disabled (#4329) --- .env.template | 4 ++++ autogpt/agent/agent.py | 2 +- autogpt/commands/file_operations.py | 2 +- autogpt/config/config.py | 1 + autogpt/spinner.py | 29 ++++++++++++++++++++--------- tests/integration/agent_factory.py | 3 +++ 6 files changed, 30 insertions(+), 11 deletions(-) diff --git a/.env.template b/.env.template index 4c079e00c..dd690308d 100644 --- a/.env.template +++ b/.env.template @@ -21,6 +21,10 @@ ## EXIT_KEY - Key to exit AUTO-GPT # EXIT_KEY=n +## PLAIN_OUTPUT - Enabeling plain output will disable spinner (Default: False) +## Note: Spinner is used to indicate that Auto-GPT is working on something in the background +# PLAIN_OUTPUT=False + ## DISABLED_COMMAND_CATEGORIES - The list of categories of commands that are disabled. Each of the below are an option: ## autogpt.commands.analyze_code ## autogpt.commands.audio_text diff --git a/autogpt/agent/agent.py b/autogpt/agent/agent.py index 93d31853c..a1673ad9b 100644 --- a/autogpt/agent/agent.py +++ b/autogpt/agent/agent.py @@ -126,7 +126,7 @@ class Agent: ) break # Send message to AI, get response - with Spinner("Thinking... "): + with Spinner("Thinking... ", plain_output=cfg.plain_output): assistant_reply = chat_with_ai( self, self.system_prompt, diff --git a/autogpt/commands/file_operations.py b/autogpt/commands/file_operations.py index 7205b3025..8288e3898 100644 --- a/autogpt/commands/file_operations.py +++ b/autogpt/commands/file_operations.py @@ -318,7 +318,7 @@ def download_file(url, filename, config: Config): directory = os.path.dirname(filename) os.makedirs(directory, exist_ok=True) message = f"{Fore.YELLOW}Downloading file from {Back.LIGHTBLUE_EX}{url}{Back.RESET}{Fore.RESET}" - with Spinner(message) as spinner: + with Spinner(message, plain_output=config.plain_output) as spinner: session = requests.Session() retry = Retry(total=3, backoff_factor=1, status_forcelist=[502, 503, 504]) adapter = HTTPAdapter(max_retries=retry) diff --git a/autogpt/config/config.py b/autogpt/config/config.py index 94255333c..1e61b8085 100644 --- a/autogpt/config/config.py +++ b/autogpt/config/config.py @@ -30,6 +30,7 @@ class Config(metaclass=Singleton): self.authorise_key = os.getenv("AUTHORISE_COMMAND_KEY", "y") self.exit_key = os.getenv("EXIT_KEY", "n") + self.plain_output = os.getenv("PLAIN_OUTPUT", "False") == "True" disabled_command_categories = os.getenv("DISABLED_COMMAND_CATEGORIES") if disabled_command_categories: diff --git a/autogpt/spinner.py b/autogpt/spinner.py index ed02eb44c..491e7e8d3 100644 --- a/autogpt/spinner.py +++ b/autogpt/spinner.py @@ -8,13 +8,20 @@ import time class Spinner: """A simple spinner class""" - def __init__(self, message: str = "Loading...", delay: float = 0.1) -> None: + def __init__( + self, + message: str = "Loading...", + delay: float = 0.1, + plain_output: bool = False, + ) -> None: """Initialize the spinner class Args: message (str): The message to display. delay (float): The delay between each spinner update. + plain_output (bool): Whether to display the spinner or not. """ + self.plain_output = plain_output self.spinner = itertools.cycle(["-", "/", "|", "\\"]) self.delay = delay self.message = message @@ -23,11 +30,17 @@ class Spinner: def spin(self) -> None: """Spin the spinner""" + if self.plain_output: + self.print_message() + return while self.running: - sys.stdout.write(f"{next(self.spinner)} {self.message}\r") - sys.stdout.flush() + self.print_message() time.sleep(self.delay) - sys.stdout.write(f"\r{' ' * (len(self.message) + 2)}\r") + + def print_message(self): + sys.stdout.write(f"\r{' ' * (len(self.message) + 2)}\r") + sys.stdout.write(f"{next(self.spinner)} {self.message}\r") + sys.stdout.flush() def __enter__(self): """Start the spinner""" @@ -57,9 +70,7 @@ class Spinner: new_message (str): New message to display. delay (float): The delay in seconds between each spinner update. """ - time.sleep(delay) - sys.stdout.write( - f"\r{' ' * (len(self.message) + 2)}\r" - ) # Clear the current message - sys.stdout.flush() + self.delay = delay self.message = new_message + if self.plain_output: + self.print_message() diff --git a/tests/integration/agent_factory.py b/tests/integration/agent_factory.py index 19af503be..c2f2d4f63 100644 --- a/tests/integration/agent_factory.py +++ b/tests/integration/agent_factory.py @@ -12,11 +12,14 @@ from autogpt.workspace import Workspace def agent_test_config(config: Config): was_continuous_mode = config.continuous_mode was_temperature = config.temperature + was_plain_output = config.plain_output config.set_continuous_mode(False) config.set_temperature(0) + config.plain_output = True yield config config.set_continuous_mode(was_continuous_mode) config.set_temperature(was_temperature) + config.plain_output = was_plain_output @pytest.fixture