glm45v3 / main.py
Benny-Tang's picture
Update main.py
bcaddf4 verified
import gradio as gr
import httpx
import os
# Use the container's base URL (relative path works inside the same app)
BASE_URL = os.environ.get("BASE_URL", "") # "" means same host:port as Gradio
def query_agent(agent: str, message: str):
url = f"{BASE_URL}/api/{agent}"
try:
r = httpx.post(url, json={"text": message})
return r.json().get("response", "No response")
except Exception as e:
return str(e)
def query_all(message: str):
url = f"{BASE_URL}/api/run_all"
try:
r = httpx.post(url, json={"text": message})
return [resp["response"] for resp in r.json()["responses"]]
except Exception as e:
return [str(e)]
def build_ui():
with gr.Blocks() as demo:
gr.Markdown("## Multi-Agent Control Panel")
with gr.Tab("Single Agent"):
msg = gr.Textbox(label="Message")
agent = gr.Dropdown(["agent1", "agent2", "agent3", "agent4"], label="Choose Agent")
output = gr.Textbox(label="Agent Response")
btn = gr.Button("Send")
btn.click(fn=lambda m, a: query_agent(a, m), inputs=[msg, agent], outputs=output)
with gr.Tab("All Agents"):
msg_all = gr.Textbox(label="Message for All Agents")
outputs_all = gr.JSON(label="All Responses")
btn_all = gr.Button("Broadcast")
btn_all.click(fn=query_all, inputs=msg_all, outputs=outputs_all)
return demo