{"id":50928351,"url":"https://github.com/persteenolsen/fastapi-jwt-auth-dl-one","last_synced_at":"2026-06-17T01:31:08.871Z","repository":{"id":353501451,"uuid":"1219684114","full_name":"persteenolsen/fastapi-jwt-auth-dl-one","owner":"persteenolsen","description":"Python FastAPI ML Inference Service with ONNX Runtime and PyTorch-Trained Model","archived":false,"fork":false,"pushed_at":"2026-05-03T13:03:29.000Z","size":39,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-05-03T13:14:12.608Z","etag":null,"topics":["deep-learning","fastapi","jwt","onnx","python","pytorch"],"latest_commit_sha":null,"homepage":"https://fastapi-jwt-auth-dl-one.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-24T05:53:08.000Z","updated_at":"2026-05-03T13:03:32.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/persteenolsen/fastapi-jwt-auth-dl-one","commit_stats":null,"previous_names":["persteenolsen/fastapi-jwt-auth-dl-one"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/persteenolsen/fastapi-jwt-auth-dl-one","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/persteenolsen%2Ffastapi-jwt-auth-dl-one","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/persteenolsen%2Ffastapi-jwt-auth-dl-one/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/persteenolsen%2Ffastapi-jwt-auth-dl-one/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/persteenolsen%2Ffastapi-jwt-auth-dl-one/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/persteenolsen","download_url":"https://codeload.github.com/persteenolsen/fastapi-jwt-auth-dl-one/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/persteenolsen%2Ffastapi-jwt-auth-dl-one/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:08.112Z","updated_at":"2026-06-17T01:31:08.850Z","avatar_url":"https://github.com/persteenolsen.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 🚀 FastAPI ML Inference Service with ONNX Runtime and PyTorch-Trained Model\n\nLast updated:\n- 01-06-2026\n\nA production-style machine learning inference API demonstrating model training in PyTorch, export to ONNX, and efficient inference using FastAPI and ONNX Runtime solving the XOR Problem\n\n---\n\n## 🚀 What this project demonstrates\n\n- End-to-end ML workflow (training → export → inference)\n- Model training in PyTorch with deployment in ONNX format\n- Clear separation of training, development, and production environments\n- Secure API design with JWT authentication\n- Lightweight inference using ONNX Runtime\n- Input validation and controlled inference behavior\n\n---\n\n## 🚀 Features\n\n- FastAPI REST API\n- PyTorch-trained neural network (XOR problem)\n- ONNX Runtime inference engine\n- JWT authentication (OAuth2 password flow)\n- Pydantic request validation\n- Strict XOR input enforcement (0 or 1 only)\n- Custom error handling\n\n---\n\n## 📈 Notes\n\n- XOR is a classic toy problem used to demonstrate why neural networks require non-linear layers\n- Focus is system design, not model accuracy\n- Demonstrates full ML pipeline: training → export → deployment\n- Clear separation between training and production inference environments\n\n---\n\n## 🧠 Machine Learning Model\n\n- Framework: PyTorch\n- Architecture:\n  - Input layer: 2 features\n  - Hidden layer: 4 neurons (Tanh activation)\n  - Output layer: 1 neuron (Sigmoid activation)\n- Task: XOR binary classification\n- Model export: ONNX format for inference\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- Added Tanh function instead of ReLu\n- Reduced the hidden layer size (8 → 4 neurons)\n- Kept learning rate (0.01)\n- Kept training epochs (2000)\n\nResult:\n\n- Smoother and faster model\n---\n\n## 🔁 System Architecture\n\nClient → JWT Authentication → Token → Prediction Request → ONNX Runtime → Response\n\n---\n\n## 🔐 Authentication\n\n### Get token\n\n```bash\ncurl -X POST \"http://localhost:8000/token\" \\\n-H \"Content-Type: application/x-www-form-urlencoded\" \\\n-d \"username=admin\u0026password=password\"\n```\n\n### Response\n\n```json id=\"auth_response\"\n{\n  \"access_token\": \"your_token_here\",\n  \"token_type\": \"bearer\"\n}\n```\n\n### Use token\n\nAuthorization header:\n\nAuthorization: Bearer \u003ctoken\u003e\n\n---\n\n## 📊 Prediction Endpoint\n\n### Request\n\nPOST /predict\n\nBody:\n\n```json id=\"predict_request\"\n{\n  \"x1\": 0,\n  \"x2\": 1\n}\n```\n\n---\n\n### Response\n\n```json id=\"predict_response\"\n{\n  \"user\": \"admin\",\n  \"prediction\": 0.9989\n}\n```\n\n---\n\n## ⚠️ Input Rules\n\n| x1 | x2 | output |\n|----|----|--------|\n| 0  | 0  | 0 |\n| 0  | 1  | 1 |\n| 1  | 0  | 1 |\n| 1  | 1  | 0 |\n\nOnly binary inputs (0 or 1) are accepted.\n\n---\n\n## 🧪 Tech Stack\n\n- FastAPI  \n- PyTorch  \n- ONNX Runtime  \n- NumPy  \n- Pydantic  \n- Python-JOSE (JWT)\n\n---\n\n## 📁 Project Structure\n\n```\nproject/\n├── train.py              # Train model and export ONNX\n├── main.py               # FastAPI inference service\n├── model.onnx            # Exported model\n├── requirements/\n│   ├── train.txt         # Training dependencies\n│   ├── dev.txt           # Local development dependencies\n├── requirements.txt      # Production (Vercel) dependencies\n└── .env                  # Local configuration (not deployed)\n```\n---\n\n## 🧪 TRAINING ENVIRONMENT (train.txt)\n\nUsed ONLY for training the model locally.\n\ntorch\u003e=2.2,\u003c2.4\nonnx\u003e=1.16,\u003c2.0\nnumpy\u003e=1.26,\u003c2.0\n\n---\n\n## 💻 LOCAL DEVELOPMENT ENVIRONMENT (dev.txt)\n\nUsed to run FastAPI locally and test full pipeline.\n\n-r train.txt\n  \nfastapi\u003e=0.110,\u003c0.116\nuvicorn\u003e=0.29,\u003c0.31\npython-dotenv\u003e=1.0,\u003c2.0\npython-jose[cryptography]\u003e=3.3,\u003c4.0\nonnxruntime\u003e=1.17,\u003c1.20\nnumpy\u003e=1.26,\u003c2.0\npython-multipart\u003e=0.0.9\n\n---\n\n## 🚀 PRODUCTION ENVIRONMENT (requirements.txt)\n\nUsed for deployment (no PyTorch included).\n\nfastapi\u003e=0.110,\u003c0.116  \nuvicorn\u003e=0.29,\u003c0.31  \nonnxruntime\u003e=1.17,\u003c1.20  \nnumpy\u003e=1.26,\u003c2.0  \npython-dotenv\u003e=1.0,\u003c2.0  \npython-jose[cryptography]\u003e=3.3,\u003c4.0  \npython-multipart\u003e=0.0.9  \n\n---\n\n## 🧪 TRAINING PIPELINE\n\nInstall training dependencies:\n\n```bash\npip install -r requirements/train.txt\n```\n\nRun training:\n\n```bash\npython train.py\n```\n\nOutput:\nmodel.onnx\n\n---\n\n## 🚀 LOCAL DEVELOPMENT\n\nInstall dependencies:\n\n```bash\npip install -r requirements/dev.txt\n```\n\nRun API:\n\n```bash\nuvicorn main:app --reload\n```\n\nSwagger UI:\nhttp://127.0.0.1:8000/docs\n\n---\n\n## 🚀 DEPLOYMENT (VERCEL)\n\nProduction uses:\n\nrequirements.txt\n\nNo PyTorch included in deployment.\n\n---\n\n## 📈 Model Parameter Adjustments Guide\n\nThis guide outlines how common neural network parameters influence the training process and results. It also provides recommendations for adjusting them to optimize model performance.\n\n## 1. Number of Hidden Layers\n- **Increase:** Adds complexity, enabling the model to capture more features. However, it also increases the risk of overfitting. This is suitable for complex datasets.\n- **Decrease:** Results in a simpler model, which may underfit complex tasks but can work well for small or simple datasets.\n\n**Recommendation:**  \n- Start with 1-2 hidden layers.  \n- Use 3-5 layers for more complexity if the dataset requires it.\n\n---\n\n## 2. Number of Neurons (per Layer)\n- **Increase:** More neurons allow the model to learn more complex patterns. However, too many neurons increase the risk of overfitting if the data is limited.\n- **Decrease:** A simpler model with fewer neurons reduces overfitting but may fail to capture complex patterns.\n\n**Recommendation:**  \n- Start with 4-16 neurons per layer.  \n- Increase the number if the model is not capturing enough complexity.\n\n---\n\n## 3. Learning Rate (lr)\n- **Increase:** Speeds up learning but can cause the model to overshoot the optimal weights, potentially leading to poor convergence or instability.\n- **Decrease:** Results in slower, more stable convergence, but may get stuck in local minima or take too long to converge.\n\n**Recommendation:**  \n- Start with a learning rate between `0.001` and `0.01`.  \n- For faster convergence on simpler tasks, try between `0.1` and `0.5`.\n\n---\n\n## 4. Epochs\n- **Increase:** More epochs allow the model to learn longer, but too many can lead to overfitting (where the model memorizes the training data).\n- **Decrease:** Fewer epochs may cause underfitting, where the model hasn't learned enough.\n\n**Recommendation:**  \n- Start with 100-300 epochs.  \n- Monitor for overfitting and stop earlier if needed.\n\n---\n\n## 5. Weight Decay (Regularization)\n- **Increase:** Helps prevent overfitting by penalizing large weights. However, excessive decay can lead to underfitting.\n- **Decrease:** Provides more flexibility but increases the risk of overfitting.\n\n**Recommendation:**  \n- Start with weight decay values between `1e-4` and `1e-5`.  \n- Increase for larger networks or more complex datasets.\n\n---\n\n## Additional Tips:\n- **Hidden Layers and Neurons:** Start with simpler architectures and increase complexity only when necessary. Use cross-validation to test performance.\n- **Learning Rate:** Consider using adaptive optimizers like **Adam** for better learning rate control.\n- **Epochs:** Monitor validation loss and implement early stopping to prevent overfitting, especially with large datasets.\n- **Weight Decay:** Use weight decay to prevent overfitting, but be cautious not to overdo it, as this could lead to underfitting.\n\n---\n\n## Key Takeaways:\n- **Start simple:** Use 1-2 layers and 4-16 neurons.\n- **Adjust learning rate:** Reduce it if the model oscillates or diverges.\n- **Monitor overfitting:** Use early stopping or adjust epochs accordingly.\n- **Regularize with care:** Apply weight decay to prevent overfitting but avoid excessive regularization.\n\n---\n\n## 👨‍💻 Author\n\nLearning project for understanding machine learning system deployment and inference architecture","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpersteenolsen%2Ffastapi-jwt-auth-dl-one","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpersteenolsen%2Ffastapi-jwt-auth-dl-one","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpersteenolsen%2Ffastapi-jwt-auth-dl-one/lists"}