{"id":50928354,"url":"https://github.com/persteenolsen/fastapi-jwt-auth-dl-four","last_synced_at":"2026-06-17T01:31:10.145Z","repository":{"id":356137690,"uuid":"1229925500","full_name":"persteenolsen/fastapi-jwt-auth-dl-four","owner":"persteenolsen","description":"Python FastAPI ML Inference Service with ONNX Runtime and PyTorch-Trained Model for House Price Prediction using Ames Dataset focusing on Tests (v8)","archived":false,"fork":false,"pushed_at":"2026-05-26T10:25:08.000Z","size":227,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-05-26T12:27:42.267Z","etag":null,"topics":["deep-learning","fastapi","jwt","onnx","python","pytorch","tests"],"latest_commit_sha":null,"homepage":"https://fastapi-jwt-auth-dl-four.vercel.app/docs","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/persteenolsen.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-05-05T14:08:47.000Z","updated_at":"2026-05-26T10:25:12.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/persteenolsen/fastapi-jwt-auth-dl-four","commit_stats":null,"previous_names":["persteenolsen/fastapi-jwt-auth-dl-four"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/persteenolsen/fastapi-jwt-auth-dl-four","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/persteenolsen%2Ffastapi-jwt-auth-dl-four","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/persteenolsen%2Ffastapi-jwt-auth-dl-four/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/persteenolsen%2Ffastapi-jwt-auth-dl-four/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/persteenolsen%2Ffastapi-jwt-auth-dl-four/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/persteenolsen","download_url":"https://codeload.github.com/persteenolsen/fastapi-jwt-auth-dl-four/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/persteenolsen%2Ffastapi-jwt-auth-dl-four/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34430688,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-16T02:00:06.860Z","response_time":126,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["deep-learning","fastapi","jwt","onnx","python","pytorch","tests"],"created_at":"2026-06-17T01:31:08.830Z","updated_at":"2026-06-17T01:31:10.086Z","avatar_url":"https://github.com/persteenolsen.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 🏠 v8 - House Price Prediction API (FastAPI + PyTorch + JWT + Ames Dataset + Tests + Vue 3 SPA)\n\nLast updated \n\n- 09-06-2026\n\nA production-style machine learning backend system that predicts house prices using a PyTorch neural network trained on the Ames Housing dataset and served through a secure FastAPI API with JWT authentication\n\nThis project demonstrates a full ML engineering pipeline: data preprocessing → feature engineering → model training → Tests → ONNX export → secure API inference\n\n# Vue 3 frontend for the Web API\n\n- [`The Vue 3 SPA at GitHub`](https://github.com/persteenolsen/vue-fastapi-jwt-auth-dl-four) - The Vue 3 SPA using JWT Authentication\n\n# 🧪 Tests\n\nThis project includes four test scripts to validate model predictions and overall quality\n\nNote: For running a test inside the folder \"tests\" use the command:  \n\n    python -m tests.testone\n\n### testone.py – Manual PyTorch vs ONNX Comparison\nCompares the PyTorch model predictions with the ONNX exported model on a set of hand-picked inputs.\n\nGr_Liv_Area=900  -\u003e PyTorch: $180,157, ONNX: $180,157, diff=0.167  \nGr_Liv_Area=1000 -\u003e PyTorch: $184,697, ONNX: $184,697, diff=0.178  \nGr_Liv_Area=1100 -\u003e PyTorch: $189,351, ONNX: $189,351, diff=0.181  \nGr_Liv_Area=1200 -\u003e PyTorch: $194,122, ONNX: $194,122, diff=0.186  \n\n### testtwo.py – Predictions for Custom Inputs\nTests specific example inputs to verify ONNX model alignment with PyTorch outputs.\n\nExample 1: PyTorch=$184,697, ONNX=$184,697, diff=0.178  \nExample 2: PyTorch=$257,907, ONNX=$257,907, diff=0.004  \n\n### testthree.py – Train/Test Loss Check\nEvaluates the model’s loss on train and test sets to confirm proper fitting.\n\nTrain Loss: 0.0471  \nTest Loss: 0.0531  \n✅ Model fit looks good  \n\n### testfour.py – Top Prediction Errors Analysis\nIdentifies the largest errors in the test set and calculates overall prediction accuracy.\n\nTop 5 largest prediction errors:  \nPredicted=$405,438, Actual=$118,500, Error=$286,938  \nPredicted=$342,997, Actual=$124,000, Error=$218,997  \nPredicted=$231,987, Actual=$35,311, Error=$196,676  \nPredicted=$248,036, Actual=$415,000, Error=$166,964  \nPredicted=$281,822, Actual=$138,887, Error=$142,935  \n\nAverage percentage error: 17.71%  \n⚠️ 66 houses have \u003e30% prediction error  \n\nThese tests ensure model consistency, alignment between PyTorch and ONNX predictions, training quality, and edge-case performance.\n\n# 📁 Refactoring of train.py\n\n- train.py was refactored into classes and modules using the new files for tests \n\n# 🚀 Features\n\n- End-to-end ML pipeline using real-world Ames Housing dataset  \n- Feature engineering (HouseAge, HasGarage derived features)  \n- Robust feature alignment between training and inference  \n- Input normalization for stable neural network training  \n- PyTorch neural network regression model  \n- Log-transformed target (log1p) for stable regression learning  \n- ONNX export for fast production inference  \n- Secure REST API using FastAPI  \n- JWT authentication (OAuth2 password flow)  \n- Named-feature JSON input (no manual ordering required)  \n- Consistent preprocessing across training and inference  \n- Serverless-ready deployment design (Vercel compatible)\n\n# 🧱 Tech Stack\n\n- Python 3.12  \n- FastAPI  \n- PyTorch  \n- ONNX Runtime  \n- NumPy  \n- Pandas  \n- scikit-learn  \n- python-jose (JWT auth)  \n- Uvicorn  \n- joblib  \n\n# 📁 Project Structure\n\n```\n    .\n    ├── main.py                # FastAPI inference API (JWT + ONNX)\n    ├── train.py               # Model training + ONNX export\n    ├── features.py            # Shared feature definitions + transforms\n    ├── AmesHousing.csv        # Dataset\n    ├── model.onnx             # Exported inference model\n    ├── mean.npy               # Feature normalization mean\n    ├── std.npy                # Feature normalization std\n    ├── requirements.txt\n    └── .env                   # Environment variables (not committed)\n    └── tests                  # Tests\n```\n\n# ⚙️ Installation\n\n    git clone https://github.com/your-repo/house-price-api.git\n    cd house-price-api\n    python -m venv .venv\n    source .venv/bin/activate  # Windows: .venv\\Scripts\\activate\n    pip install -r requirements/dev.txt\n\nVerify setup:\n\n    python -c \"import fastapi, numpy, onnxruntime; print('VENV OK')\"\n\nExpected output:\n\n    VENV OK\n\n# 🔐 Environment Variables (.env)\n\n    SECRET_KEY=your_secret_key\n    ALGORITHM=HS256\n    ACCESS_TOKEN_EXPIRE_MINUTES=60\n    ADMIN_USERNAME=admin\n    ADMIN_PASSWORD=password\n\n# 🏋️ Training the Model\n\n    python train.py\n\nOutputs:\n\n- model.onnx  \n- mean.npy  \n- std.npy  \n\n# 🔧 Model Tuning\n\n- Removed a hidden layer  \n- Reduced the hidden layer size (16 → 4 neurons)  \n- Lowered learning rate (0.01 → 0.006)  \n- Increased training epochs (500 → 1000)  \n- Added weight decay (L2 regularization)  \n- Introduced early stopping for training stability  \n- Improved numerical stability in normalization  \n\nResult:\n\n- Smooth, monotonic price curves  \n- Stable age depreciation behavior  \n- More consistent size scaling  \n- Reduced high-range prediction jumps  \n- Better generalization without overfitting\n\n# 🔬 Training Pipeline\n\n- Load Ames Housing dataset  \n- Feature engineering: HouseAge (derived), HasGarage (binary feature)  \n- Select final feature set  \n- Normalize features (mean/std scaling)  \n- Train PyTorch neural network  \n- Optimize regression using log1p target  \n- Export model to ONNX format  \n- Save normalization parameters for inference\n\n# 🚀 Run API Locally\n\n    uvicorn main:app --reload\n\nSwagger UI:\n\n    http://127.0.0.1:8000/docs\n\n# 🔐 Authentication\n\nPOST `/login`\n\n    username=admin\n    password=password\n\nResponse:\n\n    {\n      \"access_token\": \"jwt_token_here\",\n      \"token_type\": \"bearer\"\n    }\n\nUse Token:\n\n    Authorization: Bearer \u003ctoken\u003e\n\n# 📡 Prediction Endpoint\n\nPOST `/predict`\n\nExample:\n\n    {\n      \"Gr_Liv_Area\": 1500,\n      \"Overall_Qual\": 7,\n      \"Year_Built\": 2005,\n      \"Garage_Cars\": 2,\n      \"Full_Bath\": 2,\n      \"Bedroom_AbvGr\": 3,\n      \"Lot_Area\": 8000\n    }\n\nResponse:\n\n   {\n     \"predicted_price\": 209169.703125\n  }\n\nAnother example:\n\n    {\n      \"Gr_Liv_Area\": 1200,\n      \"Overall_Qual\": 7,\n      \"Year_Built\": 2005,\n      \"Garage_Cars\": 2,\n      \"Full_Bath\": 2,\n      \"Bedroom_AbvGr\": 3,\n      \"Lot_Area\": 8000\n    }\n\nResponse:\n\n   {\n     \"predicted_price\": 194122.25\n   }\n\n# Clamping Predicted Price\n\n- Maximum value: $755,000  \n- Reverse log-transform, clamp values above cap  \n\n# 🧠 Key Design Feature\n\n- Named-feature system (safe ML design)  \n- No manual feature ordering in API  \n- Centralized feature definition (`features.py`)  \n- Automatic transformation + alignment  \n- Prevents silent prediction errors  \n\n# 🔁 Training vs Inference Consistency\n\n- Training: defines feature space, applies normalization, trains PyTorch, exports ONNX + normalization params  \n- Inference: receives JSON, applies same transforms, normalization, runs ONNX, returns prediction  \n\n# 🧪 System Flow\n\n1. User logs in → receives JWT  \n2. Sends house feature JSON  \n3. API verifies JWT, builds feature vector, applies normalization, runs ONNX inference, returns predicted price  \n\n# 🧠 What this project demonstrates\n\n- Production-style ML system architecture  \n- Feature engineering for tabular regression  \n- Train/inference consistency design  \n- Secure API authentication (JWT)  \n- ONNX-based model deployment  \n- Serverless-compatible ML inference design \n\n# 👨‍💻 Author\n\nBuilt as a machine learning engineering project demonstrating production-style ML system design using FastAPI, PyTorch, and ONNX for real-world deployment scenarios.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpersteenolsen%2Ffastapi-jwt-auth-dl-four","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpersteenolsen%2Ffastapi-jwt-auth-dl-four","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpersteenolsen%2Ffastapi-jwt-auth-dl-four/lists"}