Spaces:
Runtime error
Runtime error
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,107 +0,0 @@
|
|
| 1 |
-
# app.py
|
| 2 |
-
from fastapi import FastAPI, Request, HTTPException
|
| 3 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
-
from fastapi.responses import JSONResponse
|
| 5 |
-
import logging
|
| 6 |
-
import time
|
| 7 |
-
import requests
|
| 8 |
-
|
| 9 |
-
from api.health import router as health_router
|
| 10 |
-
from api.extract import router as extract_router
|
| 11 |
-
from api.enroll import router as enroll_router
|
| 12 |
-
from api.recognize import router as recognize_router
|
| 13 |
-
|
| 14 |
-
logging.basicConfig(
|
| 15 |
-
level=logging.INFO,
|
| 16 |
-
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
| 17 |
-
)
|
| 18 |
-
logger = logging.getLogger(__name__)
|
| 19 |
-
|
| 20 |
-
app = FastAPI(
|
| 21 |
-
title="SchoolMaster Face Recognition Service",
|
| 22 |
-
description="AI face recognition for school attendance",
|
| 23 |
-
version="1.0.0"
|
| 24 |
-
)
|
| 25 |
-
|
| 26 |
-
app.add_middleware(
|
| 27 |
-
CORSMiddleware,
|
| 28 |
-
allow_origins=["*"],
|
| 29 |
-
allow_credentials=True,
|
| 30 |
-
allow_methods=["*"],
|
| 31 |
-
allow_headers=["*"],
|
| 32 |
-
)
|
| 33 |
-
|
| 34 |
-
@app.middleware("http")
|
| 35 |
-
async def log_requests(request: Request, call_next):
|
| 36 |
-
start_time = time.time()
|
| 37 |
-
response = await call_next(request)
|
| 38 |
-
process_time = time.time() - start_time
|
| 39 |
-
logger.info(f"{request.method} {request.url.path} - {response.status_code} - {process_time:.3f}s")
|
| 40 |
-
return response
|
| 41 |
-
|
| 42 |
-
@app.get("/")
|
| 43 |
-
async def root():
|
| 44 |
-
return {
|
| 45 |
-
"service": "SchoolMaster Face Recognition",
|
| 46 |
-
"status": "running",
|
| 47 |
-
"version": "1.0.0",
|
| 48 |
-
"endpoints": [
|
| 49 |
-
"GET /api/health",
|
| 50 |
-
"POST /api/extract",
|
| 51 |
-
"POST /api/enroll",
|
| 52 |
-
"POST /api/recognize"
|
| 53 |
-
]
|
| 54 |
-
}
|
| 55 |
-
|
| 56 |
-
app.include_router(health_router, prefix="/api", tags=["Health"])
|
| 57 |
-
app.include_router(extract_router, prefix="/api", tags=["Extraction"])
|
| 58 |
-
app.include_router(enroll_router, prefix="/api", tags=["Enrollment"])
|
| 59 |
-
app.include_router(recognize_router, prefix="/api", tags=["Recognition"])
|
| 60 |
-
|
| 61 |
-
@app.exception_handler(HTTPException)
|
| 62 |
-
async def http_exception_handler(request, exc):
|
| 63 |
-
return JSONResponse(
|
| 64 |
-
status_code=exc.status_code,
|
| 65 |
-
content={"success": False, "message": exc.detail}
|
| 66 |
-
)
|
| 67 |
-
|
| 68 |
-
# ============================================================
|
| 69 |
-
# Minimal Gradio status page, mounted onto the SAME app/port.
|
| 70 |
-
# This is only here so the Space genuinely uses the Gradio SDK
|
| 71 |
-
# (keeping it eligible for free hosting) - your PHP backend
|
| 72 |
-
# talks to the /api/* routes above directly and never touches this UI.
|
| 73 |
-
# ============================================================
|
| 74 |
-
import gradio as gr
|
| 75 |
-
|
| 76 |
-
def check_health():
|
| 77 |
-
try:
|
| 78 |
-
r = requests.get("http://127.0.0.1:7860/api/health", timeout=5)
|
| 79 |
-
return r.json()
|
| 80 |
-
except Exception as e:
|
| 81 |
-
return {"error": str(e)}
|
| 82 |
-
|
| 83 |
-
with gr.Blocks(title="Face Recognition API") as demo:
|
| 84 |
-
gr.Markdown(
|
| 85 |
-
"# Face Recognition API\n\n"
|
| 86 |
-
"This is a status page only. Your school system's PHP backend talks "
|
| 87 |
-
"directly to the `/api/*` endpoints below - nothing here.\n\n"
|
| 88 |
-
"**Endpoints:** `/api/health` (GET), `/api/extract`, `/api/enroll`, "
|
| 89 |
-
"`/api/recognize` (POST, require `X-API-Key` header + multipart photos)"
|
| 90 |
-
)
|
| 91 |
-
btn = gr.Button("Check API Health", variant="primary")
|
| 92 |
-
out = gr.JSON(label="Result")
|
| 93 |
-
btn.click(fn=check_health, outputs=out)
|
| 94 |
-
|
| 95 |
-
app = gr.mount_gradio_app(app, demo, path="/ui")
|
| 96 |
-
|
| 97 |
-
if __name__ == "__main__":
|
| 98 |
-
import uvicorn
|
| 99 |
-
from config import Config
|
| 100 |
-
# Single process, single port bind - this is the actual entry point.
|
| 101 |
-
# Do NOT also call demo.launch() anywhere: that would try to bind
|
| 102 |
-
# the same port a second time and crash the Space.
|
| 103 |
-
uvicorn.run(
|
| 104 |
-
app,
|
| 105 |
-
host=Config.HOST,
|
| 106 |
-
port=Config.PORT,
|
| 107 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|