techresearchspace commited on
Commit
a0edc2b
·
verified ·
1 Parent(s): d66dd8a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +384 -27
app.py CHANGED
@@ -1,39 +1,396 @@
1
- import gradio as gr
2
- from agents import build_graph, run_task
3
 
4
- # Compiled once per process — the graph itself holds no per-user state,
5
- # all mutable data lives in gr.State (per-session) so concurrent users
6
- # on the same Space don't interfere with each other.
7
- graph_app = build_graph()
 
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
- def handle_message(message, history, state):
11
- state = state or {}
12
  try:
13
- result = run_task(graph_app, message, state)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  except Exception as e:
15
- result = f"Agent error: {e}"
16
- return result, state
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
 
 
 
 
 
18
 
19
- with gr.Blocks(title="Multi-Agent Assistant") as demo:
20
- gr.Markdown("# Multi-Agent Assistant\nManager agent routes to search, code, and data specialists.")
 
 
 
21
 
22
- session_state = gr.State({})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
- chatbot = gr.Chatbot(height=450)
25
- msg = gr.Textbox(placeholder="Ask something that needs research, code, or data analysis...")
26
- clear = gr.Button("Clear")
27
 
28
- def respond(message, chat_history, state):
29
- chat_history = chat_history or []
30
- chat_history.append({"role": "user", "content": message})
31
- reply, state = handle_message(message, chat_history, state)
32
- chat_history.append({"role": "assistant", "content": reply})
33
- return "", chat_history, state
34
 
35
- msg.submit(respond, [msg, chatbot, session_state], [msg, chatbot, session_state])
36
- clear.click(lambda: ([], {}), None, [chatbot, session_state])
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
- if __name__ == "__main__":
39
- demo.launch()
 
1
+ """
2
+ Multi-agent orchestration with LangGraph.
3
 
4
+ Explicit state machine: a manager node decides which specialist to route to
5
+ next, each specialist does its work and returns to the manager, and the
6
+ manager decides when to finish. This gives you deterministic control flow
7
+ instead of framework-decided delegation.
8
+ """
9
 
