AutoGPT/autogpt/utils.py

78 lines
2.1 KiB
Python
Raw Normal View History

import os
2023-04-18 19:13:31 +00:00
import requests
2023-04-13 11:02:42 +00:00
import yaml
from colorama import Fore
from git import Repo
2023-04-13 11:02:42 +00:00
2023-04-13 22:23:23 +00:00
2023-04-14 19:42:28 +00:00
def clean_input(prompt: str = ""):
2023-04-10 00:53:32 +00:00
try:
return input(prompt)
except KeyboardInterrupt:
print("You interrupted Auto-GPT")
print("Quitting...")
2023-04-10 00:53:32 +00:00
exit(0)
2023-04-13 11:02:42 +00:00
def validate_yaml_file(file: str):
try:
with open(file, encoding="utf-8") as fp:
yaml.load(fp.read(), Loader=yaml.FullLoader)
2023-04-13 11:02:42 +00:00
except FileNotFoundError:
return (False, f"The file {Fore.CYAN}`{file}`{Fore.RESET} wasn't found")
except yaml.YAMLError as e:
2023-04-14 19:42:28 +00:00
return (
False,
f"There was an issue while trying to read with your AI Settings file: {e}",
)
2023-04-13 12:09:24 +00:00
return (True, f"Successfully validated {Fore.CYAN}`{file}`{Fore.RESET}!")
def readable_file_size(size, decimal_places=2):
"""Converts the given size in bytes to a readable format.
Args:
size: Size in bytes
decimal_places (int): Number of decimal places to display
"""
2023-04-17 20:47:38 +00:00
for unit in ["B", "KB", "MB", "GB", "TB"]:
if size < 1024.0:
break
size /= 1024.0
return f"{size:.{decimal_places}f} {unit}"
2023-04-18 19:11:26 +00:00
def get_bulletin_from_web() -> str:
try:
2023-04-18 19:11:26 +00:00
response = requests.get(
"https://raw.githubusercontent.com/Significant-Gravitas/Auto-GPT/master/BULLETIN.md"
2023-04-18 19:11:26 +00:00
)
if response.status_code == 200:
return response.text
except:
return ""
def get_current_git_branch() -> str:
try:
repo = Repo(search_parent_directories=True)
branch = repo.active_branch
return branch.name
except:
return ""
def get_latest_bulletin() -> str:
exists = os.path.exists("CURRENT_BULLETIN.md")
current_bulletin = ""
if exists:
current_bulletin = open("CURRENT_BULLETIN.md", "r", encoding="utf-8").read()
new_bulletin = get_bulletin_from_web()
is_new_news = new_bulletin != current_bulletin
if new_bulletin and is_new_news:
open("CURRENT_BULLETIN.md", "w", encoding="utf-8").write(new_bulletin)
return f" {Fore.RED}::UPDATED:: {Fore.CYAN}{new_bulletin}{Fore.RESET}"
return current_bulletin