Upload 7 files
Browse files- README.md +25 -23
- agent.py +19 -6
- agent_editor.py +12 -0
- app.py +88 -9
- runtime.txt +1 -0
- utils.py +49 -0
README.md
CHANGED
|
@@ -1,23 +1,25 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
-
|
| 15 |
-
-
|
| 16 |
-
-
|
| 17 |
-
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
# π€ Autonomous AI β Fully Self-Updating Python Agent
|
| 3 |
+
|
| 4 |
+
This is a powerful, self-improving autonomous agent capable of:
|
| 5 |
+
|
| 6 |
+
- Planning tasks
|
| 7 |
+
- Writing and executing Python code
|
| 8 |
+
- Debugging itself
|
| 9 |
+
- Storing memory and logs
|
| 10 |
+
- Growing over time
|
| 11 |
+
|
| 12 |
+
## Files
|
| 13 |
+
|
| 14 |
+
- `app.py`: Gradio UI
|
| 15 |
+
- `agent.py`: Core self-runner
|
| 16 |
+
- `utils.py`: Task planning, logging, memory
|
| 17 |
+
- `memory.txt`: Long-term task memory
|
| 18 |
+
- `logs/`: JSON logs of each run
|
| 19 |
+
|
| 20 |
+
## Usage
|
| 21 |
+
|
| 22 |
+
1. Upload to [Hugging Face Spaces](https://huggingface.co/spaces)
|
| 23 |
+
2. Set type to `Gradio`
|
| 24 |
+
3. Enjoy your AI developer assistant
|
| 25 |
+
|
agent.py
CHANGED
|
@@ -1,11 +1,24 @@
|
|
| 1 |
|
|
|
|
| 2 |
import traceback
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
| 5 |
try:
|
| 6 |
-
plan =
|
| 7 |
-
code =
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
except Exception as e:
|
| 11 |
-
|
|
|
|
|
|
|
|
|
| 1 |
|
| 2 |
+
import os
|
| 3 |
import traceback
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
from utils import save_log, read_memory, write_memory, plan_task, generate_code, run_code
|
| 6 |
|
| 7 |
+
MEMORY_FILE = "memory.txt"
|
| 8 |
+
LOG_DIR = "logs"
|
| 9 |
+
|
| 10 |
+
def autonomous_agent(task):
|
| 11 |
try:
|
| 12 |
+
plan = plan_task(task)
|
| 13 |
+
code = generate_code(task)
|
| 14 |
+
result = run_code(code)
|
| 15 |
+
|
| 16 |
+
# Save memory and logs
|
| 17 |
+
write_memory(f"{datetime.now()}: Completed task '{task}'")
|
| 18 |
+
save_log(task, plan, code, result)
|
| 19 |
+
|
| 20 |
+
return f"β
Task Complete:\nPlan: {plan}\n\nCode:\n{code}\n\nResult:\n{result}"
|
| 21 |
except Exception as e:
|
| 22 |
+
error = traceback.format_exc()
|
| 23 |
+
save_log(task, "FAILED", "None", error)
|
| 24 |
+
return f"β Task Failed:\n{error}"
|
agent_editor.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
def update_agent_code(new_code):
|
| 3 |
+
with open("agent.py", "w") as f:
|
| 4 |
+
f.write(new_code)
|
| 5 |
+
return "β
agent.py updated. Rebuild or reload your Space to apply changes."
|
| 6 |
+
|
| 7 |
+
def read_agent_code():
|
| 8 |
+
try:
|
| 9 |
+
with open("agent.py", "r") as f:
|
| 10 |
+
return f.read()
|
| 11 |
+
except FileNotFoundError:
|
| 12 |
+
return "# agent.py not found"
|
app.py
CHANGED
|
@@ -1,17 +1,96 @@
|
|
| 1 |
|
| 2 |
import gradio as gr
|
| 3 |
-
from agent import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
with gr.Blocks() as demo:
|
| 6 |
-
gr.Markdown("# π€ Autonomous AI
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
demo.launch()
|
|
|
|
| 1 |
|
| 2 |
import gradio as gr
|
| 3 |
+
from agent import autonomous_agent
|
| 4 |
+
from utils import read_memory, append_feedback
|
| 5 |
+
from agent_editor import update_agent_code, read_agent_code
|
| 6 |
+
import threading, time
|
| 7 |
+
|
| 8 |
+
current_task = ""
|
| 9 |
+
loop_running = False
|
| 10 |
+
|
| 11 |
+
def run_task(task):
|
| 12 |
+
global current_task
|
| 13 |
+
current_task = task
|
| 14 |
+
result = autonomous_agent(task)
|
| 15 |
+
return result, read_memory()
|
| 16 |
+
|
| 17 |
+
def update_memory(new_text):
|
| 18 |
+
with open("memory.txt", "w") as f:
|
| 19 |
+
f.write(new_text)
|
| 20 |
+
return "β
Memory updated."
|
| 21 |
+
|
| 22 |
+
def view_logs():
|
| 23 |
+
import os, json
|
| 24 |
+
log_entries = []
|
| 25 |
+
if not os.path.exists("logs"):
|
| 26 |
+
return "No logs yet."
|
| 27 |
+
for filename in sorted(os.listdir("logs"))[-5:]:
|
| 28 |
+
with open(f"logs/{filename}", "r") as f:
|
| 29 |
+
data = json.load(f)
|
| 30 |
+
log_entries.append(f"π {data['timestamp']}\nπ§ Task: {data['task']}\nβ
Result: {data['result'][:100]}...\n---")
|
| 31 |
+
return "\n\n".join(log_entries) if log_entries else "No logs found."
|
| 32 |
+
|
| 33 |
+
def store_feedback(score):
|
| 34 |
+
append_feedback(current_task, "See logs for result", score)
|
| 35 |
+
return "β
Feedback stored."
|
| 36 |
+
|
| 37 |
+
def toggle_loop(toggle: bool, interval: int = 30):
|
| 38 |
+
global loop_running
|
| 39 |
+
loop_running = toggle
|
| 40 |
+
if toggle:
|
| 41 |
+
thread = threading.Thread(target=loop_runner, args=(interval,), daemon=True)
|
| 42 |
+
thread.start()
|
| 43 |
+
return "π Loop started."
|
| 44 |
+
else:
|
| 45 |
+
return "βΉοΈ Loop stopped."
|
| 46 |
+
|
| 47 |
+
def loop_runner(interval):
|
| 48 |
+
global loop_running
|
| 49 |
+
while loop_running:
|
| 50 |
+
run_task("Autonomous self-improvement")
|
| 51 |
+
time.sleep(interval)
|
| 52 |
|
| 53 |
with gr.Blocks() as demo:
|
| 54 |
+
gr.Markdown("# π€ Autonomous AI Agent β Enhanced")
|
| 55 |
+
|
| 56 |
+
with gr.Tab("π¬ Task Runner"):
|
| 57 |
+
chatbot = gr.Textbox(lines=2, label="What should the AI do?")
|
| 58 |
+
submit = gr.Button("Execute")
|
| 59 |
+
output = gr.Textbox(label="Result")
|
| 60 |
+
memory_out = gr.Textbox(label="Memory Snapshot", lines=6)
|
| 61 |
+
submit.click(run_task, inputs=chatbot, outputs=[output, memory_out])
|
| 62 |
+
|
| 63 |
+
with gr.Tab("π§ Memory Editor"):
|
| 64 |
+
mem_editor = gr.Textbox(label="Edit memory.txt", lines=10)
|
| 65 |
+
save_mem = gr.Button("Save Changes")
|
| 66 |
+
result = gr.Textbox(label="Status")
|
| 67 |
+
save_mem.click(update_memory, inputs=mem_editor, outputs=result)
|
| 68 |
+
demo.load(read_memory, inputs=[], outputs=mem_editor)
|
| 69 |
+
|
| 70 |
+
with gr.Tab("π Recent Logs"):
|
| 71 |
+
log_output = gr.Textbox(label="Last 5 Logs", lines=20)
|
| 72 |
+
refresh_logs = gr.Button("Refresh")
|
| 73 |
+
refresh_logs.click(view_logs, outputs=log_output)
|
| 74 |
+
demo.load(view_logs, outputs=log_output)
|
| 75 |
+
|
| 76 |
+
with gr.Tab("β Feedback"):
|
| 77 |
+
gr.Markdown("Rate the last task:")
|
| 78 |
+
feedback_slider = gr.Slider(0, 10, step=0.5, label="Rating")
|
| 79 |
+
submit_fb = gr.Button("Submit Feedback")
|
| 80 |
+
fb_result = gr.Textbox(label="Status")
|
| 81 |
+
submit_fb.click(store_feedback, inputs=feedback_slider, outputs=fb_result)
|
| 82 |
|
| 83 |
+
with gr.Tab("π Loop Mode"):
|
| 84 |
+
interval = gr.Slider(10, 300, step=10, label="Loop interval (seconds)", value=60)
|
| 85 |
+
toggle = gr.Checkbox(label="Enable Autonomous Loop")
|
| 86 |
+
loop_status = gr.Textbox(label="Loop Status")
|
| 87 |
+
toggle.change(fn=toggle_loop, inputs=[toggle, interval], outputs=loop_status)
|
| 88 |
|
| 89 |
+
with gr.Tab("𧬠Agent Code Editor"):
|
| 90 |
+
code_box = gr.Code(label="Edit agent.py")
|
| 91 |
+
code_status = gr.Textbox(label="Update Result")
|
| 92 |
+
code_save = gr.Button("Save agent.py")
|
| 93 |
+
demo.load(read_agent_code, outputs=code_box)
|
| 94 |
+
code_save.click(update_agent_code, inputs=code_box, outputs=code_status)
|
| 95 |
|
| 96 |
demo.launch()
|
runtime.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
python-3.10
|
utils.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import os
|
| 3 |
+
import json
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
|
| 6 |
+
def save_log(task, plan, code, result):
|
| 7 |
+
os.makedirs("logs", exist_ok=True)
|
| 8 |
+
log_data = {
|
| 9 |
+
"timestamp": str(datetime.now()),
|
| 10 |
+
"task": task,
|
| 11 |
+
"plan": plan,
|
| 12 |
+
"code": code,
|
| 13 |
+
"result": result
|
| 14 |
+
}
|
| 15 |
+
with open(f"logs/{datetime.now().strftime('%Y%m%d_%H%M%S')}.json", "w") as f:
|
| 16 |
+
json.dump(log_data, f, indent=2)
|
| 17 |
+
|
| 18 |
+
def read_memory():
|
| 19 |
+
if not os.path.exists("memory.txt"):
|
| 20 |
+
return ""
|
| 21 |
+
with open("memory.txt", "r") as f:
|
| 22 |
+
return f.read()
|
| 23 |
+
|
| 24 |
+
def write_memory(entry):
|
| 25 |
+
with open("memory.txt", "a") as f:
|
| 26 |
+
f.write(entry + "\n")
|
| 27 |
+
|
| 28 |
+
def plan_task(task):
|
| 29 |
+
return f"Break the task into steps and generate executable code to achieve: '{task}'"
|
| 30 |
+
|
| 31 |
+
def generate_code(task):
|
| 32 |
+
return f"# Auto-generated code to {task}\nprint('Completed task: {task}')"
|
| 33 |
+
|
| 34 |
+
def run_code(code):
|
| 35 |
+
local_vars = {}
|
| 36 |
+
exec(code, {}, local_vars)
|
| 37 |
+
return "Code executed successfully."
|
| 38 |
+
|
| 39 |
+
def append_feedback(task, result, feedback_score):
|
| 40 |
+
import json, os, time
|
| 41 |
+
feedback_entry = {
|
| 42 |
+
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S"),
|
| 43 |
+
"task": task,
|
| 44 |
+
"result": result,
|
| 45 |
+
"feedback": feedback_score
|
| 46 |
+
}
|
| 47 |
+
os.makedirs("feedback", exist_ok=True)
|
| 48 |
+
with open(f"feedback/{int(time.time())}.json", "w") as f:
|
| 49 |
+
json.dump(feedback_entry, f, indent=2)
|