Spaces:
Sleeping
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
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. | |