Spaces:
Runtime error
Runtime error
Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# main.py
|
| 2 |
+
from typing import Dict, Callable, Awaitable
|
| 3 |
+
import asyncio
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
# We expect these callables to be injected by app.py (keeps separation/testability).
|
| 7 |
+
# Signatures:
|
| 8 |
+
# run_agent(agent_id: int, message: str) -> Awaitable[str]
|
| 9 |
+
# run_allAgents(message: str) -> Awaitable[Dict[str, str]]
|
| 10 |
+
|
| 11 |
+
def build_ui(
|
| 12 |
+
run_agent: Callable[[int, str], Awaitable[str]],
|
| 13 |
+
run_all_agents: Callable[[str], Awaitable[Dict[str, str]]],
|
| 14 |
+
) -> gr.Blocks:
|
| 15 |
+
with gr.Blocks(title="GLM 4.5 Multi-Agent Control Panel") as demo:
|
| 16 |
+
gr.Markdown("# 🧭 GLM 4.5 Multi-Agent Control Panel")
|
| 17 |
+
gr.Markdown(
|
| 18 |
+
"Send a message to one agent or broadcast to all four. "
|
| 19 |
+
"API is available at `/api/...` endpoints."
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
with gr.Row():
|
| 23 |
+
agent = gr.Radio(
|
| 24 |
+
choices=["agent1", "agent2", "agent3", "agent4", "all"],
|
| 25 |
+
value="agent1",
|
| 26 |
+
label="Target",
|
| 27 |
+
)
|
| 28 |
+
msg = gr.Textbox(
|
| 29 |
+
label="Message",
|
| 30 |
+
placeholder="Type something for the agent(s)...",
|
| 31 |
+
lines=3,
|
| 32 |
+
show_label=True,
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
with gr.Row():
|
| 36 |
+
send = gr.Button("Send", variant="primary")
|
| 37 |
+
clear = gr.Button("Clear")
|
| 38 |
+
|
| 39 |
+
# Outputs: one for single agent, one for 'all'
|
| 40 |
+
single_out = gr.Chatbot(label="Agent Response", height=300)
|
| 41 |
+
all_out = gr.JSON(label="All Agents (JSON)")
|
| 42 |
+
|
| 43 |
+
def _reset():
|
| 44 |
+
return None, None
|
| 45 |
+
|
| 46 |
+
clear.click(_reset, outputs=[single_out, all_out])
|
| 47 |
+
|
| 48 |
+
async def handle_submit(agent_choice: str, message: str):
|
| 49 |
+
if not message.strip():
|
| 50 |
+
return gr.update(), gr.update(value={"error": "Message is empty."})
|
| 51 |
+
if agent_choice == "all":
|
| 52 |
+
data = await run_all_agents(message)
|
| 53 |
+
# Show JSON in the 'all_out' and clear the single chat.
|
| 54 |
+
return gr.update(value=[]), gr.update(value=data)
|
| 55 |
+
else:
|
| 56 |
+
agent_map = {"agent1": 1, "agent2": 2, "agent3": 3, "agent4": 4}
|
| 57 |
+
agent_id = agent_map[agent_choice]
|
| 58 |
+
resp = await run_agent(agent_id, message)
|
| 59 |
+
# Append to chatbot as (user, bot)
|
| 60 |
+
return gr.update(value=[[message, resp]]), gr.update(value=None)
|
| 61 |
+
|
| 62 |
+
send.click(
|
| 63 |
+
handle_submit,
|
| 64 |
+
inputs=[agent, msg],
|
| 65 |
+
outputs=[single_out, all_out],
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
gr.Markdown(
|
| 69 |
+
"### API\n"
|
| 70 |
+
"- `POST /api/agent1` `{ message: string }`\n"
|
| 71 |
+
"- `POST /api/agent2` `{ message: string }`\n"
|
| 72 |
+
"- `POST /api/agent3` `{ message: string }`\n"
|
| 73 |
+
"- `POST /api/agent4` `{ message: string }`\n"
|
| 74 |
+
"- `POST /api/run_all` `{ message: string }`"
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
return demo
|