Skip to content

Commit

Permalink
Allow spinning to be disabled (Significant-Gravitas#4329)
Browse files Browse the repository at this point in the history
  • Loading branch information
k-boikov authored May 26, 2023
1 parent e7c0d33 commit 064e95b
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 11 deletions.
4 changes: 4 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion autogpt/agent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def signal_handler(signum, frame):
)
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,
Expand Down
2 changes: 1 addition & 1 deletion autogpt/commands/file_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions autogpt/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def __init__(self) -> None:

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:
Expand Down
29 changes: 20 additions & 9 deletions autogpt/spinner.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@
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
Expand All @@ -23,11 +30,17 @@ def __init__(self, message: str = "Loading...", delay: float = 0.1) -> None:

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"""
Expand Down Expand Up @@ -57,9 +70,7 @@ def update_message(self, new_message, delay=0.1):
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()
3 changes: 3 additions & 0 deletions tests/integration/agent_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
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
Expand Down

0 comments on commit 064e95b

Please sign in to comment.