The API module

This module contains all the classes used for the API and the related Dockerfile.

Dockerfile

This Dockerfile builds a Python 3.11 container for a FastAPI web service. It does the following things:

  • sets up the app directory
  • installs dependencies from requirements.txt
  • copies API code, classification module and model cards
  • exposes port 8080
  • creates a non-root "app" user
  • launches uvicorn on api.main:app

Modules

main.py

This module defines the main FastAPI application, handling model loading, prediction requests, and basic health checks while syncing models from MLflow at startup.

Components

get_predictor(lang: str, model_type: str): LRU-cached factory that lazily loads a ModelPredictor instance for specific language/model combinations from the MODELS_DIR, avoiding upfront memory overhead.

lifespan(app: FastAPI): Async context manager that syncs the best models from MLflow to disk on startup, logs directory status, and clears the predictor cache on shutdown.

app FastAPI instance: Configured with title, description, CORS middleware for frontend origins (localhost by default), and the lifespan handler; serves interactive docs at /docs.

_build_response(results: dict, request: Request): Helper constructs standardized JSON responses with metadata like timestamp, method, status, and optional data.

construct_response(f): Decorator that wraps endpoint functions (sync/async) to ensure consistent JSON response formatting via _build_response.

Endpoints

/ (GET): Root health check returning a welcome message pointing to /docs.

/privacy (GET): Static privacy notice confirming no data persistence.

/status (GET): Simple running status indicator.

/models (GET): Scans MODELS_DIR to list available language/model_type pairs dynamically.

/predict (POST): Core inference endpoint; validates PredictRequest payload, loads predictor on-demand, runs classification on input text, and returns predictions list or detailed errors.## main.py

schemas.py

This module defines Pydantic schemas for the code comment classification API's predict request/response, using enums for supported programming languages and model types.

Enums

ProgrammingLanguage: Enum restricting inputs to "java", "python", or "pharo" for language selection.

ModelType: Enum limiting model choices to "setfit", "random_forest", or "transformer".

Schemas

PredictRequest: BaseModel requiring text (str), language (ProgrammingLanguage), and model_type (ModelType); includes JSON example for docs.

PredictResponse: BaseModel for outputs with label (str) and score (float).

sync_models.py

This module synchronizes the latest MLflow models for code comment classification (Python, Java, Pharo) from a remote registry to local disk, normalizing the directory structure for API use.

Functions

_get_mlflow_client(): Creates an MLflow client configured via MLFLOW_TRACKING_URI and auth env vars.

_find_champion_version_for_language(client: MlflowClient, lang: str): Scans registered models prefixed with <lang>-, finds the version aliased as <lang>-champion, returns ModelVersion or None.

sync_best_models_to_disk(models_root: str | Path = "models", api_subdir: str = "api"): Main function; for each language, downloads champion model artifacts to models/api/<lang>/<model_type>/, flattens transformer model subdirs, handles errors and overwrites existing dirs.