An open API service indexing awesome lists of open source software.

https://github.com/persteenolsen/fastapi-jwt-auth-ml-one

Python FastAPI with JWT Auth serving ML for House Price Prediction
https://github.com/persteenolsen/fastapi-jwt-auth-ml-one

fastapi jwt machine-learning python

Last synced: 9 days ago
JSON representation

Python FastAPI with JWT Auth serving ML for House Price Prediction

Awesome Lists containing this project

README

          

# 🏠 v1 - House Price Prediction API (FastAPI + JWT + ML)

Last updated:

- 19-04-2026

A simple machine learning API built with FastAPI that predicts house prices based on house size and number of rooms.
The API is secured using JWT authentication and serves a trained scikit-learn model.

---

## ✨ Features

- JWT authentication (login + protected routes)
- Machine Learning model (Linear Regression)
- FastAPI REST API
- Training pipeline using CSV dataset
- Model saved with Joblib
- Swagger UI (`/docs`) for testing

---

## 🧰 Tech Stack

- FastAPI
- Uvicorn
- Scikit-learn
- Pandas
- NumPy
- PyJWT
- Joblib
- Python-dotenv

---

## 📁 Project Structure

- app.py → FastAPI application (API + JWT + ML)
- train.py → Model training script
- data.csv → Training dataset
- model.pkl → Saved trained model
- requirements.txt → Dependencies
- .env → Environment variables

---

## ⚙️ Installation

### 1. Clone repository
git clone
cd project

---

### 2. Create virtual environment (optional)
python -m venv venv

Activate:
- Windows:
venv\Scripts\activate

- Mac/Linux:
source venv/bin/activate

---

### 3. Install dependencies
pip install -r requirements.txt

---

### 4. Create .env file

SECRET_KEY=your_secret_key
FAKE_USERNAME=admin
FAKE_PASSWORD=1234

---

## 🧠 Train the Model

Run:

python train.py

This will:
- Load data.csv
- Train a Linear Regression model
- Save model as model.pkl

---

## 🚀 Run the API

Start server:

python app.py

Open:

http://localhost:8000

http://localhost:8000/docs

---

## 🔐 Authentication Flow

### Step 1: Login

POST /login

{
"username": "admin",
"password": "1234"
}

Response:

{
"token": "your_jwt_token"
}

---

### Step 2: Authorize in Swagger

Click "Authorize 🔒" and enter:

Bearer

---

## 🏠 Prediction Endpoint

POST /predict (protected)

Request:

{
"size": 90,
"rooms": 3
}

Response:

{
"user": "admin",
"input": {
"size": 90,
"rooms": 3
},
"predicted_price": 270000
}

---

## 🧠 How it works

- data.csv is used to train the model
- Linear Regression learns relationship between:
- size
- rooms
- Model is saved as model.pkl
- FastAPI loads model at startup
- JWT protects prediction endpoint

---

## 📈 What this project demonstrates

- Learn FastAPI + ML integration
- Build secure ML APIs
- Portfolio project
- Base for production ML services

---

## 🚀 Future Improvements

- Use real datasets (Kaggle)
- Add database (PostgreSQL)
- Add user roles
- Dockerize project
- Deploy to other cloud than Vercel if wanted
- Splitting the app.py into files and folders for better structure

---

## 👨‍💻 Author

Learning project combining:
FastAPI + JWT + Machine Learning

---

## 📜 License

Educational use only ...

Happy coding :-)