File size: 2,079 Bytes
cc9e528
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
02e46bd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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)