import gradio as gr from agent import autonomous_agent from utils import read_memory, append_feedback from agent_editor import update_agent_code, read_agent_code import threading, time current_task = "" loop_running = False def run_task(task): global current_task current_task = task result = autonomous_agent(task) return result, read_memory() def update_memory(new_text): with open("memory.txt", "w") as f: f.write(new_text) return "āœ… Memory updated." def view_logs(): import os, json log_entries = [] if not os.path.exists("logs"): return "No logs yet." for filename in sorted(os.listdir("logs"))[-5:]: with open(f"logs/{filename}", "r") as f: data = json.load(f) log_entries.append(f"šŸ•’ {data['timestamp']}\nšŸ”§ Task: {data['task']}\nāœ… Result: {data['result'][:100]}...\n---") return "\n\n".join(log_entries) if log_entries else "No logs found." def store_feedback(score): append_feedback(current_task, "See logs for result", score) return "āœ… Feedback stored." def toggle_loop(toggle: bool, interval: int = 30): global loop_running loop_running = toggle if toggle: thread = threading.Thread(target=loop_runner, args=(interval,), daemon=True) thread.start() return "šŸ” Loop started." else: return "ā¹ļø Loop stopped." def loop_runner(interval): global loop_running while loop_running: run_task("Autonomous self-improvement") time.sleep(interval) with gr.Blocks() as demo: gr.Markdown("# šŸ¤– Autonomous AI Agent — Enhanced") with gr.Tab("šŸ’¬ Task Runner"): chatbot = gr.Textbox(lines=2, label="What should the AI do?") submit = gr.Button("Execute") output = gr.Textbox(label="Result") memory_out = gr.Textbox(label="Memory Snapshot", lines=6) submit.click(run_task, inputs=chatbot, outputs=[output, memory_out]) with gr.Tab("🧠 Memory Editor"): mem_editor = gr.Textbox(label="Edit memory.txt", lines=10) save_mem = gr.Button("Save Changes") result = gr.Textbox(label="Status") save_mem.click(update_memory, inputs=mem_editor, outputs=result) demo.load(read_memory, inputs=[], outputs=mem_editor) with gr.Tab("šŸ“ Recent Logs"): log_output = gr.Textbox(label="Last 5 Logs", lines=20) refresh_logs = gr.Button("Refresh") refresh_logs.click(view_logs, outputs=log_output) demo.load(view_logs, outputs=log_output) with gr.Tab("⭐ Feedback"): gr.Markdown("Rate the last task:") feedback_slider = gr.Slider(0, 10, step=0.5, label="Rating") submit_fb = gr.Button("Submit Feedback") fb_result = gr.Textbox(label="Status") submit_fb.click(store_feedback, inputs=feedback_slider, outputs=fb_result) with gr.Tab("šŸ” Loop Mode"): interval = gr.Slider(10, 300, step=10, label="Loop interval (seconds)", value=60) toggle = gr.Checkbox(label="Enable Autonomous Loop") loop_status = gr.Textbox(label="Loop Status") toggle.change(fn=toggle_loop, inputs=[toggle, interval], outputs=loop_status) with gr.Tab("🧬 Agent Code Editor"): code_box = gr.Code(label="Edit agent.py") code_status = gr.Textbox(label="Update Result") code_save = gr.Button("Save agent.py") demo.load(read_agent_code, outputs=code_box) code_save.click(update_agent_code, inputs=code_box, outputs=code_status) demo.launch() import gradio as gr from goal_manager import save_goal, list_goals from multi_agent import multi_agent_brain with demo: with gr.Tab("šŸŽÆ Goal Manager"): goal_input = gr.Textbox(label="Set a New Goal") goal_status = gr.Textbox(label="Save Result") show_goals = gr.Textbox(label="Latest Goals", lines=8) save_button = gr.Button("Save Goal") refresh_goals = gr.Button("Show Recent Goals") save_button.click(fn=save_goal, inputs=goal_input, outputs=goal_status) refresh_goals.click(fn=list_goals, outputs=show_goals) demo.load(fn=list_goals, outputs=show_goals) with gr.Tab("šŸ¤– Multi-Agent Collaboration"): group_task = gr.Textbox(label="Shared Task") run_group = gr.Button("Collaborate") group_output = gr.Textbox(label="Agent Responses", lines=6) run_group.click(fn=multi_agent_brain, inputs=group_task, outputs=group_output)