from fastapi import APIRouter, File, UploadFile, HTTPException, status from fastapi.responses import JSONResponse from controller import controller from fastapi import Request, Depends from fastapi.security import HTTPBearer from slowapi import Limiter from slowapi.util import get_remote_address router = APIRouter() limiter = Limiter(key_func=get_remote_address) security = HTTPBearer() # Create an API router router = APIRouter() @router.post("/classify", summary="Classify an image as Real or Fake") async def classify_image_endpoint(image: UploadFile = File(...)): """ Accepts an image file and classifies it as 'real' or 'fake'. - **image**: The image file to be classified (e.g., JPEG, PNG). Returns a JSON object with the classification and a confidence score. """ # Check for a valid image content type if not image.content_type.startswith("image/"): raise HTTPException( status_code=status.HTTP_415_UNSUPPORTED_MEDIA_TYPE, detail="Unsupported file type. Please upload an image (e.g., JPEG, PNG)." ) # The controller expects a file-like object, which `image.file` provides result = controller.classify_image(image.file) if "error" in result: # If the controller returned an error, forward it as an HTTP exception raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail=result["error"] ) return JSONResponse(content=result, status_code=status.HTTP_200_OK)