Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from agents.philosopher import PhilosopherAgent | |
| from agents.historian import HistorianAgent | |
| from agents.hacker import HackerAgent | |
| from agents.comedian import ComedianAgent | |
| from agents.lawyer import LawyerAgent | |
| from agents.scientist import ScientistAgent | |
| from agents.journalist import JournalistAgent | |
| from agents.trader import TraderAgent | |
| from agents.base_agent import ACPMessage | |
| # β Initialize agents | |
| all_agents = [ | |
| PhilosopherAgent(), | |
| HistorianAgent(), | |
| HackerAgent(), | |
| ComedianAgent(), | |
| LawyerAgent(), | |
| ScientistAgent(), | |
| JournalistAgent(), | |
| TraderAgent() | |
| ] | |
| agent_map = {agent.name: agent for agent in all_agents} | |
| # β Core chat function | |
| def chat(prompt, selected_agents): | |
| responses = {} | |
| for name in selected_agents: | |
| agent = agent_map.get(name) | |
| try: | |
| output = agent.generate([ACPMessage(role="user", content=prompt)]) | |
| except Exception as e: | |
| output = f"[ERROR: {e}]" | |
| responses[name] = output | |
| return [responses] # must return list | |
| # β Interface | |
| demo = gr.Interface( | |
| fn=chat, | |
| inputs=[ | |
| gr.Textbox(label="Prompt"), | |
| gr.CheckboxGroup( | |
| choices=list(agent_map.keys()), | |
| label="Agents", | |
| value=list(agent_map.keys()) # default select all | |
| ) | |
| ], | |
| outputs=gr.JSON(label="Responses"), | |
| live=False, | |
| title="PerspectiveAI Backend API" | |
| ) | |
| # β Launch ONLY in API mode | |
| demo.launch( | |
| server_name="0.0.0.0", | |
| server_port=7860, | |
| show_api=True, # π₯ Enables /run/predict | |
| share=False # Optional | |
| ) | |