An open API service indexing awesome lists of open source software.

https://github.com/persteenolsen/fastapi-jwt-auth-dl-two

Python FastAPI ML Inference Service with ONNX Runtime and PyTorch-Trained Model for House Price Prediction (v5)
https://github.com/persteenolsen/fastapi-jwt-auth-dl-two

deep-learning fastapi jwt onnx python pytorch

Last synced: 9 days ago
JSON representation

Python FastAPI ML Inference Service with ONNX Runtime and PyTorch-Trained Model for House Price Prediction (v5)

Awesome Lists containing this project

README

          

# ๐Ÿ  v5 - House Price Prediction API (FastAPI + PyTorch + ONNX)

Last updated:

- 03-05-2026

A production-style machine learning backend system that predicts house prices using a neural network trained in PyTorch and deployed using ONNX Runtime with FastAPI.

This project demonstrates a complete end-to-end ML engineering workflow from data generation to secure API deployment, including model tuning for stable and realistic regression behavior.

---

## ๐Ÿš€ Features

- End-to-end ML pipeline (data โ†’ training โ†’ inference)
- Regression model for house price prediction
- Synthetic dataset generation with engineered features
- Feature scaling and target normalization
- PyTorch neural network training
- ONNX model export for fast inference
- FastAPI REST API for serving predictions
- JWT authentication (OAuth2 password flow)
- Environment-based configuration (.env)
- Production-ready dependency separation
- Vercel deployment compatible structure
- Tuned model for smooth and realistic price behavior

---

## ๐Ÿงฑ Tech Stack

- Python 3.12
- PyTorch
- NumPy
- ONNX Runtime
- FastAPI
- Uvicorn
- python-jose (JWT authentication)
- python-dotenv

---

## ๐Ÿ“ Project Structure

```
.
โ”œโ”€โ”€ main.py # FastAPI inference API
โ”œโ”€โ”€ train.py # Model training + ONNX export
โ”œโ”€โ”€ data.py # Synthetic dataset generator
โ”œโ”€โ”€ model.onnx # Exported ONNX model
โ”œโ”€โ”€ preprocessing.json # Normalization parameters
โ”œโ”€โ”€ requirements/
โ”‚ โ”œโ”€โ”€ train.txt
โ”‚ โ”œโ”€โ”€ dev.txt
โ”œโ”€โ”€ requirements.txt # Production (Vercel)
โ””โ”€โ”€ .env # Environment variables (not committed)
```
---

## โš™๏ธ Installation

git clone https://github.com/your-repo/house-price-api.git
cd house-price-api

python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate

pip install -r requirements/dev.txt

---

## ๐Ÿ” Environment Variables (.env)

JWT_SECRET=your_secret_key
JWT_ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=60

ADMIN_USERNAME=admin
ADMIN_PASSWORD=password

---

## ๐Ÿ‹๏ธ Training the Model

Generate dataset and train model:

python data.py
python train.py

Outputs:
- model.onnx
- preprocessing.json

---

## ๐Ÿ”ง Model Tuning

During development, the model was tuned to improve stability and realism of predictions.

Key tuning changes:

- Reduced hidden layer size (8 โ†’ 6 neurons)
- Lowered learning rate (0.01 โ†’ 0.002โ€“0.003)
- Increased training epochs (200 โ†’ 350)
- Added weight decay (L2 regularization)
- Introduced early stopping for training stability
- Improved numerical stability in normalization

Result:

- Smooth, monotonic price curves
- Stable age depreciation behavior
- More consistent size scaling
- Reduced high-range prediction jumps
- Better generalization without overfitting

---

## ๐Ÿš€ Run API Locally

uvicorn main:app --reload

Swagger UI:
http://127.0.0.1:8000/docs

---

## ๐Ÿ” Authentication

Get Token:

POST /token

username=admin
password=password

Use Token:

Authorization: Bearer

---

## ๐Ÿ“ก Prediction Endpoint

POST /predict

Example request with base values like in test.py:

{
"size": 100,
"rooms": 3,
"age": 10,
"distance": 5,
"income_area": 50
}

Response:

{
"predicted_price": 371053.23,
"user": {
"sub": "testuser",
"exp": 1777793908
}
}

Predictions are generated by a trained neural network and reflect learned relationships from the synthetic dataset.

---

## ๐Ÿงช Training Pipeline

- Generate synthetic housing dataset
- Apply feature engineering:
- size
- rooms
- age
- distance penalty
- income influence
- Normalize inputs and target values
- Train neural network in PyTorch using tuned hyperparameters
- Apply regularization for smoother behavior
- Export trained model to ONNX format
- Save preprocessing metadata for inference

---

## ๐Ÿš€ Deployment (Vercel)

Production environment uses:

requirements.txt

- ONNX Runtime for inference
- No PyTorch included in production
- FastAPI serverless-compatible design

---

## ๐Ÿ” System Comparison (XOR vs House Price Model v5)

XOR Project (Previous Version)
- Binary classification problem
- 2 input features
- Simple neural network (logic learning)
- Fixed dataset (truth table)
- Minimal preprocessing
- Educational focus only

House Price Project (Current Version)
- Regression problem (continuous output)
- 5 engineered input features
- Realistic synthetic dataset with noise
- Full preprocessing pipeline (X + Y normalization)
- Tuned neural network for stable behavior
- ONNX-based production inference
- JWT-secured API
- Production-style architecture

Key Upgrade:

ML concept demonstration โ†’ ML system engineering with tuning and deployment awareness

---

## ๐Ÿง  Final Summary

This project demonstrates a complete machine learning backend system covering the full lifecycle of an ML application:

- Data generation and feature engineering
- Model training using PyTorch
- Careful hyperparameter tuning for stable regression behavior
- Input and output normalization for stable learning
- Regularization and early stopping for generalization control
- Model export to ONNX for efficient inference
- FastAPI-based REST API for serving predictions
- JWT authentication with environment-based configuration
- Production-style separation between training and inference environments

It represents a transition from basic ML experimentation to real-world ML system design and tuning practices, similar to production machine learning services used in industry.

---

## ๐Ÿ‘จโ€๐Ÿ’ป Author

Built as a learning project to understand production-style machine learning system design, deployment, inference architecture, and model tuning in practice.