{"id":50928370,"url":"https://github.com/persteenolsen/fastapi-jwt-auth-dl-two","last_synced_at":"2026-06-17T01:31:12.295Z","repository":{"id":354187513,"uuid":"1222533269","full_name":"persteenolsen/fastapi-jwt-auth-dl-two","owner":"persteenolsen","description":"Python FastAPI ML Inference Service with ONNX Runtime and PyTorch-Trained Model for House Price Prediction (v5)","archived":false,"fork":false,"pushed_at":"2026-04-27T13:27:04.000Z","size":34,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-04-27T15:25:51.121Z","etag":null,"topics":["deep-learning","fastapi","jwt","onnx","python","pytorch"],"latest_commit_sha":null,"homepage":"https://fastapi-jwt-auth-dl-two.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-27T13:08:30.000Z","updated_at":"2026-04-27T13:29:09.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/persteenolsen/fastapi-jwt-auth-dl-two","commit_stats":null,"previous_names":["persteenolsen/fastapi-jwt-auth-dl-two"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/persteenolsen/fastapi-jwt-auth-dl-two","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/persteenolsen%2Ffastapi-jwt-auth-dl-two","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/persteenolsen%2Ffastapi-jwt-auth-dl-two/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/persteenolsen%2Ffastapi-jwt-auth-dl-two/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/persteenolsen%2Ffastapi-jwt-auth-dl-two/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/persteenolsen","download_url":"https://codeload.github.com/persteenolsen/fastapi-jwt-auth-dl-two/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/persteenolsen%2Ffastapi-jwt-auth-dl-two/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"],"created_at":"2026-06-17T01:31:11.075Z","updated_at":"2026-06-17T01:31:12.279Z","avatar_url":"https://github.com/persteenolsen.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 🏠 v5 - House Price Prediction API (FastAPI + PyTorch + ONNX)\n\nLast updated:\n\n- 03-05-2026\n\nA production-style machine learning backend system that predicts house prices using a neural network trained in PyTorch and deployed using ONNX Runtime with FastAPI.\n\nThis 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.\n\n---\n\n## 🚀 Features\n\n- End-to-end ML pipeline (data → training → inference)\n- Regression model for house price prediction\n- Synthetic dataset generation with engineered features\n- Feature scaling and target normalization\n- PyTorch neural network training\n- ONNX model export for fast inference\n- FastAPI REST API for serving predictions\n- JWT authentication (OAuth2 password flow)\n- Environment-based configuration (.env)\n- Production-ready dependency separation\n- Vercel deployment compatible structure\n- Tuned model for smooth and realistic price behavior\n\n---\n\n## 🧱 Tech Stack\n\n- Python 3.12\n- PyTorch\n- NumPy\n- ONNX Runtime\n- FastAPI\n- Uvicorn\n- python-jose (JWT authentication)\n- python-dotenv\n\n---\n\n## 📁 Project Structure\n\n```\n.\n├── main.py                 # FastAPI inference API\n├── train.py               # Model training + ONNX export\n├── data.py                # Synthetic dataset generator\n├── model.onnx            # Exported ONNX model\n├── preprocessing.json     # Normalization parameters\n├── requirements/\n│   ├── train.txt\n│   ├── dev.txt\n├── requirements.txt      # Production (Vercel)\n└── .env                  # Environment variables (not committed)\n```\n---\n\n## ⚙️ Installation\n\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---\n\n## 🔐 Environment Variables (.env)\n\nJWT_SECRET=your_secret_key  \nJWT_ALGORITHM=HS256  \nACCESS_TOKEN_EXPIRE_MINUTES=60  \n\nADMIN_USERNAME=admin  \nADMIN_PASSWORD=password  \n\n---\n\n## 🏋️ Training the Model\n\nGenerate dataset and train model:\n\npython data.py  \npython train.py  \n\nOutputs:\n- model.onnx\n- preprocessing.json\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- Reduced hidden layer size (8 → 6 neurons)\n- Lowered learning rate (0.01 → 0.002–0.003)\n- Increased training epochs (200 → 350)\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## 🚀 Run API Locally\n\nuvicorn main:app --reload  \n\nSwagger UI:  \nhttp://127.0.0.1:8000/docs  \n\n---\n\n## 🔐 Authentication\n\nGet Token:\n\nPOST /token  \n\nusername=admin  \npassword=password  \n\nUse Token:\n\nAuthorization: Bearer \u003ctoken\u003e  \n\n---\n\n## 📡 Prediction Endpoint\n\nPOST /predict  \n\nExample request with base values like in test.py:\n\n{\n  \"size\": 100,\n  \"rooms\": 3,\n  \"age\": 10,\n  \"distance\": 5,\n  \"income_area\": 50\n}\n\nResponse:\n\n{\n  \"predicted_price\": 371053.23,\n  \"user\": {\n    \"sub\": \"testuser\",\n    \"exp\": 1777793908\n  }\n}\n\nPredictions are generated by a trained neural network and reflect learned relationships from the synthetic dataset.\n\n---\n\n## 🧪 Training Pipeline\n\n- Generate synthetic housing dataset\n- Apply feature engineering:\n  - size\n  - rooms\n  - age\n  - distance penalty\n  - income influence\n- Normalize inputs and target values\n- Train neural network in PyTorch using tuned hyperparameters\n- Apply regularization for smoother behavior\n- Export trained model to ONNX format\n- Save preprocessing metadata for inference\n\n---\n\n## 🚀 Deployment (Vercel)\n\nProduction environment uses:\n\nrequirements.txt  \n\n- ONNX Runtime for inference\n- No PyTorch included in production\n- FastAPI serverless-compatible design\n\n---\n\n## 🔁 System Comparison (XOR vs House Price Model v5)\n\nXOR Project (Previous Version)\n- Binary classification problem\n- 2 input features\n- Simple neural network (logic learning)\n- Fixed dataset (truth table)\n- Minimal preprocessing\n- Educational focus only\n\nHouse Price Project (Current Version)\n- Regression problem (continuous output)\n- 5 engineered input features\n- Realistic synthetic dataset with noise\n- Full preprocessing pipeline (X + Y normalization)\n- Tuned neural network for stable behavior\n- ONNX-based production inference\n- JWT-secured API\n- Production-style architecture\n\nKey Upgrade:\n\nML concept demonstration → ML system engineering with tuning and deployment awareness\n\n---\n\n## 🧠 Final Summary\n\nThis project demonstrates a complete machine learning backend system covering the full lifecycle of an ML application:\n\n- Data generation and feature engineering\n- Model training using PyTorch\n- Careful hyperparameter tuning for stable regression behavior\n- Input and output normalization for stable learning\n- Regularization and early stopping for generalization control\n- Model export to ONNX for efficient inference\n- FastAPI-based REST API for serving predictions\n- JWT authentication with environment-based configuration\n- Production-style separation between training and inference environments\n\nIt 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.\n\n---\n\n## 👨‍💻 Author\n\nBuilt as a learning project to understand production-style machine learning system design, deployment, inference architecture, and model tuning in practice.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpersteenolsen%2Ffastapi-jwt-auth-dl-two","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpersteenolsen%2Ffastapi-jwt-auth-dl-two","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpersteenolsen%2Ffastapi-jwt-auth-dl-two/lists"}