| import os | |
| import re | |
| import requests | |
| from tqdm import tqdm | |
| from model import ask | |
| from agentgen import create_image | |
| from tools import * | |
| if not os.path.exists("agent"): | |
| os.makedirs("agent") | |
| def save_to_file(filename, file_content): | |
| if not filename.startswith("agent/"): | |
| filename = os.path.join("agent", filename) | |
| os.makedirs(os.path.dirname(filename), exist_ok=True) | |
| print(f"Creating file: {filename}") | |
| with open(filename, "w", encoding="utf-8") as file: | |
| file.write(file_content) | |
| print(f"File {filename} created successfully.") | |
| def parse_and_run(response): | |
| code_blocks = re.findall(r'```python(.*?)```', response, re.DOTALL) | |
| for block in tqdm(code_blocks, desc="Executing Code Blocks", unit="block"): | |
| code = block.strip() | |
| try: | |
| exec(code, globals()) | |
| except Exception as e: | |
| print(f"\nError executing code block:\n{code}\n\nException: {e}") | |
| auto_detect_html_and_link_css() | |
| def run_agent(task): | |
| build_robotstxt_file() | |
| build_license_file() | |
| build_css_file() | |
| system_prompt = f""" | |
| You are an AI agent, your response must be in python code, using only the following functions: | |
| save_to_file(filename,file_content) | |
| create_image(filename, prompt) | |
| save_to_file() saves a file with the given filename and content. | |
| create_image() creates an image with the given filename and prompt. for example as prompt you can use "A beautiful landscape with mountains and a river", or any image you want. | |
| Based on this task: {task}, make all the files. These are the files you need to create: | |
| - A file named "index.html" with the HTML content. | |
| - A file named "style.css" with the CSS content. | |
| - A file named "script.js" with the JavaScript content or just console.log("app started") if no JS is needed. | |
| - An image file named "logo.png" with the logo image. | |
| - A README.md file with a description of the project and how to run it. | |
| - Any other files you deem necessary for the project. | |
| Your task is: | |
| {task} | |
| """ | |
| print("\nSending task to agent...\n") | |
| response = ask(system_prompt) | |
| parse_and_run(response) | |