10
+ import os
11
+ import json
12
+ import subprocess
13
+ import sys
14
+ import tempfile
15
+ from typing import Annotated, Literal, TypedDict
16
+
17
+ import spaces
18
+ import torch
19
+ from transformers import AutoModelForCausalLM, AutoTokenizer
20
+ from langchain_core.messages import AnyMessage, AIMessage, HumanMessage, SystemMessage
21
+ from langchain_core.tools import tool
22
+ from langchain_tavily import TavilySearch
23
+ from langgraph.graph import StateGraph, END
24
+ from langgraph.graph.message import add_messages
25
+
26
+ # Hard ceiling on manager<->specialist round-trips, independent of the graph's
27
+ # recursion_limit. If the manager keeps flip-flopping between specialists
28
+ # without ever saying "finish", this forces finalize instead of erroring out.
29
+ MAX_TURNS = 6
30
+
31
+
32
+ # ---------------------------------------------------------------------------
33
+ # Local model, run on a ZeroGPU-allocated GPU
34
+ # ---------------------------------------------------------------------------
35
+ # Pick a model that fits comfortably in a ZeroGPU allocation. 3B is a safe
36
+ # starting point; move up to 7B/8B if quality is the bottleneck, but watch
37
+ # the `duration` below — bigger models need more time per call.
38
+ MODEL_ID = "Qwen/Qwen2.5-3B-Instruct"
39
+
40
+ # Loaded once at import time, kept on CPU. ZeroGPU only attaches a GPU for
41
+ # the duration of a function decorated with @spaces.GPU — the model is
42
+ # moved onto that GPU inside `generate()` below and this is the standard
43
+ # pattern for ZeroGPU Spaces (see huggingface.co/docs/hub/spaces-zerogpu).
44
+ _tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
45
+ _model = AutoModelForCausalLM.from_pretrained(MODEL_ID, torch_dtype=torch.bfloat16)
46
+
47
+
48
+ @spaces.GPU(duration=60)
49
+ def generate(messages: list[dict], max_new_tokens: int = 512) -> str:
50
+ """Run one generation on a ZeroGPU-allocated GPU. `messages` is a plain
51
+ list of {"role": ..., "content": ...} dicts (chat-template format)."""
52
+ _model.to("cuda")
53
+ input_ids = _tokenizer.apply_chat_template(
54
+ messages, add_generation_prompt=True, return_tensors="pt"
55
+ ).to("cuda")
56
+ with torch.no_grad():
57
+ output_ids = _model.generate(
58
+ input_ids,
59
+ max_new_tokens=max_new_tokens,
60
+ do_sample=False,
61
+ )
62
+ new_tokens = output_ids[0][input_ids.shape[-1]:]
63
+ return _tokenizer.decode(new_tokens, skip_special_tokens=True).strip()
64
+
65
+
66
+ def _lc_to_dicts(messages: list[AnyMessage]) -> list[dict]:
67
+ """Convert langchain message objects into the plain role/content dicts
68
+ apply_chat_template expects."""
69
+ role_map = {"human": "user", "ai": "assistant", "system": "system"}
70
+ return [{"role": role_map.get(m.type, "user"), "content": m.content} for m in messages]
71
+
72
+
73
+ # ---------------------------------------------------------------------------
74
+ # Shared graph state
75
+ # ---------------------------------------------------------------------------
76
+ class AgentState(TypedDict):
77
+ messages: Annotated[list[AnyMessage], add_messages]
78
+ next: str # which node the manager wants to run next
79
+ task: str # original user task, kept for reference
80
+ results: dict # scratch space specialists write into
81
+ turn_count: int # manager round-trips so far, enforces MAX_TURNS
82
+
83
+
84
+ # ---------------------------------------------------------------------------
85
+ # Tools per specialist
86
+ # ---------------------------------------------------------------------------
87
+
88
+ # Requires TAVILY_API_KEY as a Space secret. Tavily is built for LLM agents
89
+ # (returns clean, citeable snippets rather than raw HTML), which is why it's
90
+ # preferred here over scraping a generic search engine yourself.
91
+ #
92
+ # Lazily constructed on first use rather than at import time: TavilySearch
93
+ # validates its API key eagerly in __init__, so building it at module load
94
+ # means a missing/bad key crashes the entire app before Gradio even starts.
95
+ # Deferring it means only the search tool fails, with a clear message,
96
+ # while the rest of the app (code_agent, data_agent) still works.
97
+ _tavily_instance = None
98
+
99
+
100
+ def _get_tavily():
101
+ global _tavily_instance
102
+ if _tavily_instance is None:
103
+ _tavily_instance = TavilySearch(max_results=5)
104
+ return _tavily_instance
105
+
106
+
107
+ @tool
108
+ def web_search(query: str) -> str:
109
+ """Search the web for a query and return a short summary of results."""
110
+ try:
111
+ tavily = _get_tavily()
112
+ except Exception as e:
113
+ return f"Search unavailable: TAVILY_API_KEY missing or invalid ({e})"
114
 
 
 
115
  try:
