Skip to content

Commit

Permalink
BAI-1502 apply PR suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
PE39806 committed Nov 18, 2024
1 parent 75414bf commit 15a426c
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 19 deletions.
2 changes: 1 addition & 1 deletion docker-compose-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ services:
ports:
- 3311:80
healthcheck:
test: ['CMD-SHELL', 'curl --fail http://localhost:80/health || exit 1']
test: ['CMD-SHELL', 'curl --fail http://localhost:80/info || exit 1']
interval: 30s
timeout: 10s
retries: 5
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ services:
ports:
- 3311:80
healthcheck:
test: ['CMD-SHELL', 'curl --fail http://localhost:80/health || exit 1']
test: ['CMD-SHELL', 'curl --fail http://localhost:80/info || exit 1']
interval: 30s
timeout: 10s
retries: 5
Expand Down
2 changes: 2 additions & 0 deletions lib/modelscan_api/.pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ ci:
autoupdate_commit_msg: 'chore: update pre-commit hooks'
autofix_commit_msg: 'style: pre-commit fixes'

files: ^lib/modelscan_api/

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
Expand Down
2 changes: 1 addition & 1 deletion lib/modelscan_api/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.12-slim
FROM python:3.12-slim-bullseye

# Prevents Python from writing pyc files.
ENV PYTHONDONTWRITEBYTECODE=1
Expand Down
2 changes: 1 addition & 1 deletion lib/modelscan_api/Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.12-slim
FROM python:3.12-slim-bullseye

# Prevents Python from writing pyc files.
ENV PYTHONDONTWRITEBYTECODE=1
Expand Down
38 changes: 27 additions & 11 deletions lib/modelscan_api/bailo_modelscan_api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@

from __future__ import annotations

from contextlib import nullcontext
import logging
from contextlib import nullcontext
from functools import lru_cache
from http import HTTPStatus
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import Annotated, Any

import modelscan
import uvicorn
from fastapi import BackgroundTasks, Depends, FastAPI, HTTPException, UploadFile
from modelscan.modelscan import ModelScan
from pydantic import BaseModel

from bailo_modelscan_api.config import Settings
from bailo_modelscan_api.dependencies import safe_join
Expand All @@ -40,22 +42,34 @@ def get_settings() -> Settings:
)

# Instantiating ModelScan
modelscan = ModelScan(settings=get_settings().modelscan_settings)
modelscan_model = ModelScan(settings=get_settings().modelscan_settings)


class ApiInformation(BaseModel):
apiName: str
apiVersion: str
scannerName: str
modelscanVersion: str


@app.get(
"/health",
summary="Simple health check endpoint",
description="Utility to check the operational status of the API.",
"/info",
summary="Simple information endpoint",
description="Utility to get the key information about the API.",
status_code=HTTPStatus.OK,
response_description='Always `{"status": "healthy"}`',
response_description="A populated ApiInformation object",
)
def health_check() -> dict[str, str]:
"""Minimal health check for the API endpoint.
def info(settings: Annotated[Settings, Depends(get_settings)]) -> ApiInformation:
"""Information about the API.
:return: always `{"status": "healthy"}`
:return: a JSON representable object with keys from ApiInformation
"""
return {"status": "healthy"}
return ApiInformation(
apiName=settings.app_name,
apiVersion=settings.app_version,
scannerName=modelscan.__name__,
modelscanVersion=modelscan.__version__,
)


@app.post(
Expand Down Expand Up @@ -110,7 +124,9 @@ def scan_file(
) from exception

# Scan the uploaded file.
result = modelscan.scan(pathlib_path)
logger.info("Initiating ModelScan scan of %s", pathlib_path)
result = modelscan_model.scan(pathlib_path)
logger.info("ModelScan result: %s", result)

# Finally, return the result.
return result
Expand Down
1 change: 0 additions & 1 deletion lib/modelscan_api/bailo_modelscan_api/test_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

from .dependencies import safe_join


# Helpers


Expand Down
17 changes: 14 additions & 3 deletions lib/modelscan_api/bailo_modelscan_api/test_main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
"""Test for the main.py file.
"""

from __future__ import annotations

from functools import lru_cache
from pathlib import Path
from unittest.mock import Mock, patch

import modelscan
from fastapi.testclient import TestClient

from .config import Settings
Expand All @@ -12,18 +17,24 @@
client = TestClient(app)


@lru_cache
def get_settings_override():
return Settings(download_dir=".")


app.dependency_overrides[get_settings] = get_settings_override


def test_health():
response = client.get("/health")
def test_info():
response = client.get("/info")

assert response.status_code == 200
assert response.json() == {"status": "healthy"}
assert response.json() == {
"apiName": get_settings_override().app_name,
"apiVersion": get_settings_override().app_version,
"scannerName": modelscan.__name__,
"modelscanVersion": modelscan.__version__,
}


@patch("modelscan.modelscan.ModelScan.scan")
Expand Down

0 comments on commit 15a426c

Please sign in to comment.