Spaces:
Sleeping
Sleeping
File size: 2,474 Bytes
07ba06f | 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 | #!/usr/bin/env python3
"""
AirCompute Terminal Chat & Swarm Mesh Monitor
Sends a prompt to the decentralized swarm, streams response tokens live in the terminal,
and monitors real-time Mesh Inspector telemetry logs.
"""
import asyncio
import json
import sys
import websockets
import httpx
async def run_terminal_test(prompt: str):
print("=" * 70)
print("🌐 AIRCOMPUTE DECENTRALIZED SWARM TERMINAL CLIENT")
print(f"📝 Prompt: \"{prompt}\"")
print("=" * 70 + "\n")
ws_url = "ws://127.0.0.1:8000/ws"
trigger_url = "http://127.0.0.1:8000/api/trigger"
async with websockets.connect(ws_url) as ws:
async def _trigger():
async with httpx.AsyncClient(timeout=120.0) as client:
await client.post(trigger_url, json={"prompt": prompt})
# Launch trigger in background task so WebSocket receives tokens concurrently
trigger_task = asyncio.create_task(_trigger())
print("🚀 Triggered Swarm Prompt!\n")
print("🤖 [AI RESPONSE STREAMING]: ")
print("-" * 50)
tokens_received = []
try:
while True:
msg = await asyncio.wait_for(ws.recv(), timeout=15.0)
data = json.loads(msg)
if data.get("type") == "token":
token = data.get("token", "")
tokens_received.append(token)
sys.stdout.write(f"[{token}] ")
sys.stdout.flush()
elif data.get("type") == "telemetry":
m1_cpu = data.get("m1_cpu", 0)
m2_cpu = data.get("m2_cpu", 0)
step = data.get("pipeline_step", "")
sys.stdout.write(f"\n 📊 [MESH TELEMETRY] Laptop CPU: {m1_cpu}% | Phone NPU: {m2_cpu}% | Active: {step}")
sys.stdout.flush()
elif data.get("type") == "log":
log_msg = data.get("msg", "")
print(f"\n\n📜 {log_msg}")
except asyncio.TimeoutError:
print("\n⏰ Stream finished.")
full_response = "".join(tokens_received)
print("-" * 50)
print(f"\n✅ Total Tokens Generated: {len(tokens_received)}")
print(f"💬 Complete Answer: \"{full_response.strip()}\"")
print("=" * 70)
if __name__ == "__main__":
prompt_text = sys.argv[1] if len(sys.argv) > 1 else "What is 2 + 2?"
asyncio.run(run_terminal_test(prompt_text))
|