Spaces:
Sleeping

File size: 4,368 Bytes
0de3c15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
## 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.