Benny-Tang commited on
Commit
c868868
·
verified ·
1 Parent(s): c65d50c

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +35 -76
main.py CHANGED
@@ -1,80 +1,39 @@
1
- # main.py
2
- from typing import Dict, Callable, Awaitable
3
  import gradio as gr
4
-
5
- # We expect these callables to be injected by app.py (keeps separation/testability).
6
- # Signatures:
7
- # run_agent(agent_id: int, message: str) -> Awaitable[str]
8
- # run_allAgents(message: str) -> Awaitable[Dict[str, str]]
9
-
10
- def build_ui(
11
- run_agent: Callable[[int, str], Awaitable[str]],
12
- run_all_agents: Callable[[str], Awaitable[Dict[str, str]]],
13
- ) -> gr.Blocks:
14
- with gr.Blocks(title="GLM 4.5 Multi-Agent Control Panel") as demo:
15
- gr.Markdown("# 🧭 GLM 4.5 Multi-Agent Control Panel")
16
- gr.Markdown(
17
- "Send a message to one agent or broadcast to all four. "
18
- "API is available at `/api/...` endpoints."
19
- )
20
-
21
- with gr.Row():
22
- agent = gr.Radio(
23
- choices=["agent1", "agent2", "agent3", "agent4", "all"],
24
- value="agent1",
25
- label="Target",
26
- )
27
- msg = gr.Textbox(
28
- label="Message",
29
- placeholder="Type something for the agent(s)...",
30
- lines=3,
31
- show_label=True,
32
- )
33
-
34
- with gr.Row():
35
- send = gr.Button("Send", variant="primary")
36
- clear = gr.Button("Clear")
37
-
38
- # ✅ Use OpenAI-style messages format
39
- single_out = gr.Chatbot(label="Agent Response", height=300, type="messages")
40
- all_out = gr.JSON(label="All Agents (JSON)")
41
-
42
- def _reset():
43
- return [], None
44
-
45
- clear.click(_reset, outputs=[single_out, all_out])
46
-
47
- async def handle_submit(agent_choice: str, message: str):
48
- if not message.strip():
49
- return gr.update(), gr.update(value={"error": "Message is empty."})
50
- if agent_choice == "all":
51
- data = await run_all_agents(message)
52
- # Show JSON in the 'all_out' and clear the single chat.
53
- return gr.update(value=[]), gr.update(value=data)
54
- else:
55
- agent_map = {"agent1": 1, "agent2": 2, "agent3": 3, "agent4": 4}
56
- agent_id = agent_map[agent_choice]
57
- resp = await run_agent(agent_id, message)
58
- # ✅ Append to chatbot using dict format
59
- return gr.update(value=[
60
- {"role": "user", "content": message},
61
- {"role": "assistant", "content": resp},
62
- ]), gr.update(value=None)
63
-
64
- send.click(
65
- handle_submit,
66
- inputs=[agent, msg],
67
- outputs=[single_out, all_out],
68
- )
69
-
70
- gr.Markdown(
71
- "### API\n"
72
- "- `POST /api/agent1` `{ message: string }`\n"
73
- "- `POST /api/agent2` `{ message: string }`\n"
74
- "- `POST /api/agent3` `{ message: string }`\n"
75
- "- `POST /api/agent4` `{ message: string }`\n"
76
- "- `POST /api/run_all` `{ message: string }`"
77
- )
78
 
79
  return demo
80
 
 
 
 
 
1
  import gradio as gr
2
+ import httpx
3
+
4
+ def query_agent(agent: str, message: str):
5
+ url = f"http://localhost:7860/api/{agent}" # works locally
6
+ try:
7
+ r = httpx.post(url, json={"text": message})
8
+ return r.json().get("response", "No response")
9
+ except Exception as e:
10
+ return str(e)
11
+
12
+ def query_all(message: str):
13
+ url = "http://localhost:7860/api/run_all"
14
+ try:
15
+ r = httpx.post(url, json={"text": message})
16
+ return [resp["response"] for resp in r.json()["responses"]]
17
+ except Exception as e:
18
+ return [str(e)]
19
+
20
+ def build_ui():
21
+ with gr.Blocks() as demo:
22
+ gr.Markdown("## Multi-Agent Control Panel")
23
+
24
+ with gr.Tab("Single Agent"):
25
+ msg = gr.Textbox(label="Message")
26
+ agent = gr.Dropdown(["agent1","agent2","agent3","agent4"], label="Choose Agent")
27
+ output = gr.Textbox(label="Agent Response")
28
+ btn = gr.Button("Send")
29
+ btn.click(fn=lambda m,a: query_agent(a,m), inputs=[msg,agent], outputs=output)
30
+
31
+ with gr.Tab("All Agents"):
32
+ msg_all = gr.Textbox(label="Message for All Agents")
33
+ outputs_all = gr.JSON(label="All Responses")
34
+ btn_all = gr.Button("Broadcast")
35
+ btn_all.click(fn=query_all, inputs=msg_all, outputs=outputs_all)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  return demo
38
 
39
+