mmo / app.py
robertgrayam's picture
Create app.py
3d7a815 verified
Raw History Blame Contribute Delete
1.89 kB
import gradio as gr
import random
import time
import asyncio
# Shared memory - simulates the "quantum entanglement"
shared_memory = {} # format: { "task_name": "failure_reason" }
# Wallet (starting money)
wallet = {"balance": 20.0}
# Agent class
class Agent:
def __init__(self, agent_id):
self.id = agent_id
async def do_task(self, task_name):
# Check shared memory first
if task_name in shared_memory:
return f"Agent {self.id}: Skipped {task_name}, known failure: {shared_memory[task_name]}"
try:
# Simulated task logic (replace with real API later)
if random.random() < 0.3: # 30% chance to fail
raise Exception("Simulated failure")
# If success
wallet["balance"] += 2.0
return f"Agent {self.id}: Completed {task_name}! Balance: ${wallet['balance']:.2f}"
except Exception as e:
# Log failure in shared memory so no other agent repeats it
shared_memory[task_name] = str(e)
return f"Agent {self.id}: Failed {task_name} -> {str(e)}"
# Initialize agents
agents = [Agent("A1"), Agent("A2"), Agent("A3")]
# Async loop to auto-run tasks
async def run_swarm():
results = []
tasks = ["transcribe", "data_entry", "survey_task"]
for agent in agents:
for task in tasks:
res = await agent.do_task(task)
results.append(res)
await asyncio.sleep(0.5) # small delay between tasks
return "\n".join(results)
# Gradio interface
async def run_button_click():
output = await run_swarm()
return output
with gr.Blocks() as demo:
gr.Markdown("# Self-Correcting Swarm Dashboard")
output_box = gr.Textbox(label="Swarm Output", lines=15)
run_button = gr.Button("Run Swarm Now")
run_button.click(fn=run_button_click, outputs=output_box)
demo.launch()