skincare-fastapi / app /main.py
M Bilal Naeem
final tuning for best results
edb77cb
from fastapi import FastAPI, HTTPException, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from fastapi import BackgroundTasks
import logging
from app.utils.download_models import download_models
# Initialize FastAPI
app = FastAPI(title="AI-Powered Skin Care Computer Vision Backend API", version="1.0")
# Configure logging
logging.basicConfig(level=logging.ERROR)
logger = logging.getLogger(__name__)
# Enable CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["https://ai-powered-skin-care-ecommerce.vercel.app","http://localhost:5173"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
try:
download_models()
logger.info("βœ… Models downloaded successfully before route import.")
except Exception as e:
logger.error(f"❌ Model download failed: {e}")
raise RuntimeError("Cannot start app β€” model download failed.")
# Import API routes
from app.routes.acne import router as acne_router
from app.routes.puffy_eyes import router as puffy_eyes_router
from app.routes.inpainting import router as inpainting_router
from app.routes.analyze_skin import router as analyze_skin_router
from app.routes.acne_severity import router as acne_sev_router
from app.routes.skin_type import router as skin_type_router
from app.routes.verify_face import router as verify_face_router
# Include API routes
app.include_router(acne_router, prefix="/api/acne")
app.include_router(acne_sev_router, prefix="/api/acne_severity")
app.include_router(puffy_eyes_router, prefix="/api/puffy_eyes")
app.include_router(inpainting_router, prefix="/api/inpainting")
app.include_router(analyze_skin_router, prefix="/api/skin_analysis")
app.include_router(skin_type_router, prefix="/api/skin_type")
app.include_router(verify_face_router, prefix="/api/face")
@app.get("/")
async def root():
return {"message": "Welcome to AI-Powered Skin Care API"}
# βœ… Custom Error Handling for Unhandled Exceptions
@app.exception_handler(Exception)
async def global_exception_handler(request: Request, exc: Exception):
logger.error(f"Unhandled error: {exc}", exc_info=True)
return JSONResponse(
status_code=500,
content={"error": "Internal Server Error", "details": str(exc)},
)
# βœ… Custom Error Handling for Not Found Routes
@app.exception_handler(HTTPException)
async def not_found_handler(request: Request, exc: HTTPException):
return JSONResponse(
status_code=exc.status_code,
content={"error": "Not Found", "details": exc.detail},
)