-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
71 lines (62 loc) · 1.98 KB
/
Copy pathmain.py
File metadata and controls
71 lines (62 loc) · 1.98 KB
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
# main.py
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from fastapi.middleware.cors import CORSMiddleware
import os
import logging
# Import routers
from app.controllers import api_router
from app.index_controllers import index_router
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Create FastAPI app
app = FastAPI(
title="AI Stroke Prediction API",
description="Advanced Machine Learning API for stroke risk prediction based on patient health data.",
version="1.0.0",
docs_url="/docs",
redoc_url="/redoc"
)
# Add CORS middleware for cross-origin requests
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # In production, specify your domains
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Mount static files (with error handling)
try:
if os.path.exists("frontend"):
app.mount("/static", StaticFiles(directory="frontend"), name="static")
except Exception as e:
logger.warning(f"Could not mount static files: {e}")
# Setup templates
templates = Jinja2Templates(directory="templates")
# Include routers
app.include_router(api_router)
app.include_router(index_router)
# Global health check (separate from API health check)
@app.get("/health")
async def root_health_check():
"""Root level health check for Render monitoring"""
return {
"status": "healthy",
"message": "AI Stroke Prediction API is running on Render",
"version": "1.0.0",
"docs": "/docs"
}
if __name__ == "__main__":
import uvicorn
# Use environment PORT for Render, fallback to 8000 for local
port = int(os.environ.get("PORT", 8000))
uvicorn.run(
"main:app",
host="0.0.0.0", # Important: Use 0.0.0.0 for Render
port=port,
reload=False, # Disable reload in production
log_level="info"
)