This repository contains a FastAPI-based API for predicting heart disease risk using machine learning.
- Python 3.10 or higher
- pip (Python package manager)
- Git
-
Clone the repository:
git clone https://github.com/seanthw/heart_disease_prediction_app_lablab.git cd heart_disease_prediction
-
Create a virtual environment:
python -m venv venv
-
Activate the virtual environment:
- On Windows:
venv\Scripts\activate
- On macOS and Linux:
source venv/bin/activate
- On Windows:
-
Install the required packages:
pip install -r requirements.txt
-
Ensure the
heart_disease_model.pkl
file is in thebackend/models/
directory. -
Set up the database:
- The application uses SQLite by default. The database file will be created automatically when you run the application for the first time.
- If you want to use a different database, update the
DATABASE_URL
inbackend/app/database.py
.
-
(Optional) Set environment variables:
SECRET_KEY
: Used for token encryption. If not set, a default value will be used.ALGORITHM
: JWT algorithm. Default is HS256.ACCESS_TOKEN_EXPIRE_MINUTES
: Token expiration time. Default is 30 minutes.
-
Navigate to the backend directory:
cd backend
-
Start the FastAPI server:
uvicorn app.main:app --host 0.0.0.0 --port 8000
-
The API will be available at
http://localhost:8000
.
Endpoint: POST /register
Example Request:
curl -X POST "http://localhost:8000/register" \
-H "Content-Type: application/json" \
-d '{"email": "[email protected]", "password": "strongpassword123"}'
Example Response:
{
"email": "[email protected]",
"id": 1
}
Endpoint: POST /token
Example Request:
curl -X POST "http://localhost:8000/token" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "[email protected]&password=strongpassword123"
Example Response:
{
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"token_type": "bearer"
}
Endpoint: POST /predict
Example Request:
curl -X POST "http://localhost:8000/predict" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"age": 50,
"sex": 1,
"cp": 0,
"trestbps": 140,
"chol": 233,
"fbs": 1,
"restecg": 0,
"thalach": 150,
"exang": 0,
"oldpeak": 2.3,
"slope": 0,
"ca": 0,
"thal": 1
}'
Example Response:
{
"prediction": 1,
"probability": 0.75,
"risk_category": "High",
"message": "This prediction suggests a higher likelihood of heart disease. Please consult with a healthcare professional for proper evaluation and advice."
}
Endpoint: GET /data
Example Request:
curl -X GET "http://localhost:8000/data" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN"
Example Response:
[
{
"id": 1,
"age": 50,
"sex": 1,
"cp": 0,
"trestbps": 140,
"chol": 233,
"fbs": 1,
"restecg": 0,
"thalach": 150,
"exang": 0,
"oldpeak": 2.3,
"slope": 0,
"ca": 0,
"thal": 1,
"target": 1,
"user_id": 1
}
]
You can use the following dummy data to test the prediction endpoint:
-
High Risk Case:
{ "age": 65, "sex": 1, "cp": 2, "trestbps": 160, "chol": 280, "fbs": 1, "restecg": 2, "thalach": 130, "exang": 1, "oldpeak": 3.1, "slope": 2, "ca": 2, "thal": 3 }
-
Low Risk Case:
{ "age": 35, "sex": 0, "cp": 0, "trestbps": 120, "chol": 180, "fbs": 0, "restecg": 0, "thalach": 170, "exang": 0, "oldpeak": 0.5, "slope": 0, "ca": 0, "thal": 1 }
-
Medium Risk Case:
{ "age": 50, "sex": 1, "cp": 1, "trestbps": 140, "chol": 230, "fbs": 1, "restecg": 1, "thalach": 150, "exang": 0, "oldpeak": 1.5, "slope": 1, "ca": 1, "thal": 2 }
age
: Age in yearssex
: Sex (1 = male; 0 = female)cp
: Chest pain type (0 = typical angina; 1 = atypical angina; 2 = non-anginal pain; 3 = asymptomatic)trestbps
: Resting blood pressure (in mm Hg on admission to the hospital)chol
: Serum cholesterol in mg/dlfbs
: Fasting blood sugar > 120 mg/dl (1 = true; 0 = false)restecg
: Resting electrocardiographic results (0 = normal; 1 = having ST-T wave abnormality; 2 = showing probable or definite left ventricular hypertrophy)thalach
: Maximum heart rate achievedexang
: Exercise induced angina (1 = yes; 0 = no)oldpeak
: ST depression induced by exercise relative to restslope
: The slope of the peak exercise ST segment (0 = upsloping; 1 = flat; 2 = downsloping)ca
: Number of major vessels (0-3) colored by fluoroscopythal
: 1 = normal; 2 = fixed defect; 3 = reversable defect
FastAPI provides automatic interactive API documentation. Once the server is running, you can access:
- Swagger UI:
http://localhost:8000/docs
- ReDoc:
http://localhost:8000/redoc
To run the tests:
pytest
[Include your chosen license here]
[Include guidelines for contributing to your project]
If you encounter any problems or have any questions, please open an issue in the GitHub repository.