File size: 3,034 Bytes
62f828b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from fastapi import FastAPI,Request,status
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from src.app.routes import inference
from src.app.exceptions import ModelLoadError, PreprocessingError, InferenceError,InputError, PostprocessingError
import torch
import os
import sys

app = FastAPI(title="Super Resolution Dental X-ray API", version="1.0.0")
app.add_middleware(CORSMiddleware, allow_origins=["*"])

# Include API routes
app.include_router(inference.router, prefix="/inference", tags=["Inference"])

@app.get("/")
def read_root():
    return {"message": "Welcome to the Super Resolution Dental X-ray API"}

@app.get("/health", tags=["Health"])
async def health_check():
    """

    Health check endpoint to ensure the API and CUDA are running.



    Returns:

        dict: Status message indicating the API and CUDA availability.

    """
    def bash(command):
        return os.popen(command).read()

    # Check CUDA status

    # Construct response
    return {
        "status": "Healthy",
        "message": "API is running successfully.",
        "cuda": {
            "sys.version": sys.version,
            "torch.__version__": torch.__version__,
            "torch.cuda.is_available()": torch.cuda.is_available(),
            "torch.version.cuda": torch.version.cuda,
            "torch.backends.cudnn.version()": torch.backends.cudnn.version(),
            "torch.backends.cudnn.enabled": torch.backends.cudnn.enabled,
            "nvidia-smi": bash('nvidia-smi')
        }
    }

# Custom exception handlers
@app.exception_handler(ModelLoadError)
async def model_load_error_handler(request: Request, exc: ModelLoadError):
    return JSONResponse(
        status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
        content={"error": "ModelLoadError", "message": exc.message},
    )

@app.exception_handler(PreprocessingError)
async def preprocessing_error_handler(request: Request, exc: PreprocessingError):
    return JSONResponse(
        status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
        content={"error": "PreprocessingError", "message": exc.message},
    )

@app.exception_handler(PostprocessingError)
async def postprocessing_error_handler(request: Request, exc: PostprocessingError):
    return JSONResponse(
        status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
        content={"error": "PostprocessingError", "message": exc.message},
    )

@app.exception_handler(InferenceError)
async def inference_error_handler(request: Request, exc: InferenceError):
    return JSONResponse(
        status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
        content={"error": "InferenceError", "message": exc.message},
    )


@app.exception_handler(InputError)
async def input_load_error_handler(request: Request, exc: InputError):
    return JSONResponse(
        status_code=status.HTTP_400_BAD_REQUEST,
        content={"error": "InputError", "message": exc.message},
    )