Spaces:
Runtime error
Runtime error
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,80 +1,39 @@
|
|
| 1 |
-
# main.py
|
| 2 |
-
from typing import Dict, Callable, Awaitable
|
| 3 |
import gradio as gr
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
msg = gr.Textbox(
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 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 |
+
|