Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from agent import Agent
|
| 3 |
+
from persistence import AgentPersistence
|
| 4 |
+
|
| 5 |
+
# Instantiate persistence for shared use
|
| 6 |
+
persistence = AgentPersistence()
|
| 7 |
+
|
| 8 |
+
def initialize_agent(name, persona, instruction, strategy):
|
| 9 |
+
"""Initialize a new agent."""
|
| 10 |
+
agent = Agent(name, persistence)
|
| 11 |
+
agent.persona = persona
|
| 12 |
+
agent.instruction = instruction
|
| 13 |
+
agent.strategy = strategy
|
| 14 |
+
agent.save_state()
|
| 15 |
+
return f"Agent '{name}' initialized successfully."
|
| 16 |
+
|
| 17 |
+
def execute_task(agent_name, task):
|
| 18 |
+
"""Execute a task with the specified agent."""
|
| 19 |
+
agent = Agent(agent_name, persistence)
|
| 20 |
+
if not agent.load_state():
|
| 21 |
+
return f"Error: Agent '{agent_name}' not found."
|
| 22 |
+
result = agent.execute(task)
|
| 23 |
+
return result
|
| 24 |
+
|
| 25 |
+
def pause_agent(agent_name):
|
| 26 |
+
"""Pause the agent by saving its state."""
|
| 27 |
+
agent = Agent(agent_name, persistence)
|
| 28 |
+
if agent.save_state():
|
| 29 |
+
return f"Agent '{agent_name}' paused successfully."
|
| 30 |
+
else:
|
| 31 |
+
return f"Error: Failed to pause agent '{agent_name}'."
|
| 32 |
+
|
| 33 |
+
def resume_agent(agent_name):
|
| 34 |
+
"""Resume the agent's state."""
|
| 35 |
+
agent = Agent(agent_name, persistence)
|
| 36 |
+
if agent.load_state():
|
| 37 |
+
return f"Agent '{agent_name}' resumed successfully. Current task: {agent.task}"
|
| 38 |
+
else:
|
| 39 |
+
return f"Error: Failed to resume agent '{agent_name}'."
|
| 40 |
+
|
| 41 |
+
def list_agents():
|
| 42 |
+
"""List all saved agents."""
|
| 43 |
+
saved_agents = persistence.list_saved_agents()
|
| 44 |
+
if not saved_agents:
|
| 45 |
+
return "No agents found."
|
| 46 |
+
return "\n".join([f"{name} (Last updated: {timestamp})" for name, timestamp in saved_agents.items()])
|
| 47 |
+
|
| 48 |
+
# Gradio Interface
|
| 49 |
+
with gr.Blocks() as demo:
|
| 50 |
+
gr.Markdown("# Intelligent Agent Management")
|
| 51 |
+
|
| 52 |
+
with gr.Tab("Initialize Agent"):
|
| 53 |
+
agent_name = gr.Textbox(label="Agent Name")
|
| 54 |
+
persona = gr.Textbox(label="Persona")
|
| 55 |
+
instruction = gr.Textbox(label="Instruction")
|
| 56 |
+
strategy = gr.Textbox(label="Strategy (e.g., ChainOfThoughtStrategy)")
|
| 57 |
+
init_button = gr.Button("Initialize Agent")
|
| 58 |
+
init_output = gr.Textbox(label="Output")
|
| 59 |
+
init_button.click(
|
| 60 |
+
initialize_agent,
|
| 61 |
+
inputs=[agent_name, persona, instruction, strategy],
|
| 62 |
+
outputs=init_output,
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
with gr.Tab("Execute Task"):
|
| 66 |
+
exec_agent_name = gr.Textbox(label="Agent Name")
|
| 67 |
+
task = gr.Textbox(label="Task")
|
| 68 |
+
exec_button = gr.Button("Execute Task")
|
| 69 |
+
exec_output = gr.Textbox(label="Output")
|
| 70 |
+
exec_button.click(
|
| 71 |
+
execute_task,
|
| 72 |
+
inputs=[exec_agent_name, task],
|
| 73 |
+
outputs=exec_output,
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
with gr.Tab("Pause Agent"):
|
| 77 |
+
pause_agent_name = gr.Textbox(label="Agent Name")
|
| 78 |
+
pause_button = gr.Button("Pause Agent")
|
| 79 |
+
pause_output = gr.Textbox(label="Output")
|
| 80 |
+
pause_button.click(
|
| 81 |
+
pause_agent,
|
| 82 |
+
inputs=[pause_agent_name],
|
| 83 |
+
outputs=pause_output,
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
with gr.Tab("Resume Agent"):
|
| 87 |
+
resume_agent_name = gr.Textbox(label="Agent Name")
|
| 88 |
+
resume_button = gr.Button("Resume Agent")
|
| 89 |
+
resume_output = gr.Textbox(label="Output")
|
| 90 |
+
resume_button.click(
|
| 91 |
+
resume_agent,
|
| 92 |
+
inputs=[resume_agent_name],
|
| 93 |
+
outputs=resume_output,
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
with gr.Tab("List Agents"):
|
| 97 |
+
list_button = gr.Button("List All Agents")
|
| 98 |
+
list_output = gr.Textbox(label="Agents")
|
| 99 |
+
list_button.click(
|
| 100 |
+
list_agents,
|
| 101 |
+
inputs=[],
|
| 102 |
+
outputs=list_output,
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
demo.launch()
|