Spaces:
Paused
Paused
Nicholas Bumgarner commited on
Commit ·
b356ed7
1
Parent(s): 76b2b96
Revert to ring buffer protocol; optimize log tail reading
Browse files
app.py
CHANGED
|
@@ -171,9 +171,11 @@ def _monitor_phoenix():
|
|
| 171 |
for _ in range(120): # Check for up to 2 minutes
|
| 172 |
_time.sleep(0.5)
|
| 173 |
try:
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
|
|
|
|
|
|
| 177 |
_update_boot("ready", "Phoenix is ready to chat!")
|
| 178 |
return
|
| 179 |
# Check if process died
|
|
@@ -318,8 +320,10 @@ def get_boot_status():
|
|
| 318 |
|
| 319 |
|
| 320 |
def phox_chat(message, history):
|
| 321 |
-
"""Main chat function —
|
| 322 |
-
|
|
|
|
|
|
|
| 323 |
with _state_lock:
|
| 324 |
phoenix_running = _phoenix_proc is not None and _phoenix_proc.poll() is None
|
| 325 |
if not phoenix_running:
|
|
@@ -336,39 +340,11 @@ def phox_chat(message, history):
|
|
| 336 |
if not token_ids:
|
| 337 |
return "Empty input."
|
| 338 |
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
# Open pipe for writing (blocks until phoenix opens it for reading)
|
| 343 |
-
pipe_fd = os.open(pipe_path, os.O_WRONLY | os.O_NONBLOCK)
|
| 344 |
-
token_str = " ".join(str(t) for t in token_ids) + "\n"
|
| 345 |
-
os.write(pipe_fd, token_str.encode())
|
| 346 |
-
os.close(pipe_fd)
|
| 347 |
-
except (FileNotFoundError, OSError, BrokenPipeError):
|
| 348 |
-
return "Phoenix not ready for chat. Please wait for startup."
|
| 349 |
-
|
| 350 |
-
# Wait for predictions by reading the state JSONL
|
| 351 |
-
pred_ids = []
|
| 352 |
-
start = time.time()
|
| 353 |
-
while time.time() - start < CHAT_TIMEOUT_S:
|
| 354 |
-
try:
|
| 355 |
-
with open(STATE_FILE, 'r') as f:
|
| 356 |
-
lines = f.readlines()
|
| 357 |
-
for line in reversed(lines):
|
| 358 |
-
line = line.strip()
|
| 359 |
-
if line.startswith('{"event":"token"'):
|
| 360 |
-
# Parse the last token event
|
| 361 |
-
evt = json.loads(line)
|
| 362 |
-
if evt.get("tok") not in pred_ids:
|
| 363 |
-
pred_ids.append(evt.get("tok"))
|
| 364 |
-
if len(pred_ids) >= len(token_ids):
|
| 365 |
-
break
|
| 366 |
-
except (FileNotFoundError, PermissionError, json.JSONDecodeError):
|
| 367 |
-
pass
|
| 368 |
-
if pred_ids:
|
| 369 |
-
break
|
| 370 |
-
time.sleep(0.1)
|
| 371 |
|
|
|
|
| 372 |
if not pred_ids:
|
| 373 |
return "No prediction received (timeout). Try again."
|
| 374 |
|
|
@@ -412,14 +388,19 @@ with gr.Blocks(title="Phox — Wave-Rider Brain") as demo:
|
|
| 412 |
# Periodic status refresh using gr.Timer (Gradio 6.x API)
|
| 413 |
def tick():
|
| 414 |
base = f"Phase: {_boot_status['phase']} | {_boot_status['message']}"
|
| 415 |
-
# Show last line of daemon log if available
|
| 416 |
try:
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 423 |
except (FileNotFoundError, PermissionError):
|
| 424 |
pass
|
| 425 |
return base
|
|
|
|
| 171 |
for _ in range(120): # Check for up to 2 minutes
|
| 172 |
_time.sleep(0.5)
|
| 173 |
try:
|
| 174 |
+
size = os.path.getsize(STATE_LOG)
|
| 175 |
+
with open(STATE_LOG, 'rb') as f:
|
| 176 |
+
f.seek(max(0, size - 5000))
|
| 177 |
+
log = f.read().decode('utf-8', errors='replace')
|
| 178 |
+
if "Chat Mode" in log or "ring buffer" in log.lower() or "ready" in log.lower():
|
| 179 |
_update_boot("ready", "Phoenix is ready to chat!")
|
| 180 |
return
|
| 181 |
# Check if process died
|
|
|
|
| 320 |
|
| 321 |
|
| 322 |
def phox_chat(message, history):
|
| 323 |
+
"""Main chat function — writes tokens to ring buffer, reads predictions."""
|
| 324 |
+
if not _ring_in_mm or not _ring_out_mm:
|
| 325 |
+
return "Engine not initialized. Please wait for startup."
|
| 326 |
+
|
| 327 |
with _state_lock:
|
| 328 |
phoenix_running = _phoenix_proc is not None and _phoenix_proc.poll() is None
|
| 329 |
if not phoenix_running:
|
|
|
|
| 340 |
if not token_ids:
|
| 341 |
return "Empty input."
|
| 342 |
|
| 343 |
+
pushed = send_tokens_to_phoenix(token_ids)
|
| 344 |
+
if pushed == 0:
|
| 345 |
+
return "Ring buffer full. Try again."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 346 |
|
| 347 |
+
pred_ids = poll_predictions()
|
| 348 |
if not pred_ids:
|
| 349 |
return "No prediction received (timeout). Try again."
|
| 350 |
|
|
|
|
| 388 |
# Periodic status refresh using gr.Timer (Gradio 6.x API)
|
| 389 |
def tick():
|
| 390 |
base = f"Phase: {_boot_status['phase']} | {_boot_status['message']}"
|
| 391 |
+
# Show last line of daemon log if available (read tail only)
|
| 392 |
try:
|
| 393 |
+
import os as _os
|
| 394 |
+
if _os.path.exists(STATE_LOG):
|
| 395 |
+
size = _os.path.getsize(STATE_LOG)
|
| 396 |
+
with open(STATE_LOG, 'rb') as f:
|
| 397 |
+
f.seek(max(0, size - 500))
|
| 398 |
+
tail = f.read().decode('utf-8', errors='replace')
|
| 399 |
+
lines = [l.strip() for l in tail.split('\n') if l.strip()]
|
| 400 |
+
if lines:
|
| 401 |
+
last = lines[-1]
|
| 402 |
+
if last:
|
| 403 |
+
base += f"\n[daemon] {last[:200]}"
|
| 404 |
except (FileNotFoundError, PermissionError):
|
| 405 |
pass
|
| 406 |
return base
|