Spaces:
Running
Running
import pandas as pd | |
from sklearn.model_selection import train_test_split | |
from sklearn.ensemble import RandomForestClassifier | |
from sklearn.metrics import accuracy_score | |
import gradio as gr | |
# Load the dataset | |
url = "https://raw.githubusercontent.com/plotly/datasets/master/diabetes.csv" | |
df = pd.read_csv(url) | |
# Preparing the data | |
X = df.drop("Outcome", axis=1) | |
y = df["Outcome"] | |
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) | |
# Training the model | |
model = RandomForestClassifier() | |
model.fit(X_train, y_train) | |
# Check accuracy in console | |
y_pred = model.predict(X_test) | |
print("Model trained. Accuracy on test data:", accuracy_score(y_test, y_pred)) | |
# Gradio prediction function | |
def predict_diabetes(Pregnancies, Glucose, BloodPressure, SkinThickness, Insulin, BMI, DiabetesPedigreeFunction, Age): | |
input_data = [[Pregnancies, Glucose, BloodPressure, SkinThickness,Insulin, BMI, DiabetesPedigreeFunction, Age]] | |
prediction = model.predict(input_data)[0] | |
return "Diabetes" if prediction == 1 else "Not Diabetic" | |
# Gradio Interface | |
with gr.Blocks() as iface: | |
gr.Markdown("# 🩺 Diabetes Risk Predictor") | |
gr.Markdown("Enter medical details to predict whether the patient is diabetic.") | |
with gr.Row(): | |
Pregnancies = gr.Number(label="Pregnancies") | |
Glucose = gr.Number(label="Glucose") | |
BloodPressure = gr.Number(label="BloodPressure") | |
SkinThickness = gr.Number(label="SkinThickness") | |
Insulin = gr.Number(label="Insulin") | |
BMI = gr.Number(label="BMI") | |
DiabetesPedigreeFunction = gr.Number(label="DiabetesPedigreeFunction") | |
Age = gr.Number(label="Age") | |
predict_btn = gr.Button("Predict") | |
output = gr.Textbox(label="Prediction") | |
def on_click_fn(*args): | |
return predict_diabetes(*args) | |
predict_btn.click( | |
on_click_fn, | |
inputs=[ | |
Pregnancies, Glucose, BloodPressure, SkinThickness, | |
Insulin, BMI, DiabetesPedigreeFunction, Age | |
], | |
outputs=output | |
) | |
iface.launch(share=True) | |