Download app.py from nivith/DiabetesTypePredictionSystemPublic: direct link, hf CLI and curl.
- Browser
- Download file 6.75 kB
-
https://huggingface.co/spaces/nivith/DiabetesTypePredictionSystemPublic/resolve/main/app.py
- Command line
-
hf download hf://spaces/nivith/DiabetesTypePredictionSystemPublic/app.py
-
curl -L -o app.py https://huggingface.co/spaces/nivith/DiabetesTypePredictionSystemPublic/resolve/main/app.py
6.75 kB
| 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() | |