File size: 2,341 Bytes
7ddebf7
 
 
 
 
 
afd3b91
7ddebf7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# import the necessary packages
import gradio as gr
import pickle
import pandas as pd

# Load the saved model using pickle
with open(r'model.pkl', 'rb') as file:
    model = pickle.load(file)

# Define a function to make predictions


def predict(gender, age, hypertension, heart_disease, ever_married, work_type, residence_type, avg_glucose_level, bmi, smoking_status):
    # Convert the input into a pandas DataFrame
    input_df = pd.DataFrame([[gender, age, hypertension, heart_disease, ever_married, work_type, residence_type, avg_glucose_level, bmi, smoking_status]],
                            columns=['gender', 'age', 'hypertension', 'heart_disease', 'ever_married', 'work_type',
                                     'Residence_type', 'avg_glucose_level', 'bmi', 'smoking_status'])
    # Predict the stroke probability
    prediction = model.predict_proba(input_df)[0][1]
    # Return the prediction
    result = "The probability of stroke is {:.2f}%".format(prediction * 100) #to give a percentage 
    return result


# Create the input and output interfaces
gender = gr.inputs.Radio(choices=["Male", "Female"], label="Gender")
age = gr.inputs.Slider(minimum=0, maximum=100)
hypertension = gr.inputs.Radio(choices=["Yes", "No"], label="Hypertension")
heart_disease = gr.inputs.Radio(choices=["Yes", "No"], label="Heart Disease")
ever_married = gr.inputs.Radio(choices=["Yes", "No"], label="Ever Married")
work_type = gr.inputs.Dropdown(
    ["Private", "Self-employed", "Govt_job", "Children", "Never_worked"], label="Work Type")
residence_type = gr.inputs.Radio(
    choices=["Urban", "Rural"], label="Residence Type")
avg_glucose_level = gr.inputs.Number(label="Average Glucose Level")
bmi = gr.inputs.Slider(minimum=0, maximum=60, label="BMI")
smoking_status = gr.inputs.Dropdown(
    ["formerly smoked", "never smoked", "smokes"], label="Smoking Status")

# Create the interface
inputs = [gender, age, hypertension, heart_disease, ever_married,
          work_type, residence_type, avg_glucose_level, bmi, smoking_status]
outputs = gr.outputs.Textbox(label="Stroke Probability")

# Launch the interface
demo = gr.Interface(fn=predict, inputs=inputs, outputs=outputs, title="Stroke Prediction",
                    description="Fill in the details and click submit to check the probability of stroke")
demo.launch(share=True)