Spaces:
Sleeping
Sleeping
File size: 3,635 Bytes
e74d883 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | #!/usr/bin/env python3
"""CLI test script for Analog Town simulation.
Run this to verify the simulation pipeline works before launching the UI.
Usage:
python test_cli.py
"""
import json
import sys
from pathlib import Path
from schemas import Town, BroadcastEvent, AgentState
from model_client import ModelClient
from simulator import Simulator
def load_sample_town(name: str = "graybridge") -> Town:
"""Load a sample town from the sample_towns directory."""
path = Path(__file__).parent / "sample_towns" / f"{name}.json"
if not path.exists():
print(f"β Sample town '{name}' not found at {path}")
sys.exit(1)
with open(path) as f:
data = json.load(f)
return Town(**data)
def main():
print("=" * 60)
print("π» ANALOG TOWN β CLI Simulation Test")
print("=" * 60)
print()
# 1. Load town
print("π Loading Graybridge sample town...")
town = load_sample_town("graybridge")
print(f" Town: {town.name} ({len(town.agents)} agents)")
for agent in town.agents:
print(f" π‘ {agent.frequency} FM β {agent.name} ({agent.role})")
print()
# 2. Set up event
event = town.default_event or BroadcastEvent(
title="Station Hotel Development",
content="The old train station will be converted into a luxury hotel by an outside developer.",
source="Town Council Announcement",
)
print(f"π’ Broadcasting: \"{event.title}\"")
print(f" {event.content}")
print()
# 3. Initialize simulator
print("π§ Initializing model client...")
try:
client = ModelClient()
print(f" Model: {client.model_id}")
except Exception as e:
print(f"β Failed to initialize model client: {e}")
sys.exit(1)
simulator = Simulator(model_client=client)
# 4. Run simulation
print()
print("π Running simulation...")
print("-" * 60)
def progress_callback(name, status, index, total):
emoji = {"processing": "β³", "complete": "β
", "failed": "β"}.get(
status.split(":")[0], "β "
)
print(f" {emoji} [{index+1}/{total}] {name}: {status}")
result = simulator.run_town_simulation(
town=town,
event=event,
progress_callback=progress_callback,
)
print("-" * 60)
print()
# 5. Display results
print(f"π Results: {len(result.transitions)}/{len(town.agents)} agents responded")
print()
for transition in result.transitions:
agent = next((a for a in town.agents if a.id == transition.agent_id), None)
freq = agent.frequency if agent else "?"
name = agent.name if agent else transition.agent_id
print(f"π» {freq} FM β {name}")
print(f" Mood: {transition.updated_state.mood_label}")
print(f" π \"{transition.internal_monologue}\"")
print(f" π Value conflict: {transition.value_conflict}")
print(f" π― Public action: {transition.likely_public_action}")
print(f" β Uncertainty: {transition.uncertainty}")
print()
# 6. Summary
if len(result.transitions) == len(town.agents):
print("β
All agents responded successfully!")
elif len(result.transitions) > 0:
print(f"β {len(town.agents) - len(result.transitions)} agent(s) failed")
else:
print("β No agents responded. Check model configuration.")
print()
print(f"π Simulation completed at {result.created_at}")
return len(result.transitions) > 0
if __name__ == "__main__":
success = main()
sys.exit(0 if success else 1)
|