## Pre-commit Hooks This project uses [pre-commit](https://pre-commit.com/) to automatically check and format code before each commit. ### Setup 1. Install pre-commit if you haven't already: ```powershell pip install pre-commit ``` 2. Install the git hook scripts: ```powershell pre-commit install ``` 3. Now, every time you run `git commit`, pre-commit will automatically run Black (and any other configured hooks) on your code. ### Configuration The configuration is in `.pre-commit-config.yaml`. Example for Black: ```yaml repos: - repo: https://github.com/psf/black rev: 24.10.0 hooks: - id: black language_version: python3.12 files: '\.py$' ``` ### Manually Run All Hooks You can manually run all pre-commit hooks on all files with: ```powershell pre-commit run --all-files ``` # ML API Project Documentation ## Project Structure ``` ml-api/ │ ├── app.py # Main FastAPI application entry point ├── Dockerfile # Docker configuration ├── requirements.txt # Python dependencies ├── README.md # Project overview ├── .gitignore # Git ignore rules │ ├── ml/ # Machine learning code │ ├── common/ │ │ └── model_loader.py │ ├── digits/ │ │ └── mnist_model.py │ └── spaceship/ │ └── spaceship_transformers.py │ ├── models/ # Saved model files │ ├── digit_recognizer/ │ │ └── mnist_checkpoint.pth │ └── spaceship/ │ ├── hgb_tuned.pkl │ ├── lgbm_tuned.pkl │ └── xgb_tuned.pkl │ ├── routers/ # FastAPI routers │ ├── digits.py │ └── spaceship.py │ ├── tests/ # Tests and test data │ ├── test_api.py │ └── data/ │ ├── test_digits.csv │ └── test_spaceship.csv │ └── docs/ └── documentation.md # This file ``` ## How to Start the API Locally 1. **Activate your virtual environment** ```powershell .venv\Scripts\Activate.ps1 ``` 2. **Install dependencies** ```powershell pip install -r requirements.txt ``` 3. **Start the API server** ```powershell uvicorn app:app --reload ``` The server will start at [http://127.0.0.1:8000](http://127.0.0.1:8000) 4. **Test the API** - Swagger UI: [http://127.0.0.1:8000/docs](http://127.0.0.1:8000/docs) - Redoc: [http://127.0.0.1:8000/redoc](http://127.0.0.1:8000/redoc) ## API Endpoints - `/digit-recognizer/predict` (POST): Upload a CSV of digit images (flattened 28x28) to get predictions. - `/spaceship-titanic/predict` (POST): Upload a CSV of spaceship passenger data to get survival predictions. - `/health` (GET): Health check endpoint. ## Model and Transformer Notes - Custom transformers for Spaceship Titanic are in `ml/spaceship/spaceship_transformers.py`. - The digit recognizer model is defined in `ml/digits/mnist_model.py`. - Model loading logic is in `ml/common/model_loader.py`. - Saved models are in the `models/` directory. ## Testing - Test scripts and sample data are in the `tests/` folder. ### How to Run Tests 1. Make sure you have all dependencies installed: ```powershell pip install -r requirements.txt pip install pytest httpx ``` 2. Run the tests from the project root with the correct Python path: ```powershell $env:PYTHONPATH="."; pytest ``` This ensures that your app and modules are correctly imported during testing. 3. All tests in the `tests/` folder will be discovered and executed automatically. ## Updating Models or Transformers If you change or add custom transformers or functions: - Update both your training code and API code to import them from the same file (e.g., `ml/spaceship/spaceship_transformers.py`). - Retrain and re-save your models so the references are correct. ## Docker To build and run the API with Docker: ```powershell docker build -t ml-api . docker run -p 8000:8000 ml-api ``` --- For more details, see the README.md.