{"id":50928368,"url":"https://github.com/persteenolsen/fastapi-jwt-auth-dl-three","last_synced_at":"2026-06-17T01:31:11.239Z","repository":{"id":354447673,"uuid":"1223679627","full_name":"persteenolsen/fastapi-jwt-auth-dl-three","owner":"persteenolsen","description":"Python FastAPI ML Inference Service with ONNX Runtime and PyTorch-Trained Model for House Price Prediction using Ames Dataset (v6)","archived":false,"fork":false,"pushed_at":"2026-04-28T16:41:25.000Z","size":223,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-04-28T17:06:06.654Z","etag":null,"topics":["deep-learning","fastapi","jwt","onnx","python"],"latest_commit_sha":null,"homepage":"https://fastapi-jwt-auth-dl-three.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-04-28T14:51:07.000Z","updated_at":"2026-04-28T16:41:29.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/persteenolsen/fastapi-jwt-auth-dl-three","commit_stats":null,"previous_names":["persteenolsen/fastapi-jwt-auth-dl-three"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/persteenolsen/fastapi-jwt-auth-dl-three","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/persteenolsen%2Ffastapi-jwt-auth-dl-three","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/persteenolsen%2Ffastapi-jwt-auth-dl-three/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/persteenolsen%2Ffastapi-jwt-auth-dl-three/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/persteenolsen%2Ffastapi-jwt-auth-dl-three/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/persteenolsen","download_url":"https://codeload.github.com/persteenolsen/fastapi-jwt-auth-dl-three/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/persteenolsen%2Ffastapi-jwt-auth-dl-three/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"],"created_at":"2026-06-17T01:31:10.604Z","updated_at":"2026-06-17T01:31:11.225Z","avatar_url":"https://github.com/persteenolsen.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 🏠 v6 - House Price Prediction API (FastAPI + PyTorch + JWT + Ames Dataset)\n\nLast updated \n\n- 05-05-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 → ONNX export → secure API inference.\n\n# 👨‍💻 Things I learned\n\n- For Ames Housing Dataset which are messy and noisy tabular data using a Neural Network was an interesting experience\n\n- My v7 using Linear Regression performs \"better\" with Ames Housing Dataset, but both models have their pros and cons \n\nHowever, I got experience with PyTorch and compared PyToch Neural Network v6 with Linear Regression v7. PyTorch would be a good choice for massive datsets and image classification\n\nTake a look at the section Model Tuning\n\n---\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---\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---\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```\n\n---\n\n# ⚙️ Installation\n\n```bash\ngit clone https://github.com/your-repo/house-price-api.git\ncd house-price-api\n\npython -m venv .venv\nsource .venv/bin/activate  # Windows: .venv\\Scripts\\activate\n\npip install -r requirements/dev.txt\n```\n\nVerify setup:\n\n```bash\npython -c \"import fastapi, numpy, onnxruntime; print('VENV OK')\"\n```\n\nExpected output:\n\n```\nVENV OK\n```\n\n---\n\n# 🔐 Environment Variables (.env)\n\n```\nSECRET_KEY=your_secret_key\nALGORITHM=HS256\nACCESS_TOKEN_EXPIRE_MINUTES=60\n\nADMIN_USERNAME=admin\nADMIN_PASSWORD=password\n\n```\n\n---\n\n# 🏋️ Training the Model\n\n```bash\npython train.py\n```\n\nOutputs:\n- model.onnx  \n- mean.npy  \n- std.npy  \n\n---\n\n## 🔧 Model Tuning\n\nDuring development, the model was tuned to improve stability and realism of predictions.\n\nKey tuning changes:\n\n- Removed a hidden layer\n- Reduced the hidden layer size (16 → 4 neurons)\n- Lowered learning rate (0.01 → 0.005)\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---\n\n# 🔬 Training Pipeline\n\n- Load Ames Housing dataset  \n- Feature engineering:\n  - HouseAge (derived)\n  - 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---\n\n# 🚀 Run API Locally\n\n```bash\nuvicorn main:app --reload\n```\n\nSwagger UI:\n```\nhttp://127.0.0.1:8000/docs\n```\n\n---\n\n# 🔐 Authentication\n\n## Login\n\nPOST `/login`\n\n```\nusername=admin\npassword=password\n```\n\nResponse:\n\n```json\n{\n  \"access_token\": \"jwt_token_here\",\n  \"token_type\": \"bearer\"\n}\n```\n\n## Use Token\n\n```\nAuthorization: Bearer \u003ctoken\u003e\n```\n\n---\n\n# 📡 Prediction Endpoint\n\nPOST `/predict`\n\n## Example requests\n\n \"Gr_Liv_Area\" = 1500\n\n```json\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```\n\n## Response\n\n```json\n{\n  \"predicted_price\": 216979.625\n}\n```\n\n\n\"Gr_Liv_Area\" = 1200 gives lower price like expected\n\n```json\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```\n\n## Response\n\n```json\n{\n  \"predicted_price\": 197978.359375\n}\n```\n---\n\n## Clamping Predicted Price\n\nTo avoid unrealistic house price predictions, the model clamps the output to a maximum value of **$755,000**. After predicting, the price is reverse log-transformed and if it exceeds the cap, it's set to this maximum value.\n\nExample:\n\n1. Model predicts a price.\n2. The price is transformed using `np.expm1()`.\n3. If the price exceeds $755,000, it's clamped to that value.\n\nThis ensures predictions stay within a realistic range.\n\n---\n\n# 🧠 Key Design Feature\n\n### Named-feature system (safe ML design)\n\n- No manual feature ordering in API  \n- Centralized feature definition (`features.py`)  \n- Automatic transformation + alignment  \n- Prevents silent prediction errors  \n\n---\n\n# 🔁 Training vs Inference Consistency\n\n## Training:\n- defines feature space in `features.py`\n- applies normalization (mean/std)\n- trains PyTorch model\n- exports ONNX + normalization params  \n\n## Inference:\n- receives named JSON input  \n- applies same feature transform  \n- applies identical normalization  \n- runs ONNX model  \n- returns real-world prediction  \n\n---\n\n# 🧪 System Flow\n\n1. User logs in → receives JWT  \n2. Sends house feature JSON  \n3. API:\n   - verifies JWT  \n   - builds feature vector  \n   - applies normalization  \n   - runs ONNX inference  \n   - returns predicted price  \n\n---\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---\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-three","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpersteenolsen%2Ffastapi-jwt-auth-dl-three","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpersteenolsen%2Ffastapi-jwt-auth-dl-three/lists"}