Spaces:
Runtime error
Runtime error
Upload 9 files
Browse files- agent_editor.py +12 -0
- app.py +72 -5
- goal_manager.py +20 -0
- multi_agent.py +11 -0
- utils.py +12 -0
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,9 +1,16 @@
|
|
1 |
|
2 |
import gradio as gr
|
3 |
from agent import autonomous_agent
|
4 |
-
from utils import read_memory
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
def run_task(task):
|
|
|
|
|
7 |
result = autonomous_agent(task)
|
8 |
return result, read_memory()
|
9 |
|
@@ -17,28 +24,46 @@ def view_logs():
|
|
17 |
log_entries = []
|
18 |
if not os.path.exists("logs"):
|
19 |
return "No logs yet."
|
20 |
-
for filename in sorted(os.listdir("logs"))[-5:]:
|
21 |
with open(f"logs/{filename}", "r") as f:
|
22 |
data = json.load(f)
|
23 |
log_entries.append(f"π {data['timestamp']}\nπ§ Task: {data['task']}\nβ
Result: {data['result'][:100]}...\n---")
|
24 |
return "\n\n".join(log_entries) if log_entries else "No logs found."
|
25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
with gr.Blocks() as demo:
|
27 |
-
gr.Markdown("# π€ Autonomous AI Agent")
|
28 |
|
29 |
with gr.Tab("π¬ Task Runner"):
|
30 |
chatbot = gr.Textbox(lines=2, label="What should the AI do?")
|
31 |
submit = gr.Button("Execute")
|
32 |
output = gr.Textbox(label="Result")
|
33 |
memory_out = gr.Textbox(label="Memory Snapshot", lines=6)
|
34 |
-
|
35 |
submit.click(run_task, inputs=chatbot, outputs=[output, memory_out])
|
36 |
|
37 |
with gr.Tab("π§ Memory Editor"):
|
38 |
mem_editor = gr.Textbox(label="Edit memory.txt", lines=10)
|
39 |
save_mem = gr.Button("Save Changes")
|
40 |
result = gr.Textbox(label="Status")
|
41 |
-
|
42 |
save_mem.click(update_memory, inputs=mem_editor, outputs=result)
|
43 |
demo.load(read_memory, inputs=[], outputs=mem_editor)
|
44 |
|
@@ -48,4 +73,46 @@ with gr.Blocks() as demo:
|
|
48 |
refresh_logs.click(view_logs, outputs=log_output)
|
49 |
demo.load(view_logs, outputs=log_output)
|
50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
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 |
|
|
|
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 |
|
|
|
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()
|
97 |
+
|
98 |
+
import gradio as gr
|
99 |
+
from goal_manager import save_goal, list_goals
|
100 |
+
from multi_agent import multi_agent_brain
|
101 |
+
|
102 |
+
with demo:
|
103 |
+
with gr.Tab("π― Goal Manager"):
|
104 |
+
goal_input = gr.Textbox(label="Set a New Goal")
|
105 |
+
goal_status = gr.Textbox(label="Save Result")
|
106 |
+
show_goals = gr.Textbox(label="Latest Goals", lines=8)
|
107 |
+
save_button = gr.Button("Save Goal")
|
108 |
+
refresh_goals = gr.Button("Show Recent Goals")
|
109 |
+
|
110 |
+
save_button.click(fn=save_goal, inputs=goal_input, outputs=goal_status)
|
111 |
+
refresh_goals.click(fn=list_goals, outputs=show_goals)
|
112 |
+
demo.load(fn=list_goals, outputs=show_goals)
|
113 |
+
|
114 |
+
with gr.Tab("π€ Multi-Agent Collaboration"):
|
115 |
+
group_task = gr.Textbox(label="Shared Task")
|
116 |
+
run_group = gr.Button("Collaborate")
|
117 |
+
group_output = gr.Textbox(label="Agent Responses", lines=6)
|
118 |
+
run_group.click(fn=multi_agent_brain, inputs=group_task, outputs=group_output)
|
goal_manager.py
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import json, os
|
3 |
+
|
4 |
+
def save_goal(goal):
|
5 |
+
os.makedirs("goals", exist_ok=True)
|
6 |
+
goal_data = {
|
7 |
+
"goal": goal,
|
8 |
+
"status": "pending"
|
9 |
+
}
|
10 |
+
with open(f"goals/{int(time.time())}.json", "w") as f:
|
11 |
+
json.dump(goal_data, f)
|
12 |
+
|
13 |
+
def list_goals():
|
14 |
+
goal_files = sorted(os.listdir("goals")) if os.path.exists("goals") else []
|
15 |
+
goals = []
|
16 |
+
for gf in goal_files[-10:]: # Show last 10 goals
|
17 |
+
with open(f"goals/{gf}", "r") as f:
|
18 |
+
data = json.load(f)
|
19 |
+
goals.append(f"π― {data['goal']} β [{data['status']}]")
|
20 |
+
return "\n".join(goals) if goals else "No goals yet."
|
multi_agent.py
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import random
|
3 |
+
|
4 |
+
def multi_agent_brain(task):
|
5 |
+
agents = [agent1, agent2, agent3]
|
6 |
+
results = [a(task) for a in agents]
|
7 |
+
return f"π€ Multiple agents responded:\n" + "\n---\n".join(results)
|
8 |
+
|
9 |
+
def agent1(task): return f"Agent Alpha completed: {task.upper()}"
|
10 |
+
def agent2(task): return f"Agent Beta handled: {task[::-1]}"
|
11 |
+
def agent3(task): return f"Agent Gamma response: {task.lower()}"
|
utils.py
CHANGED
@@ -35,3 +35,15 @@ def run_code(code):
|
|
35 |
local_vars = {}
|
36 |
exec(code, {}, local_vars)
|
37 |
return "Code executed successfully."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|