DEV Community

Mate Technologies
Mate Technologies

Posted on

Build a Diabetes Predictor with Python & AI – Step by Step

In this tutorial, we’ll create a Diabetes Predictor v2.0 using Python, Tkinter, and a machine learning model. This app can predict diabetes risk from single patient entries or batch CSV files with a polished GUI.

We’ll break it down into bite-sized steps so beginners can follow along.

🔗 GitHub Repo: Diabetes Predictor

Step 1: Install Required Libraries

Before we start, install these Python libraries:

pip install pandas scikit-learn tkinter ttkbootstrap
Enter fullscreen mode Exit fullscreen mode

Optional (for drag & drop support):

pip install tkinterdnd2
Enter fullscreen mode Exit fullscreen mode

Explanation:

pandas → handles CSV files and data manipulation

scikit-learn → provides machine learning tools

tkinter → builds the GUI

ttkbootstrap → adds modern themes to Tkinter

tkinterdnd2 → enables drag & drop for files

Step 2: Import Libraries

Start your Python script by importing necessary modules:

import os, sys, threading
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
import tkinter as tk
from tkinter import filedialog, messagebox, ttk
import ttkbootstrap as tb
from ttkbootstrap.constants import *
Enter fullscreen mode Exit fullscreen mode

Explanation:
These imports allow us to handle data, run machine learning models, and create a graphical interface for the app.

Optional drag & drop support:

try:
    from tkinterdnd2 import TkinterDnD, DND_FILES
    DND_ENABLED = True
except ImportError:
    DND_ENABLED = False
    print("Drag & Drop requires tkinterdnd2: pip install tkinterdnd2")
Enter fullscreen mode Exit fullscreen mode

Step 3: Prepare the Dataset

We’ll use the Pima Indians Diabetes Dataset (diabetes.csv). Make sure it’s in your project folder.

df = pd.read_csv("diabetes.csv")
X = df.drop("Outcome", axis=1)  # Features
y = df["Outcome"]               # Target
Enter fullscreen mode Exit fullscreen mode

Explanation:

X contains patient features like glucose, BMI, age, etc.

y contains the prediction label (0 = Non-Diabetic, 1 = Diabetic).

Step 4: Train the Machine Learning Model

We’ll use a Random Forest Classifier to predict diabetes:

from sklearn.ensemble import RandomForestClassifier

model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X, y)
Enter fullscreen mode Exit fullscreen mode

Explanation:

Random Forest is a powerful model that combines multiple decision trees.

n_estimators=100 means 100 trees are used to make predictions.

Step 5: Make a Prediction

For a single patient:

patient_data = [2, 120, 70, 20, 79, 25.6, 0.5, 33]  # Example values
prediction = model.predict([patient_data])[0]

if prediction == 1:
    print("Predicted Outcome: Diabetic")
else:
    print("Predicted Outcome: Non-Diabetic")
Enter fullscreen mode Exit fullscreen mode

Explanation:

Input values must match the dataset features in order: Pregnancies, Glucose, BloodPressure, SkinThickness, Insulin, BMI, DiabetesPedigreeFunction, Age.

The model outputs 0 or 1.

Step 6: Build a Basic GUI

We’ll use Tkinter + ttkbootstrap to make it interactive.

root = tb.Window(themename="darkly")
root.title("Diabetes Predictor v2.0")
root.geometry("600x400")

tb.Label(root, text="AI-Powered Diabetes Predictor", font=("Segoe UI", 18, "bold")).pack(pady=20)
Enter fullscreen mode Exit fullscreen mode

Explanation:

tb.Window creates a themed Tkinter window.

Label displays the app title.

Step 7: Add Single Entry Prediction UI

entry_frame = tb.Frame(root)
entry_frame.pack(pady=10)

features = ["Pregnancies","Glucose","BloodPressure","SkinThickness","Insulin","BMI","DiabetesPedigreeFunction","Age"]
entries = {}

for feature in features:
    row = tb.Frame(entry_frame)
    row.pack(fill=tk.X, pady=2)
    tb.Label(row, text=f"{feature}:").pack(side=tk.LEFT)
    entry = tb.Entry(row, width=10)
    entry.pack(side=tk.LEFT)
    entries[feature] = entry
Enter fullscreen mode Exit fullscreen mode

Explanation:

We create an input field for each feature in the dataset.

Users can type patient data directly into the app.

Step 8: Add Predict Button & Logic

def predict_single():
    try:
        values = [float(entries[f].get()) for f in features]
        pred = model.predict([values])[0]
        result_label.config(text="Diabetic" if pred==1 else "Non-Diabetic")
    except ValueError:
        messagebox.showerror("Error", "Enter valid numeric values")

tb.Button(root, text="Predict", command=predict_single).pack(pady=10)
result_label = tb.Label(root, text="", font=("Segoe UI", 12, "bold"))
result_label.pack(pady=10)
Enter fullscreen mode Exit fullscreen mode

Explanation:

Reads values from the entry fields.

Predicts diabetes risk and displays it.

Step 9: Add CSV Batch Prediction (Optional)

def browse_file():
    file_path = filedialog.askopenfilename(filetypes=[("CSV Files","*.csv")])
    if file_path:
        df = pd.read_csv(file_path)
        predictions = model.predict(df)
        print(predictions)

tb.Button(root, text="Predict from CSV", command=browse_file).pack(pady=10)
Enter fullscreen mode Exit fullscreen mode

Explanation:

Allows batch prediction by reading a CSV file.

Outputs an array of predictions for all entries in the CSV.

Step 10: Run the App

root.mainloop()
Enter fullscreen mode Exit fullscreen mode

Explanation:

Starts the Tkinter main loop and opens the app window.

🎉 Congratulations!
You now have a working Diabetes Predictor v2.0 with AI and a GUI. You can:

Predict single patients

Predict from CSV files

See results instantly

Check the full code here: GitHub Repo

Top comments (0)