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)
- Host: GitHub
- URL: https://github.com/persteenolsen/fastapi-jwt-auth-dl-two
- Owner: persteenolsen
- Created: 2026-04-27T13:08:30.000Z (about 2 months ago)
- Default Branch: main
- Last Pushed: 2026-04-27T13:27:04.000Z (about 2 months ago)
- Last Synced: 2026-04-27T15:25:51.121Z (about 2 months ago)
- Topics: deep-learning, fastapi, jwt, onnx, python, pytorch
- Language: Python
- Homepage: https://fastapi-jwt-auth-dl-two.vercel.app/docs
- Size: 33.2 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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.