Spaces:
Sleeping
Download docs/documentation.md from HighFive52/ml-api: direct link, hf CLI and curl.
- Browser
- Download file 4.37 kB
-
https://huggingface.co/spaces/HighFive52/ml-api/resolve/main/docs/documentation.md
- Command line
-
hf download hf://spaces/HighFive52/ml-api/docs/documentation.md
-
curl -L -o documentation.md https://huggingface.co/spaces/HighFive52/ml-api/resolve/main/docs/documentation.md
Pre-commit Hooks
This project uses pre-commit to automatically check and format code before each commit.
Setup
Install pre-commit if you haven't already:
pip install pre-commitInstall the git hook scripts:
pre-commit installNow, 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:
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:
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
Activate your virtual environment
.venv\Scripts\Activate.ps1Install dependencies
pip install -r requirements.txtStart the API server
uvicorn app:app --reloadThe server will start at http://127.0.0.1:8000
Test the API
- Swagger UI: http://127.0.0.1:8000/docs
- 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
Make sure you have all dependencies installed:
pip install -r requirements.txt pip install pytest httpxRun the tests from the project root with the correct Python path:
$env:PYTHONPATH="."; pytestThis ensures that your app and modules are correctly imported during testing.
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:
docker build -t ml-api .
docker run -p 8000:8000 ml-api
For more details, see the README.md.