116
+ results = tavily.invoke({"query": query})
117
+ except Exception as e:
118
+ return f"Search failed: {e}"
119
+
120
+ items = results.get("results", []) if isinstance(results, dict) else results
121
+ if not items:
122
+ return "No results found."
123
+
124
+ lines = []
125
+ for r in items[:5]:
126
+ title = r.get("title", "")
127
+ url = r.get("url", "")
128
+ content = (r.get("content", "") or "")[:300]
129
+ lines.append(f"- {title} ({url}): {content}")
130
+ return "\n".join(lines)
131
+
132
+
133
+ # Sandboxing note: this uses a locked-down subprocess as a reasonable
134
+ # starting point (no shell, no filesystem/network access from the executed
135
+ # code's perspective within the limits below, hard timeout, restricted
136
+ # builtins). It is NOT a substitute for real isolation. For production,
137
+ # run this in a dedicated sandbox with its own OS-level boundary — e.g.
138
+ # E2B Code Interpreter, Modal Sandboxes, or a locked-down gVisor/Firecracker
139
+ # container — rather than a subprocess on the Space's own host.
140
+ _ALLOWED_BUILTIN_NAMES = [
141
+ "print", "range", "len", "str", "int", "float", "bool", "list",
142
+ "dict", "set", "tuple", "enumerate", "zip", "map", "filter",
143
+ "sum", "min", "max", "sorted", "abs", "round",
144
+ ]
145
+
146
+ # This template is written to a temp file and run as a separate, isolated
147
+ # (`-I`) interpreter process. It builds its OWN restricted builtins dict at
148
+ # runtime inside that fresh process — it does not reuse anything from this
149
+ # module's environment.
150
+ _SANDBOX_RUNNER = """
151
+ import sys
152
+ import builtins as _builtins_module
153
+ code = sys.stdin.read()
154
+ allowed = %r
155
+ safe_builtins = {name: getattr(_builtins_module, name) for name in allowed if hasattr(_builtins_module, name)}
156
+ env = {"__builtins__": safe_builtins}
157
+ try:
158
+ exec(code, env)
159
+ except Exception as e:
160
+ print(f"Error: {e}", file=sys.stderr)
161
+ sys.exit(1)
162
+ """ % (_ALLOWED_BUILTIN_NAMES,)
163
+
164
+
165
+ @tool
166
+ def run_python(code: str) -> str:
167
+ """Execute a snippet of Python in a restricted subprocess and return stdout or the error."""
168
+ runner_src = _SANDBOX_RUNNER
169
+
170
+ try:
171
+ with tempfile.NamedTemporaryFile("w", suffix=".py", delete=False) as f:
172
+ f.write(runner_src)
173
+ runner_path = f.name
174
+
175
+ proc = subprocess.run(
176
+ [sys.executable, "-I", runner_path], # -I: isolated mode, ignores env/site
177
+ input=code,
178
+ capture_output=True,
179
+ text=True,
180
+ timeout=5,
181
+ )
182
+ if proc.returncode != 0:
183
+ return f"Error: {proc.stderr.strip() or 'execution failed'}"
184
+ return proc.stdout or "(no output)"
185
+ except subprocess.TimeoutExpired:
186
+ return "Error: execution timed out after 5s"
187
  except Exception as e:
