Spaces:
Sleeping
Sleeping
Update graph.py
#21
by Muthuraja18 - opened
graph.py
CHANGED
|
@@ -8,68 +8,154 @@ from agents.memory import Memory
|
|
| 8 |
from agents.planner import PlannerAgent
|
| 9 |
|
| 10 |
|
| 11 |
-
#
|
|
|
|
|
|
|
|
|
|
| 12 |
class DummyLLM:
|
|
|
|
| 13 |
def invoke(self, prompt):
|
| 14 |
-
|
|
|
|
| 15 |
content = "generate"
|
| 16 |
-
|
|
|
|
| 17 |
|
| 18 |
|
| 19 |
llm = DummyLLM()
|
| 20 |
|
| 21 |
memory = Memory()
|
|
|
|
| 22 |
router = RouterAgent(llm)
|
|
|
|
| 23 |
planner = PlannerAgent()
|
|
|
|
| 24 |
agent = ImageAgent(router, memory, planner)
|
| 25 |
|
| 26 |
|
| 27 |
-
# ----------------
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
def route_node(state: AgentState):
|
| 30 |
task = router.route(state["user_input"])
|
|
|
|
| 31 |
state["task"] = task
|
|
|
|
| 32 |
return state
|
| 33 |
|
| 34 |
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
state["steps"] = planner.plan(state["task"])
|
|
|
|
| 37 |
return state
|
| 38 |
|
| 39 |
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
result = agent.run(state["user_input"])
|
|
|
|
| 42 |
state["result"] = result
|
|
|
|
| 43 |
return state
|
| 44 |
|
| 45 |
|
| 46 |
-
# ----------------
|
|
|
|
|
|
|
| 47 |
|
| 48 |
workflow = StateGraph(AgentState)
|
| 49 |
|
|
|
|
|
|
|
| 50 |
workflow.add_node("route", route_node)
|
|
|
|
| 51 |
workflow.add_node("plan", plan_node)
|
|
|
|
|
|
|
|
|
|
| 52 |
workflow.add_node("execute", execute_node)
|
| 53 |
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
workflow.add_edge("route", "plan")
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
workflow.add_edge("execute", END)
|
| 59 |
|
| 60 |
app = workflow.compile()
|
| 61 |
|
| 62 |
|
| 63 |
-
#
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
state = {
|
|
|
|
| 67 |
"user_input": user_input,
|
|
|
|
| 68 |
"task": "",
|
|
|
|
| 69 |
"steps": [],
|
|
|
|
| 70 |
"uploaded_files": uploaded_files or [],
|
| 71 |
-
|
|
|
|
|
|
|
| 72 |
"result": None,
|
|
|
|
| 73 |
"history": []
|
| 74 |
}
|
| 75 |
|
|
|
|
| 8 |
from agents.planner import PlannerAgent
|
| 9 |
|
| 10 |
|
| 11 |
+
# -------------------------------
|
| 12 |
+
# Dummy LLM
|
| 13 |
+
# -------------------------------
|
| 14 |
+
|
| 15 |
class DummyLLM:
|
| 16 |
+
|
| 17 |
def invoke(self, prompt):
|
| 18 |
+
|
| 19 |
+
class Response:
|
| 20 |
content = "generate"
|
| 21 |
+
|
| 22 |
+
return Response()
|
| 23 |
|
| 24 |
|
| 25 |
llm = DummyLLM()
|
| 26 |
|
| 27 |
memory = Memory()
|
| 28 |
+
|
| 29 |
router = RouterAgent(llm)
|
| 30 |
+
|
| 31 |
planner = PlannerAgent()
|
| 32 |
+
|
| 33 |
agent = ImageAgent(router, memory, planner)
|
| 34 |
|
| 35 |
|
| 36 |
+
# -------------------------------
|
| 37 |
+
# ROUTE
|
| 38 |
+
# -------------------------------
|
| 39 |
+
|
| 40 |
+
def route_node(state):
|
| 41 |
|
|
|
|
| 42 |
task = router.route(state["user_input"])
|
| 43 |
+
|
| 44 |
state["task"] = task
|
| 45 |
+
|
| 46 |
return state
|
| 47 |
|
| 48 |
|
| 49 |
+
# -------------------------------
|
| 50 |
+
# PLAN
|
| 51 |
+
# -------------------------------
|
| 52 |
+
|
| 53 |
+
def plan_node(state):
|
| 54 |
+
|
| 55 |
state["steps"] = planner.plan(state["task"])
|
| 56 |
+
|
| 57 |
return state
|
| 58 |
|
| 59 |
|
| 60 |
+
# -------------------------------
|
| 61 |
+
# LOAD UPLOADED IMAGE
|
| 62 |
+
# -------------------------------
|
| 63 |
+
|
| 64 |
+
def upload_node(state):
|
| 65 |
+
|
| 66 |
+
uploaded = state.get("uploaded_files", [])
|
| 67 |
+
|
| 68 |
+
if uploaded:
|
| 69 |
+
|
| 70 |
+
# Store uploaded image in memory
|
| 71 |
+
memory.uploaded_files = uploaded
|
| 72 |
+
memory.last_used_image = uploaded[0]
|
| 73 |
+
|
| 74 |
+
return state
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
# -------------------------------
|
| 78 |
+
# AUTO ANALYZE
|
| 79 |
+
# -------------------------------
|
| 80 |
+
|
| 81 |
+
def analyze_node(state):
|
| 82 |
+
|
| 83 |
+
image = memory.last_used_image
|
| 84 |
+
|
| 85 |
+
if image:
|
| 86 |
+
|
| 87 |
+
analysis = agent.auto_analyze(image)
|
| 88 |
+
|
| 89 |
+
state["analysis"] = analysis
|
| 90 |
+
|
| 91 |
+
return state
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
# -------------------------------
|
| 95 |
+
# EXECUTE
|
| 96 |
+
# -------------------------------
|
| 97 |
+
|
| 98 |
+
def execute_node(state):
|
| 99 |
+
|
| 100 |
result = agent.run(state["user_input"])
|
| 101 |
+
|
| 102 |
state["result"] = result
|
| 103 |
+
|
| 104 |
return state
|
| 105 |
|
| 106 |
|
| 107 |
+
# -------------------------------
|
| 108 |
+
# GRAPH
|
| 109 |
+
# -------------------------------
|
| 110 |
|
| 111 |
workflow = StateGraph(AgentState)
|
| 112 |
|
| 113 |
+
workflow.add_node("upload", upload_node)
|
| 114 |
+
|
| 115 |
workflow.add_node("route", route_node)
|
| 116 |
+
|
| 117 |
workflow.add_node("plan", plan_node)
|
| 118 |
+
|
| 119 |
+
workflow.add_node("analyze", analyze_node)
|
| 120 |
+
|
| 121 |
workflow.add_node("execute", execute_node)
|
| 122 |
|
| 123 |
+
|
| 124 |
+
workflow.set_entry_point("upload")
|
| 125 |
+
|
| 126 |
+
workflow.add_edge("upload", "route")
|
| 127 |
|
| 128 |
workflow.add_edge("route", "plan")
|
| 129 |
+
|
| 130 |
+
workflow.add_edge("plan", "analyze")
|
| 131 |
+
|
| 132 |
+
workflow.add_edge("analyze", "execute")
|
| 133 |
+
|
| 134 |
workflow.add_edge("execute", END)
|
| 135 |
|
| 136 |
app = workflow.compile()
|
| 137 |
|
| 138 |
|
| 139 |
+
# -------------------------------
|
| 140 |
+
# MAIN
|
| 141 |
+
# -------------------------------
|
| 142 |
+
|
| 143 |
+
def run_graph(user_input, uploaded_files=None):
|
| 144 |
|
| 145 |
state = {
|
| 146 |
+
|
| 147 |
"user_input": user_input,
|
| 148 |
+
|
| 149 |
"task": "",
|
| 150 |
+
|
| 151 |
"steps": [],
|
| 152 |
+
|
| 153 |
"uploaded_files": uploaded_files or [],
|
| 154 |
+
|
| 155 |
+
"analysis": None,
|
| 156 |
+
|
| 157 |
"result": None,
|
| 158 |
+
|
| 159 |
"history": []
|
| 160 |
}
|
| 161 |
|