Spaces:
Sleeping
Sleeping
File size: 2,962 Bytes
2c11783 | 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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | """
Flange Looseness Detection β FastAPI backend
============================================
Hosted on Hugging Face Spaces (Docker, port 7860).
Frontend on GitHub Pages makes cross-origin requests.
"""
import os
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from routers import upload, process, features, training, coral
# ββ App ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
app = FastAPI(
title="Flange ML API",
description="Backend for the Bolted Flange Looseness Detection educational app.",
version="1.0.0",
docs_url="/docs",
redoc_url="/redoc",
)
# ββ CORS β allow GitHub Pages + localhost dev ββββββββββββββββββββββββββββββββββ
ALLOWED_ORIGINS = [
"http://localhost:5173", # Vite dev server
"http://localhost:3000",
"https://localhost:5173",
# Add your GitHub Pages URL here after first deploy:
# "https://<username>.github.io",
]
# Allow all origins in development (override with env var in production)
if os.getenv("ALLOW_ALL_ORIGINS", "false").lower() == "true":
ALLOWED_ORIGINS = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=ALLOWED_ORIGINS,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# ββ Routers βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
app.include_router(upload.router)
app.include_router(process.router)
app.include_router(features.router)
app.include_router(training.router)
app.include_router(coral.router)
# ββ Health / root βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@app.get("/")
async def root():
return {"status": "ok", "message": "Flange ML API is running"}
@app.get("/health")
async def health():
from session import session_manager
return {
"status": "ok",
"sessions": session_manager.count(),
"version": "1.0.0",
}
# ββ Startup / shutdown ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@app.on_event("startup")
async def startup():
print("Flange ML API started.")
print("Docs: /docs")
if __name__ == "__main__":
import uvicorn
uvicorn.run(
"main:app",
host="0.0.0.0",
port=int(os.getenv("PORT", 7860)),
reload=os.getenv("ENV", "prod") == "dev",
)
|