File size: 1,202 Bytes
da2b98d
b1f0e98
d481329
b1f0e98
 
 
5ddae77
b1f0e98
 
 
 
da2b98d
b1f0e98
 
5ddae77
da2b98d
 
b1f0e98
da2b98d
b1f0e98
da2b98d
 
b1f0e98
5ddae77
da2b98d
b1f0e98
d481329
b1f0e98
 
 
5ddae77
 
da2b98d
b1f0e98
 
da2b98d
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
"""API controllers for request handling and validation."""

import asyncio
from fastapi import HTTPException

from app.core.logging import logger
from app.services.base import InferenceService
from app.api.models import ImageRequest, PredictionResponse


class PredictionController:
    """Controller for prediction endpoints."""

    @staticmethod
    async def predict(
        request: ImageRequest,
        service: InferenceService
    ) -> PredictionResponse:
        """Run inference using the configured service."""
        try:
            if not service or not service.is_loaded:
                raise HTTPException(503, "Service not available")

            if not request.image.mediaType.startswith('image/'):
                raise HTTPException(400, f"Invalid media type: {request.image.mediaType}")

            return await asyncio.to_thread(service.predict, request)

        except HTTPException:
            raise
        except ValueError as e:
            logger.error(f"Invalid input: {e}")
            raise HTTPException(400, str(e))
        except Exception as e:
            logger.error(f"Prediction failed: {e}")
            raise HTTPException(500, "Internal server error")