Spaces:
Sleeping
Sleeping
Commit ·
3a962ff
0
Parent(s):
chore: Initial project structure
Browse files- .gitattributes +1 -0
- .gitignore +20 -0
- README.md +89 -0
- requirements.txt +12 -0
.gitattributes
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__pycache__/
|
| 2 |
+
*.py[cod]
|
| 3 |
+
*$py.class
|
| 4 |
+
.env
|
| 5 |
+
.venv
|
| 6 |
+
env/
|
| 7 |
+
venv/
|
| 8 |
+
.pytest_cache/
|
| 9 |
+
.coverage
|
| 10 |
+
htmlcov/
|
| 11 |
+
*.db
|
| 12 |
+
*.sqlite
|
| 13 |
+
*.pkl
|
| 14 |
+
*.csv
|
| 15 |
+
# Keep the specific model and data files if they are part of the repo, but usually data is ignored.
|
| 16 |
+
# User context implies they are provided files, so maybe we keep them or ignore them.
|
| 17 |
+
# For this exercise, I'll assume we track them or they are already there.
|
| 18 |
+
# Let's ignore the generated schemas text files
|
| 19 |
+
generated_schemas.txt
|
| 20 |
+
generated_schemas_v2.txt
|
README.md
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ML Prediction API
|
| 2 |
+
|
| 3 |
+
Production-ready POC for serving an XGBoost model via FastAPI with PostgreSQL logging.
|
| 4 |
+
|
| 5 |
+
## Features
|
| 6 |
+
|
| 7 |
+
- **FastAPI**: High-performance API with automatic validation.
|
| 8 |
+
- **PostgreSQL**: Persistent logging of all inputs and predictions.
|
| 9 |
+
- **XGBoost**: Efficient model inference.
|
| 10 |
+
- **Docker**: Containerized for easy deployment.
|
| 11 |
+
- **CI/CD**: Automated testing and deployment via GitHub Actions.
|
| 12 |
+
- **Authentication**: Simple API Key protection.
|
| 13 |
+
|
| 14 |
+
## Installation
|
| 15 |
+
|
| 16 |
+
### Prerequisites
|
| 17 |
+
- Docker
|
| 18 |
+
- Python 3.10+
|
| 19 |
+
- PostgreSQL (if running locally without Docker Compose)
|
| 20 |
+
|
| 21 |
+
### Local Setup
|
| 22 |
+
|
| 23 |
+
1. **Clone the repository**
|
| 24 |
+
```bash
|
| 25 |
+
git clone <repo-url>
|
| 26 |
+
cd <repo-name>
|
| 27 |
+
```
|
| 28 |
+
|
| 29 |
+
2. **Install Dependencies**
|
| 30 |
+
```bash
|
| 31 |
+
pip install -r requirements.txt
|
| 32 |
+
```
|
| 33 |
+
|
| 34 |
+
3. **Environment Variables**
|
| 35 |
+
Create a `.env` file or set variables:
|
| 36 |
+
```bash
|
| 37 |
+
export DATABASE_URL="postgresql://user:password@localhost:5432/dbname"
|
| 38 |
+
export API_KEY="your-secret-key"
|
| 39 |
+
export MODEL_PATH="02_xgb_model_tuned.pkl"
|
| 40 |
+
```
|
| 41 |
+
|
| 42 |
+
4. **Initialize Database**
|
| 43 |
+
```bash
|
| 44 |
+
python scripts/create_db.py
|
| 45 |
+
```
|
| 46 |
+
|
| 47 |
+
5. **Run the API**
|
| 48 |
+
```bash
|
| 49 |
+
uvicorn app.main:app --reload
|
| 50 |
+
```
|
| 51 |
+
|
| 52 |
+
## API Documentation
|
| 53 |
+
|
| 54 |
+
Once running, visit:
|
| 55 |
+
- Swagger UI: `http://localhost:8000/docs`
|
| 56 |
+
- ReDoc: `http://localhost:8000/redoc`
|
| 57 |
+
|
| 58 |
+
### Endpoints
|
| 59 |
+
|
| 60 |
+
- `POST /predict`: Make a prediction. Requires `X-API-KEY` header.
|
| 61 |
+
- `GET /health`: Health check.
|
| 62 |
+
- `GET /model/info`: Model metadata. Requires `X-API-KEY` header.
|
| 63 |
+
|
| 64 |
+
## Deployment
|
| 65 |
+
|
| 66 |
+
### Hugging Face Spaces
|
| 67 |
+
|
| 68 |
+
This project is configured to deploy to Hugging Face Spaces using Docker.
|
| 69 |
+
|
| 70 |
+
1. Create a new Space on Hugging Face (Select Docker SDK).
|
| 71 |
+
2. Add the following Secrets in the Space settings:
|
| 72 |
+
- `DATABASE_URL`: Connection string to your external PostgreSQL database.
|
| 73 |
+
- `API_KEY`: Your desired API key.
|
| 74 |
+
3. The GitHub Action `ci-cd.yml` will automatically deploy changes from the `main` branch.
|
| 75 |
+
|
| 76 |
+
## Architecture
|
| 77 |
+
|
| 78 |
+
- **app/main.py**: Entry point.
|
| 79 |
+
- **app/routers**: API route definitions.
|
| 80 |
+
- **app/services**: Business logic (ML inference).
|
| 81 |
+
- **app/models**: Database and Pydantic models.
|
| 82 |
+
- **app/core**: Configuration and DB setup.
|
| 83 |
+
|
| 84 |
+
## Testing
|
| 85 |
+
|
| 86 |
+
Run tests with coverage:
|
| 87 |
+
```bash
|
| 88 |
+
pytest --cov=app tests/
|
| 89 |
+
```
|
requirements.txt
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
xgboost
|
| 4 |
+
pandas
|
| 5 |
+
scikit-learn
|
| 6 |
+
sqlalchemy
|
| 7 |
+
psycopg2-binary
|
| 8 |
+
pytest
|
| 9 |
+
pytest-cov
|
| 10 |
+
httpx
|
| 11 |
+
pydantic
|
| 12 |
+
python-multipart
|