Spaces:
Sleeping
Sleeping
import os | |
import time | |
import random | |
import logging | |
from pathlib import Path | |
# Set up logging | |
logging.basicConfig(level=logging.INFO) | |
logger = logging.getLogger(__name__) | |
# Define the root directory for the application | |
ROOT_DIR = Path(__file__).parent | |
AGENTS_DIR = ROOT_DIR / "agents" | |
TOOLS_DIR = ROOT_DIR / "tools" | |
# Create directories if they do not exist | |
AGENTS_DIR.mkdir(exist_ok=True) | |
TOOLS_DIR.mkdir(exist_ok=True) | |
# Function to generate a new agent | |
def generate_agent(agent_name: str) -> str: | |
agent_code = f""" | |
class {agent_name}: | |
def __init__(self): | |
self.name = '{agent_name}' | |
def run(self): | |
print("Running {agent_name}...") | |
""" | |
agent_file_path = AGENTS_DIR / f"{agent_name}.py" | |
with open(agent_file_path, 'w') as f: | |
f.write(agent_code) | |
logger.info(f"Generated agent: {agent_name} at {agent_file_path}") | |
return agent_file_path | |
# Function to generate a new tool | |
def generate_tool(tool_name: str) -> str: | |
tool_code = f""" | |
def {tool_name}(): | |
print("Executing tool: {tool_name}") | |
""" | |
tool_file_path = TOOLS_DIR / f"{tool_name}.py" | |
with open(tool_file_path, 'w') as f: | |
f.write(tool_code) | |
logger.info(f"Generated tool: {tool_name} at {tool_file_path}") | |
return tool_file_path | |
# Main loop to continuously generate agents and tools | |
def main(): | |
while True: | |
# Randomly decide to generate an agent or a tool | |
if random.choice([True, False]): | |
agent_name = f"Agent{random.randint(1, 1000)}" | |
generate_agent(agent_name) | |
else: | |
tool_name = f"Tool{random.randint(1, 1000)}" | |
generate_tool(tool_name) | |
# Sleep for a while before generating the next agent/tool | |
time.sleep(5) # Adjust the sleep time as needed | |
if __name__ == "__main__": | |
main() |