188
+ return f"Error: {e}"
189
+ finally:
190
+ try:
191
+ os.remove(runner_path)
192
+ except OSError:
193
+ pass
194
+
195
+
196
+ @tool
197
+ def summarize_numbers(numbers: list[float]) -> str:
198
+ """Compute summary statistics (count, sum, mean, min, max) for numbers."""
199
+ if not numbers:
200
+ return "No numbers provided."
201
+ n = len(numbers)
202
+ total = sum(numbers)
203
+ return f"count={n}, sum={total:.2f}, mean={total / n:.2f}, min={min(numbers)}, max={max(numbers)}"
204
+
205
+
206
+ SEARCH_TOOLS = [web_search]
207
+ CODE_TOOLS = [run_python]
208
+ DATA_TOOLS = [summarize_numbers]
209
+
210
+
211
+ # ---------------------------------------------------------------------------
212
+ # Manager node — decides which specialist runs next, or "finish"
213
+ # ---------------------------------------------------------------------------
214
+ MANAGER_SYSTEM_PROMPT = """You are a manager coordinating three specialist agents:
215
+ - search_agent: looks things up on the web
216
+ - code_agent: writes and runs Python
217
+ - data_agent: computes statistics on numeric data
218
+
219
+ Given the conversation so far, decide the single next step. Respond with ONLY
220
+ a JSON object of the form: {"next": "search_agent" | "code_agent" | "data_agent" | "finish", "reason": "..."}
221
+ Choose "finish" once you have enough information to answer the user directly.
222
+ """
223
+
224
+
225
+ def manager_node(state: AgentState):
226
+ turn_count = state.get("turn_count", 0) + 1
227
+
228
+ # Hard stop: don't even call the model once we've hit the cap — force
229
+ # finish so a manager stuck in a routing loop can't burn turns indefinitely.
230
+ if turn_count > MAX_TURNS:
231
+ return {
232
+ "next": "finish",
233
+ "turn_count": turn_count,
234
+ "messages": [SystemMessage(content=f"Max turns ({MAX_TURNS}) reached, finalizing with available results.")],
235
+ }
236
+
237
+ messages = [SystemMessage(content=MANAGER_SYSTEM_PROMPT)] + state["messages"]
238
+ raw_response = generate(_lc_to_dicts(messages), max_new_tokens=200)
239
+
240
+ try:
241
+ raw = raw_response.strip().strip("`")
242
+ if raw.startswith("json"):
243
+ raw = raw[4:]
244
+ decision = json.loads(raw)
245
+ next_step = decision.get("next", "finish")
246
+ except json.JSONDecodeError:
247
+ next_step = "finish"
248
+
249
+ if next_step not in {"search_agent", "code_agent", "data_agent", "finish"}:
250
+ next_step = "finish"
251
+
252
+ return {"next": next_step, "turn_count": turn_count, "messages": [AIMessage(content=raw_response)]}
253
+
254
+
255
+ def route_from_manager(state: AgentState) -> Literal["search_agent", "code_agent", "data_agent", "finalize"]:
256
+ if state["next"] == "finish":
257
+ return "finalize"
258
+ return state["next"]
259
+
260
+
261
+ # ---------------------------------------------------------------------------
262
+ # Specialist nodes — each does its work, appends a message, returns to manager
263
+ # ---------------------------------------------------------------------------
264
+ def _tool_spec_text(tools) -> str:
265
+ lines = []
266
+ for t in tools:
267
+ lines.append(f'- {t.name}: {t.description} Args schema: {t.args}')
268
+ return "\n".join(lines)
269
+
270
+
271
+ def make_specialist_node(name: str, tools, role_description: str):
272
+ tool_spec = _tool_spec_text(tools)
273
+ system_prompt = (
274
+ f"{role_description}\n\n"
275
+ f"Available tools:\n{tool_spec}\n\n"
276
+ 'Respond with ONLY a JSON object. Either call a tool: '
277
+ '{"tool": "<tool_name>", "args": {...}} — or, if no tool is needed, '
278
+ 'give the answer directly: {"final": "<your answer>"}'
279
+ )
280
+
281
+ def node(state: AgentState):
282
+ messages = [{"role": "system", "content": system_prompt}, {"role": "user", "content": state["task"]}]
283
+ raw_response = generate(messages, max_new_tokens=300)
284
+
285
+ summary = None
286
+ try:
287
+ raw = raw_response.strip().strip("`")
288
+ if raw.startswith("json"):
289
+ raw = raw[4:]
290
+ decision = json.loads(raw)
291
+ except json.JSONDecodeError:
292
+ decision = None
293
+
294
+ if decision and "tool" in decision:
295
+ tool_fn = next((t for t in tools if t.name == decision["tool"]), None)
296
+ if tool_fn is not None:
297
+ try:
298
+ tool_output = tool_fn.invoke(decision.get("args", {}))
299
+ except Exception as e:
300
+ tool_output = f"Tool error: {e}"
301
+ # One follow-up call to turn the raw tool output into a
302
+ # concise summary, rather than dumping it unprocessed.
303
+ followup = messages + [
304
+ {"role": "assistant", "content": raw_response},
305
+ {"role": "user", "content": f"Tool result: {tool_output}\n\nSummarize this for the manager."},
306
+ ]
307
+ summary = generate(followup, max_new_tokens=250)
308
+ else:
309
+ summary = f"Requested unknown tool '{decision['tool']}'."
310
+ elif decision and "final" in decision:
311
+ summary = decision["final"]
312
+ else:
313
+ summary = raw_response # model didn't follow the JSON protocol — use raw text as a fallback
314
+
315
+ state["results"][name] = summary
316
+ return {
317
+ "messages": [HumanMessage(content=f"[{name}] {summary}")],
318
+ "results": state["results"],
319
+ }
320
+ return node
321
+
322
+
323
+ search_node = make_specialist_node(
324
+ "search_agent", SEARCH_TOOLS, "You answer questions using the web_search tool. Be concise."
325
+ )
326
+ code_node = make_specialist_node(
327
+ "code_agent", CODE_TOOLS, "You solve tasks by writing and running Python with the run_python tool."
328
+ )
329
+ data_node = make_specialist_node(
330
+ "data_agent", DATA_TOOLS, "You analyze numeric data using the summarize_numbers tool."
331
+ )
332
+
333
+
334
+ # ---------------------------------------------------------------------------
335
+ # Finalize node — synthesizes a final answer from gathered specialist results
336
+ # ---------------------------------------------------------------------------
337
+ def finalize_node(state: AgentState):
338
+ context = "\n".join(f"{k}: {v}" for k, v in state["results"].items())
339
+ messages = [
340
+ {"role": "system", "content": "Write a final answer for the user based on the specialist findings below."},
341
+ {"role": "user", "content": f"Task: {state['task']}\n\nFindings:\n{context}"},
342
+ ]
343
+ response = generate(messages, max_new_tokens=500)
344
+ return {"messages": [AIMessage(content=response)]}
345
+
346
 
