Lightweight end-to-end example that trains and serves a Random Forest model to predict diabetes risk from tabular patient data.
This repository includes:
- Training script (
train.py) that performs K-Fold validation and saves a model + encoder. - A pre-trained model file (
rf_model_40_trees_depth_10_min_samples_leaf_1.bin). - A Flask-based prediction endpoint (
predict.py), a Gradio UI (app.py) and a small test client (predict_test.py).
Live demo on Hugging Face Spaces: https://huggingface.co/spaces/rohith96/diabetes-prediction
- Dataset: [
diabetes_prediction_dataset.csv(patient features +diabetestarget).]
(https://www.kaggle.com/datasets/iammustafatz/diabetes-prediction-dataset/data)
-
Model:
RandomForestClassifierwith a saved One-Hot encoder (DictVectorizer-style) and classifier serialized together in a.binfile. -
API: POST
/predict(frompredict.py) accepts a single patient JSON and returnsdiabetes_probabilityanddiabetes(bool). -
UI:
app.pyprovides a Gradio interface for interactive use on local machines and HF Spaces.
- Use Docker (recommended, ensures correct Python and deps):
docker build -t diabetes-predict:latest .
docker run -p 7860:7860 diabetes-predict:latestIf you prefer the Flask/Gunicorn route (the Dockerfile included exposes port 9696):
docker build -t diabetes-predict:latest .
docker run -p 9696:9696 diabetes-predict:latest- Test the running server locally:
python predict_test.pyOr open the Gradio UI when running app.py at http://localhost:7860.
The project uses a Pipfile (Python 3.12) but you can use requirements.txt provided.
Using pipenv:
pip install pipenv
pipenv install --deploy --systemOr with a virtualenv and requirements.txt:
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txtStart Gradio UI locally:
python app.py
# open http://localhost:7860Start Flask API with Gunicorn:
gunicorn --bind=0.0.0.0:9696 predict:appThen run python predict_test.py to send a sample request.
Run full training and save a new model file:
python train.pyWhat happens:
- Reads
diabetes_prediction_dataset.csvand auto-detects categorical vs numerical columns. - Runs K-Fold cross-validation and prints per-fold accuracy.
- Retrains on the full training set and writes the encoder + model to a
.binfile.
diabetes_prediction_dataset.csv— dataset.train.py— training + CV script.predict.py— Flask app (loads.binfile and serves/predict).app.py— Gradio app for interactive UI.predict_test.py— example client that POSTs a sample patient.rf_model_40_trees_depth_10_min_samples_leaf_1.bin— included saved model.Dockerfile,Pipfile,requirements.txt— runtime and packaging.
- Ensure JSON keys and categorical values you send to
/predictare identical (names & categories) to those used during training — otherwise the encoder may produce different feature vectors or raise an error. - If you change preprocessing or features, retrain and save a new
.binthat contains the encoder and model together. - If the model file is large or you want to avoid committing binaries, upload the model to the Hugging Face Hub and download it at runtime. Use
huggingface_hub.hf_hub_downloadand provide an HF token through Space secrets. - Add missing dependencies to
requirements.txtif runtime build on HF shows failures.
- File not found: ensure
rf_model_40_trees_depth_10_min_samples_leaf_1.binis in the repo root or adjust the path inapp.py/predict.py. - Pickle errors: training and inference should use compatible Python and
scikit-learnversions; if you hit incompatibilities, retrain with matching versions. - Missing package errors during deployment: add the package name to
requirements.txtand re-push.
The repository does not include a full EDA notebook, but train.py performs simple feature inspection and auto-detection of categorical vs numerical features. Recommended EDA steps you can run locally:
- Inspect ranges, missing values and distributions:
import pandas as pd
df = pd.read_csv('diabetes_prediction_dataset.csv')
df.describe(include='all')
df.isna().sum()- Visualize target balance and numeric feature distributions (histograms, boxplots) and check correlations.
- For categorical features, list unique values and frequencies so inputs at inference match training categories.
Performing these steps addresses the EDA criterion by documenting feature ranges, missing values, and target distribution.
Training is performed in train.py and includes:
- Model:
RandomForestClassifier(scikit-learn). - Cross-validation:
KFoldwithn_splits = 6for per-fold accuracy reporting. - Final training: retrains on the full training set and saves the encoder + model to
rf_model_40_trees_depth_10_min_samples_leaf_1.bin. - Key hyperparameters used in this repo:
n_estimators = 40max_depth = 10min_samples_leaf = 1
If you want to extend experiments (for higher model-training score): try multiple models (logistic regression, gradient boosting), grid search or randomized search for hyperparameter tuning, and record metrics (accuracy, precision, recall, ROC-AUC).
The training logic is exported as a standalone script: train.py. Running python train.py will perform cross-validation and save the trained model + encoder to the .bin file.
To reproduce training and evaluation locally:
- Ensure dataset
diabetes_prediction_dataset.csvis placed in the repository root. - Create environment and install dependencies:
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt- Run training:
python train.pyNotes:
- Python version used in the project: 3.12 (see
Pipfile). - If you prefer pipenv:
pipenv install --deploy --system. - If the dataset is not committed, include clear download instructions here or upload the CSV to the repo/hub.
This repo includes a Dockerfile to create a reproducible runtime. Build and run commands:
docker build -t diabetes-predict:latest .
# Run Gradio default app if you modified Dockerfile to start it; otherwise the provided Dockerfile runs gunicorn on port 9696:
docker run -p 7860:7860 diabetes-predict:latest
# or if using predict: docker run -p 9696:9696 diabetes-predict:latestMake sure the Dockerfile copies the model file into the image (it currently does) and starts the correct server.
This application is deployed to Hugging Face Spaces (Gradio). Space URL:
https://huggingface.co/spaces/rohith96/diabetes-prediction
To deploy manually from your local repo:
git init
git add .
git commit -m "HF Space: add Gradio app"
git branch -M main
git remote add origin https://huggingface.co/spaces/<HF_USERNAME>/<SPACE_NAME>.git
git push -u origin mainIf you prefer not to commit the binary model, upload the model to the Hugging Face Hub and download it at runtime using huggingface_hub.hf_hub_download and an HF token stored in the Space secrets.
- A
Pipfileis present (Python 3.12). For Spaces we includerequirements.txtfor deterministic install. - To run locally, use the
requirements.txtorpipenvinstructions shown above.