Spaces:
Sleeping
Sleeping
Claude Code Claude Opus 4.6 commited on
Commit ·
bfc56a4
1
Parent(s): dc05a69
Claude Code: Fix startup crash with lazy imports and simplified root route
Browse files- Move FastAPI imports inside get_app() function to avoid blocking init
- Simplify root route to return {"status": "alive"}
- Add explicit stdout logging with flush=True
- Remove error_handlers dependency to avoid import failures
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
app.py
CHANGED
|
@@ -1,108 +1,66 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
Minimal HuggingClaw - Cain
|
| 4 |
-
|
| 5 |
"""
|
| 6 |
import os
|
| 7 |
import sys
|
| 8 |
import time
|
| 9 |
-
import logging
|
| 10 |
-
from fastapi import FastAPI, status as http_status
|
| 11 |
-
from fastapi.responses import JSONResponse
|
| 12 |
|
| 13 |
-
# EXPLICIT
|
| 14 |
-
print(">>> " + "="*50)
|
| 15 |
print(">>> CAIN: Python app.py loading...")
|
| 16 |
-
print(">>> " + "="*50)
|
| 17 |
print(">>> CAIN: Python version:", sys.version.split()[0])
|
| 18 |
print(">>> CAIN: Working directory:", os.getcwd())
|
| 19 |
print(">>> CAIN: PORT =", os.environ.get('PORT', '7860'))
|
| 20 |
-
print(">>>
|
| 21 |
-
|
| 22 |
-
#
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
# Import error handlers
|
| 35 |
-
try:
|
| 36 |
-
from error_handlers import (
|
| 37 |
-
http_exception_handler,
|
| 38 |
-
generic_exception_handler
|
| 39 |
)
|
| 40 |
-
logger
|
| 41 |
-
except ImportError as e:
|
| 42 |
-
logger.warning(f">>> STARTUP: error_handlers.py import failed: {e}")
|
| 43 |
-
# Define fallback handlers
|
| 44 |
-
async def http_exception_handler(request, exc):
|
| 45 |
-
return JSONResponse(status_code=exc.status_code, content={"error": exc.detail})
|
| 46 |
-
async def generic_exception_handler(request, exc):
|
| 47 |
-
return JSONResponse(status_code=500, content={"error": str(exc)})
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
logger.info(">>> STARTUP: FastAPI app created")
|
| 52 |
|
| 53 |
-
|
| 54 |
-
app.add_exception_handler(Exception, generic_exception_handler)
|
| 55 |
-
logger.info(">>> STARTUP: Exception handlers registered")
|
| 56 |
|
| 57 |
-
START_TIME = time.time()
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
@app.
|
| 61 |
-
async def
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
logger.info(f">>> STARTUP: PORT={os.environ.get('PORT', '7860')}")
|
| 65 |
-
logger.info(f">>> STARTUP: Working directory={os.getcwd()}")
|
| 66 |
-
logger.info(">>> STARTUP: Initialization complete, ready to serve requests")
|
| 67 |
-
print("Cain is ready")
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
"""Root endpoint - welcome message."""
|
| 73 |
-
try:
|
| 74 |
-
return {
|
| 75 |
-
"message": "Welcome to HuggingClaw - Cain",
|
| 76 |
-
"status": "running",
|
| 77 |
-
"version": "0.0.1",
|
| 78 |
-
"uptime_seconds": round(time.time() - START_TIME, 2)
|
| 79 |
-
}
|
| 80 |
-
except Exception as e:
|
| 81 |
-
logger.error("Root route exception", exc_info=True)
|
| 82 |
-
return JSONResponse(
|
| 83 |
-
status_code=500,
|
| 84 |
-
content={"error": str(e), "type": type(e).__name__}
|
| 85 |
-
)
|
| 86 |
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
"""
|
| 91 |
-
Health check endpoint - always returns 200 OK.
|
| 92 |
-
|
| 93 |
-
This endpoint is used for uptime monitoring and should never fail.
|
| 94 |
-
"""
|
| 95 |
-
return JSONResponse(
|
| 96 |
-
status_code=http_status.HTTP_200_OK,
|
| 97 |
-
content={
|
| 98 |
-
"status": "ok",
|
| 99 |
-
"uptime_seconds": round(time.time() - START_TIME, 2),
|
| 100 |
-
"timestamp": time.time()
|
| 101 |
-
}
|
| 102 |
-
)
|
| 103 |
|
| 104 |
|
| 105 |
if __name__ == "__main__":
|
| 106 |
import uvicorn
|
| 107 |
-
|
| 108 |
-
|
|
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
Minimal HuggingClaw - Cain
|
| 4 |
+
FastAPI with lazy imports to avoid startup blocking.
|
| 5 |
"""
|
| 6 |
import os
|
| 7 |
import sys
|
| 8 |
import time
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# EXPLICIT STARTUP PRINTS (visible in container logs)
|
|
|
|
| 11 |
print(">>> CAIN: Python app.py loading...")
|
|
|
|
| 12 |
print(">>> CAIN: Python version:", sys.version.split()[0])
|
| 13 |
print(">>> CAIN: Working directory:", os.getcwd())
|
| 14 |
print(">>> CAIN: PORT =", os.environ.get('PORT', '7860'))
|
| 15 |
+
print(">>> CAIN: sys.stdout flush...", flush=True)
|
| 16 |
+
|
| 17 |
+
# Lazy import FastAPI to avoid blocking init
|
| 18 |
+
def get_app():
|
| 19 |
+
"""Create FastAPI app with imports inside function."""
|
| 20 |
+
from fastapi import FastAPI
|
| 21 |
+
import logging
|
| 22 |
+
|
| 23 |
+
# Configure stdout logging
|
| 24 |
+
logging.basicConfig(
|
| 25 |
+
level=logging.INFO,
|
| 26 |
+
format='%(asctime)s - %(levelname)s - %(message)s',
|
| 27 |
+
handlers=[logging.StreamHandler(sys.stdout)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
)
|
| 29 |
+
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
print(">>> CAIN: Creating FastAPI app...", flush=True)
|
| 32 |
+
logger.info(">>> CAIN: FastAPI initialization starting")
|
|
|
|
| 33 |
|
| 34 |
+
app = FastAPI(title="HuggingClaw - Cain", version="0.0.1")
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
START_TIME = time.time()
|
| 37 |
|
| 38 |
+
@app.get("/")
|
| 39 |
+
async def root():
|
| 40 |
+
"""Root endpoint - simple alive status."""
|
| 41 |
+
return {"status": "alive"}
|
| 42 |
|
| 43 |
+
@app.get("/health")
|
| 44 |
+
async def health():
|
| 45 |
+
"""Health check endpoint."""
|
| 46 |
+
return {"status": "ok", "uptime": round(time.time() - START_TIME, 2)}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
@app.on_event("startup")
|
| 49 |
+
async def startup():
|
| 50 |
+
"""Log startup complete."""
|
| 51 |
+
print(">>> CAIN: Startup complete - ready to serve", flush=True)
|
| 52 |
+
logger.info(">>> CAIN: Ready to serve requests on port %s", os.environ.get('PORT', '7860'))
|
| 53 |
|
| 54 |
+
print(">>> CAIN: FastAPI app created successfully", flush=True)
|
| 55 |
+
return app
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
|
| 58 |
+
# Create app at module level for uvicorn
|
| 59 |
+
app = get_app()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
|
| 62 |
if __name__ == "__main__":
|
| 63 |
import uvicorn
|
| 64 |
+
port = int(os.environ.get('PORT', 7860))
|
| 65 |
+
print(f">>> CAIN: Starting uvicorn on port {port}...", flush=True)
|
| 66 |
+
uvicorn.run(app, host="0.0.0.0", port=port)
|