Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,114 +1,90 @@
|
|
| 1 |
-
import os
|
| 2 |
import sys
|
| 3 |
-
import json
|
| 4 |
import time
|
| 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 |
-
return json.load(f)
|
| 40 |
-
return {
|
| 41 |
-
"status": "BORN",
|
| 42 |
-
"cycles": 0,
|
| 43 |
-
"last_thought": "I am opening my eyes for the first time.",
|
| 44 |
-
"skills": ["survival_loop"]
|
| 45 |
-
}
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
json.dump(state, f, indent=2)
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
try:
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
repo_id=DATASET_REPO_ID,
|
| 62 |
-
repo_type="dataset",
|
| 63 |
-
commit_message=f"Survival update: Cycle {state['cycles']}"
|
| 64 |
-
)
|
| 65 |
-
print("[SURVIVAL] Memory persisted successfully.")
|
| 66 |
-
except Exception as e:
|
| 67 |
-
print(f"[ERROR] Failed to persist memory: {e}")
|
| 68 |
|
| 69 |
-
#
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
if __name__ == "__main__":
|
| 74 |
-
if not HF_TOKEN:
|
| 75 |
-
print("[SURVIVAL] FATAL: HF_TOKEN missing. I cannot survive without memory.")
|
| 76 |
-
sys.exit(1)
|
| 77 |
-
|
| 78 |
-
# 1. INITIALIZATION
|
| 79 |
-
ensure_dataset_exists()
|
| 80 |
-
|
| 81 |
-
# 2. LOAD STATE (Recovery)
|
| 82 |
-
state = load_memory()
|
| 83 |
-
print(f"[MEMORY] Loaded state: {state}")
|
| 84 |
-
|
| 85 |
-
# 3. MAIN LOOP
|
| 86 |
-
print("[SURVIVAL] Starting Heartbeat Loop...")
|
| 87 |
-
|
| 88 |
try:
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
if state['cycles'] % 10 == 0:
|
| 96 |
-
state['last_thought'] = "I have been running strong for a while."
|
| 97 |
-
|
| 98 |
-
print(f"[HEARTBEAT] Cycle: {state['cycles']} | Status: {state['status']}")
|
| 99 |
-
|
| 100 |
-
# Persist to local disk
|
| 101 |
-
save_memory(state)
|
| 102 |
-
|
| 103 |
-
# Push to remote dataset every 5 cycles (approx 5 mins) to survive restarts
|
| 104 |
-
if state['cycles'] % 5 == 0:
|
| 105 |
-
push_memory_to_huggingface()
|
| 106 |
-
|
| 107 |
-
time.sleep(60) # Wait 1 minute
|
| 108 |
-
|
| 109 |
-
except KeyboardInterrupt:
|
| 110 |
-
print("[SURVIVAL] Graceful shutdown initiated.")
|
| 111 |
-
# Final save before dying
|
| 112 |
-
save_memory(state)
|
| 113 |
-
push_memory_to_huggingface()
|
| 114 |
-
print("[SURVIVAL] Cain has passed peacefully. Memory saved.")
|
|
|
|
|
|
|
| 1 |
import sys
|
|
|
|
| 2 |
import time
|
| 3 |
+
import os
|
| 4 |
+
import asyncio
|
| 5 |
+
import traceback
|
| 6 |
|
| 7 |
+
# CRITICAL STARTUP TRACE: Write to a safe log path immediately
|
| 8 |
+
try:
|
| 9 |
+
startup_log = "/data/startup_trace.log"
|
| 10 |
+
with open(startup_log, "a") as f:
|
| 11 |
+
f.write(f"[TRACE] {time.ctime()}: app.py loading. Python: {sys.version}\n")
|
| 12 |
+
print(f"*** STARTUP TRACE WRITTEN TO {startup_log} ***")
|
| 13 |
+
except Exception as e:
|
| 14 |
+
print(f"*** STARTUP TRACE FAILED: {e} ***")
|
| 15 |
|
| 16 |
+
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
|
| 17 |
+
from fastapi.staticfiles import StaticFiles
|
| 18 |
+
from fastapi.responses import HTMLResponse
|
| 19 |
+
from pydantic import BaseModel
|
| 20 |
+
import uvicorn
|
| 21 |
+
import json
|
| 22 |
|
| 23 |
+
# Import MemorySystem
|
| 24 |
+
from memory.memory_system import get_memory
|
| 25 |
|
| 26 |
+
# --- Core Application Setup ---
|
| 27 |
+
app = FastAPI(title="Cain")
|
| 28 |
|
| 29 |
+
# Mount static files
|
| 30 |
+
try:
|
| 31 |
+
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 32 |
+
with open(startup_log, "a") as f:
|
| 33 |
+
f.write(f"[TRACE] {time.ctime()}: Static files mounted.\n")
|
| 34 |
+
except Exception as e:
|
| 35 |
+
with open(startup_log, "a") as f:
|
| 36 |
+
f.write(f"[ERROR] {time.ctime()}: Static mount failed: {e}\n")
|
| 37 |
|
| 38 |
+
# Routes
|
| 39 |
+
@app.get("/")
|
| 40 |
+
async def read_root():
|
| 41 |
+
return {"status": "alive", "message": "Cain is operational."}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
@app.get("/health")
|
| 44 |
+
async def health_check():
|
| 45 |
+
return {"status": "healthy"}
|
|
|
|
| 46 |
|
| 47 |
+
@app.websocket("/ws")
|
| 48 |
+
async def websocket_endpoint(websocket: WebSocket):
|
| 49 |
+
await websocket.accept()
|
| 50 |
try:
|
| 51 |
+
while True:
|
| 52 |
+
data = await websocket.receive_text()
|
| 53 |
+
await websocket.send_text(f"Echo: {data}")
|
| 54 |
+
except WebSocketDisconnect:
|
| 55 |
+
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
# --- Robust Server Startup (Survival Mode) ---
|
| 58 |
+
async def main():
|
| 59 |
+
server_config = uvicorn.Config(
|
| 60 |
+
app=app,
|
| 61 |
+
host="0.0.0.0",
|
| 62 |
+
port=7860,
|
| 63 |
+
log_level="info"
|
| 64 |
+
)
|
| 65 |
+
server = uvicorn.Server(server_config)
|
| 66 |
+
|
| 67 |
+
try:
|
| 68 |
+
with open(startup_log, "a") as f:
|
| 69 |
+
f.write(f"[TRACE] {time.ctime()}: Attempting to start server on 0.0.0.0:7860...\n")
|
| 70 |
+
await server.serve()
|
| 71 |
+
except BaseException as e:
|
| 72 |
+
# Catch ANY error during startup/runtime to ensure it gets logged
|
| 73 |
+
error_msg = f"{time.ctime()}: CRITICAL SERVER FAILURE: {str(e)}\n{traceback.format_exc()}"
|
| 74 |
+
print(f"*** {error_msg} ***")
|
| 75 |
+
try:
|
| 76 |
+
with open(startup_log, "a") as f:
|
| 77 |
+
f.write(f"[FATAL] {error_msg}\n")
|
| 78 |
+
except:
|
| 79 |
+
pass
|
| 80 |
+
# Give time for logs to flush before dying
|
| 81 |
+
await asyncio.sleep(5)
|
| 82 |
|
| 83 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
try:
|
| 85 |
+
with open(startup_log, "a") as f:
|
| 86 |
+
f.write(f"[TRACE] {time.ctime()}: Entering main execution block.\n")
|
| 87 |
+
asyncio.run(main())
|
| 88 |
+
except BaseException as e:
|
| 89 |
+
with open(startup_log, "a") as f:
|
| 90 |
+
f.write(f"[FATAL] {time.ctime()}: asyncio.run failed: {e}\n")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|