File size: 2,622 Bytes
672cd1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import os
import re
import requests
from tqdm import tqdm

def ask(prompt: str) -> str:
    url = "http://text.pollinations.ai/"
    fullurl = url + prompt.replace(" ", "%20")
    response = requests.get(fullurl)
    return response.text

if not os.path.exists("agent"):
    os.makedirs("agent")

def save_to_file(filename, prompt, context=""):
    filename = os.path.join('agent', filename)
    print("Creating file:", filename)
    full_prompt = context + prompt if context else prompt
    print(f"Saving to file: {filename}\nPrompt:\n{full_prompt}\n")
    with open(filename, "w", encoding="utf-8") as file:
        file.write(full_prompt)

def create_image(filename, prompt):
    print(f"Creating image: {filename}")
    prompt = re.sub(r'\s+', ' ', prompt).strip()
    url = f"https://pollinations.ai/p/{prompt}?seed=2025&nologo=true&model=flux"
    response = requests.get(url)
    with open(os.path.join("agent", filename), "wb") as f:
        f.write(response.content)

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}")

def run_agent(task):
    system_prompt = """

You are an AI agent, your response must be in python code, using only the following functions:

save_to_file(filename, prompt, context)
create_image(filename, prompt)

save_to_file takes a filename and a prompt, passes it to the LLM, and saves the result. If context is given, it will be loaded and prepended to the prompt. Do not use context on the first file.
Your responses should be python code that calls these functions, do not use any other functions or libraries. make sure to make manifest.json, index.html, style.css, app.js, and logo.png files for web apps, and use the create_image function to create images.
the prompt in create_image should be a short description of the image you want to create.
You may also create readme.md files, .py files, .c files, and any other files apropriate for the task.
Your task is:
"""
    full_prompt = system_prompt + task
    print("\nSending task to agent...\n")
    response = ask(full_prompt)
    parse_and_run(response)

if __name__ == "__main__":
    print("What task shall I perform, sir?")
    task_input = input("> ").strip()
    if task_input:
        run_agent(task_input)
        print("\nAgent has completed the task.")
    else:
        print("No task provided, sir.")