347
+ # ---------------------------------------------------------------------------
348
+ # Build the graph
349
+ # ---------------------------------------------------------------------------
350
+ def build_graph():
351
+ graph = StateGraph(AgentState)
352
 
353
+ graph.add_node("manager", manager_node)
354
+ graph.add_node("search_agent", search_node)
355
+ graph.add_node("code_agent", code_node)
356
+ graph.add_node("data_agent", data_node)
357
+ graph.add_node("finalize", finalize_node)
358
 
359
+ graph.set_entry_point("manager")
360
+ graph.add_conditional_edges(
361
+ "manager",
362
+ route_from_manager,
363
+ {
364
+ "search_agent": "search_agent",
365
+ "code_agent": "code_agent",
366
+ "data_agent": "data_agent",
367
+ "finalize": "finalize",
368
+ },
369
+ )
370
+ # each specialist reports back to the manager for the next decision
371
+ graph.add_edge("search_agent", "manager")
372
+ graph.add_edge("code_agent", "manager")
373
+ graph.add_edge("data_agent", "manager")
374
+ graph.add_edge("finalize", END)
375
 
376
+ return graph.compile()
 
 
377
 
 
 
 
 
 
 
378
 
379
+ def run_task(app, task: str, session_state: dict) -> str:
380
+ """Run one task through the graph. session_state is the gr.State dict
381
+ used to persist conversation history across turns in the UI."""
382
+ initial_state: AgentState = {
383
+ "messages": [HumanMessage(content=task)],
384
+ "next": "",
385
+ "task": task,
386
+ "results": {},
387
+ "turn_count": 0,
388
+ }
389
+ # recursion_limit is a blunt backstop (counts every node hop, including
390
+ # finalize); MAX_TURNS in manager_node is the real, tuned cap on manager
391
+ # round-trips and is what actually prevents runaway loops.
392
+ final_state = app.invoke(initial_state, config={"recursion_limit": 2 * MAX_TURNS + 5})
393
+ answer = final_state["messages"][-1].content
394
 
395
+ session_state.setdefault("history", []).append({"task": task, "answer": answer})
396
+ return answer