Spaces:
Sleeping

ml-api / docs /documentation.md
Eric Dyke
FastAPI ML API for Spaceship Titanic and Digit Recognition
0de3c15
|
Raw History Blame Contribute Delete
4.37 kB
## 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.