File size: 1,322 Bytes
b7c5baf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import APIRouter, File, UploadFile, HTTPException, status
from fastapi.responses import JSONResponse

# Import the controller instance
from controller import controller

# Create an API router
router = APIRouter()

@router.post("/classify_forgery", 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)