WamaW commited on
Commit
2f0634a
1 Parent(s): cdea636

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -0
app.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Necessary imports
2
+ pip install pandas numpy scipy statsmodels patsy dtale scikit-learn pandas_profiling pycaret
3
+ import gradio as gr
4
+ import pandas as pd
5
+ from pycaret.classification import load_model, predict_model
6
+
7
+ # Load the tuned model
8
+ tuned_gbc_classifier = load_model('tuned_gbc_classifier')
9
+
10
+ def predict_ten_year_chd(male, age, education, currentSmoker, cigsPerDay, BPMeds, prevalentStroke,
11
+ prevalentHyp, diabetes, totChol, sysBP, diaBP, BMI, heartRate, glucose):
12
+ try:
13
+ # Convert categorical variables to numerical representation
14
+ male = 1 if male == "Male" else 0
15
+ education_mapping = {
16
+ "Some High School": 0,
17
+ "High School Graduate": 1,
18
+ "Some College": 2,
19
+ "College Graduate": 3
20
+ }
21
+ education = education_mapping.get(education, 0)
22
+
23
+ # Create a DataFrame with the input values
24
+ data = pd.DataFrame(
25
+ data=[[male, age, education, currentSmoker, cigsPerDay, BPMeds, prevalentStroke,
26
+ prevalentHyp, diabetes, totChol, sysBP, diaBP, BMI, heartRate, glucose]],
27
+ columns=['male', 'age', 'education', 'currentSmoker', 'cigsPerDay', 'BPMeds', 'prevalentStroke',
28
+ 'prevalentHyp', 'diabetes', 'totChol', 'sysBP', 'diaBP', 'BMI', 'heartRate', 'glucose']
29
+ )
30
+
31
+ # Make a prediction
32
+ pred = predict_model(tuned_gbc_classifier, data=data)
33
+
34
+ # Extract the prediction and the confidence using the correct keys
35
+ prediction = pred['prediction_label'].iloc[0]
36
+ confidence = pred['prediction_score'].iloc[0]
37
+
38
+ # Return the prediction with 'At Risk' category for No CHD with confidence < 0.8
39
+ if prediction == 0 and confidence < 0.8:
40
+ return f"Prediction: No CHD (At Risk), Confidence: {confidence:.2f}"
41
+ else:
42
+ return f"Prediction: {'Has CHD' if prediction == 1 else 'No CHD'}, Confidence: {confidence:.2f}"
43
+
44
+ except Exception as e:
45
+ return f"An error occurred: {str(e)}"
46
+
47
+ # Create the Gradio interface
48
+ iface = gr.Interface(
49
+ fn=predict_ten_year_chd,
50
+ inputs=[
51
+ gr.inputs.Radio(["Male", "Female"], label="Gender"),
52
+ gr.inputs.Slider(minimum=18, maximum=100, label="Age"),
53
+ gr.inputs.Dropdown(["Some High School", "High School Graduate", "Some College", "College Graduate"], label="Education"),
54
+ gr.inputs.Checkbox(label="Current Smoker"),
55
+ gr.inputs.Slider(minimum=0, maximum=50, default=0, label="Cigarettes Per Day"),
56
+ gr.inputs.Checkbox(label="On Blood Pressure Medication"),
57
+ gr.inputs.Checkbox(label="History of Prevalent Stroke"),
58
+ gr.inputs.Checkbox(label="History of Prevalent Hypertension"),
59
+ gr.inputs.Checkbox(label="Diabetes"),
60
+ gr.inputs.Slider(minimum=100, maximum=400, default=200, label="Total Cholesterol"),
61
+ gr.inputs.Slider(minimum=90, maximum=200, default=120, label="Systolic BP"),
62
+ gr.inputs.Slider(minimum=60, maximum=120, default=80, label="Diastolic BP"),
63
+ gr.inputs.Slider(minimum=15, maximum=50, default=25, label="BMI"),
64
+ gr.inputs.Slider(minimum=40, maximum=120, default=75, label="Heart Rate"),
65
+ gr.inputs.Slider(minimum=40, maximum=300, default=100, label="Glucose Level")
66
+ ],
67
+ outputs=gr.outputs.Textbox(),
68
+ live=False, # set live to False to add a submit button
69
+ title="CHD Prediction",
70
+ description="By Abderrahim Benmoussa, Ph.D."
71
+ )
72
+
73
+ # Run the app
74
+ iface.launch(share=True)