File size: 6,751 Bytes
765c6e4 cfdb53f 765c6e4 cfdb53f 3eb81e9 cfdb53f | 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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | import gradio as gr
import pandas as pd
from xgboost import XGBClassifier
model_path = 'xgb_model5.bin'
xgb_model = XGBClassifier()
xgb_model.load_model(model_path)
def predict_diabetes(BMI, family_history, medication, activity_level, smoking, alcohol, systolic_bp, diastolic_bp):
try:
input_data = pd.DataFrame({
'BMI': [BMI],
'Family History of Diabetes': [family_history],
'Medication': [medication],
'Physical Activity Level': [activity_level],
'Smoking': [smoking],
'Alcohol Consumption': [alcohol],
'Systolic_BP': [systolic_bp],
'Diastolic_BP': [diastolic_bp]
})
input_data['BMI'] = pd.to_numeric(input_data['BMI'], errors='coerce')
input_data['Systolic_BP'] = pd.to_numeric(input_data['Systolic_BP'], errors='coerce')
input_data['Diastolic_BP'] = pd.to_numeric(input_data['Diastolic_BP'], errors='coerce')
if input_data.isnull().values.any():
return "Error: Please enter valid numerical values for BMI, Systolic BP, and Diastolic BP."
input_data['Family History of Diabetes'] = input_data['Family History of Diabetes'].replace({'No': 0, 'Yes': 1})
input_data['Medication'] = input_data['Medication'].replace({'No': 0, 'Yes': 1})
input_data['Physical Activity Level'] = input_data['Physical Activity Level'].replace({'Low': 2, 'Moderate': 3, 'High': 4})
input_data['Smoking'] = input_data['Smoking'].replace({'No': 0, 'Yes': 1})
input_data['Alcohol Consumption'] = input_data['Alcohol Consumption'].replace({'No': 0, 'Yes': 1})
expected_order = ['BMI', 'Family History of Diabetes', 'Medication', 'Physical Activity Level', 'Smoking', 'Alcohol Consumption', 'Systolic_BP', 'Diastolic_BP']
input_data = input_data[expected_order]
prediction = xgb_model.predict(input_data)
return "Type 1 Diabetes" if prediction[0] == 0 else "Type 2 Diabetes"
except Exception as e:
return f"Error during prediction: {str(e)}"
def toggle_inputs(diagnosed):
if diagnosed == "No":
return (
gr.update(visible=False), gr.update(visible=False), gr.update(visible=False),
gr.update(visible=False), gr.update(visible=False), gr.update(visible=False),
gr.update(visible=False), gr.update(visible=False), gr.update(visible=False),
"To be used only by patients diagnosed with Diabetes Mellitus.", gr.update(visible=False),
gr.update(visible=False)
)
else:
return (
gr.update(visible=True), gr.update(visible=True), gr.update(visible=True),
gr.update(visible=True), gr.update(visible=True), gr.update(visible=True),
gr.update(visible=True), gr.update(visible=True), gr.update(visible=True),
"", gr.update(visible=True)
)
def check_all_fields(BMI, family_history, medication, activity_level, smoking, alcohol, systolic_bp, diastolic_bp):
missing_fields = []
if not BMI:
missing_fields.append("BMI")
if not family_history:
missing_fields.append("Family History of Diabetes")
if not medication:
missing_fields.append("Medication")
if not activity_level:
missing_fields.append("Physical Activity Level")
if not smoking:
missing_fields.append("Smoking")
if not alcohol:
missing_fields.append("Alcohol Consumption")
if not systolic_bp:
missing_fields.append("Systolic Blood Pressure")
if not diastolic_bp:
missing_fields.append("Diastolic Blood Pressure")
if missing_fields:
message = "Please fill in: " + ", ".join(missing_fields)
return gr.update(visible=False), message
return gr.update(visible=True), ""
with gr.Blocks() as iface:
gr.Markdown("# Diabetes Type Prediction")
gr.Markdown("Enter the following details to predict the type of diabetes.")
diagnosed = gr.Radio(choices=["Yes", "No"], label="Are you diagnosed with Diabetes Mellitus?")
BMI = gr.Textbox(label="BMI (e.g., 25)", visible=False)
family_history = gr.Radio(choices=["No", "Yes"], label="Family History of Diabetes", visible=False)
medication = gr.Radio(choices=["No", "Yes"], label="Medication", visible=False)
activity_level = gr.Radio(choices=["Low", "Moderate", "High"], label="Physical Activity Level", visible=False)
smoking = gr.Radio(choices=["No", "Yes"], label="Smoking", visible=False)
alcohol = gr.Radio(choices=["No", "Yes"], label="Alcohol Consumption", visible=False)
systolic_bp = gr.Textbox(label="Systolic Blood Pressure (e.g., 120)", visible=False)
diastolic_bp = gr.Textbox(label="Diastolic Blood Pressure (e.g., 80)", visible=False)
output = gr.Textbox(label="Prediction", visible=False)
message = gr.Textbox(visible=True, label="Notice", value="")
predict_btn = gr.Button("Predict", visible=False)
diagnosed.change(fn=toggle_inputs, inputs=diagnosed, outputs=[BMI, family_history, medication, activity_level, smoking, alcohol, systolic_bp, diastolic_bp, predict_btn, message, output])
BMI.change(fn=check_all_fields, inputs=[BMI, family_history, medication, activity_level, smoking, alcohol, systolic_bp, diastolic_bp], outputs=[predict_btn, message])
family_history.change(fn=check_all_fields, inputs=[BMI, family_history, medication, activity_level, smoking, alcohol, systolic_bp, diastolic_bp], outputs=[predict_btn, message])
medication.change(fn=check_all_fields, inputs=[BMI, family_history, medication, activity_level, smoking, alcohol, systolic_bp, diastolic_bp], outputs=[predict_btn, message])
activity_level.change(fn=check_all_fields, inputs=[BMI, family_history, medication, activity_level, smoking, alcohol, systolic_bp, diastolic_bp], outputs=[predict_btn, message])
smoking.change(fn=check_all_fields, inputs=[BMI, family_history, medication, activity_level, smoking, alcohol, systolic_bp, diastolic_bp], outputs=[predict_btn, message])
alcohol.change(fn=check_all_fields, inputs=[BMI, family_history, medication, activity_level, smoking, alcohol, systolic_bp, diastolic_bp], outputs=[predict_btn, message])
systolic_bp.change(fn=check_all_fields, inputs=[BMI, family_history, medication, activity_level, smoking, alcohol, systolic_bp, diastolic_bp], outputs=[predict_btn, message])
diastolic_bp.change(fn=check_all_fields, inputs=[BMI, family_history, medication, activity_level, smoking, alcohol, systolic_bp, diastolic_bp], outputs=[predict_btn, message])
predict_btn.click(predict_diabetes, inputs=[BMI, family_history, medication, activity_level, smoking, alcohol, systolic_bp, diastolic_bp], outputs=output)
iface.launch()
|