Fouzanjaved/ImplementationProject
0
1import pandas as pd2import numpy as np3import tensorflow as tf4from tensorflow.keras.models import load_model5from sklearn.preprocessing import MinMaxScaler6import gradio as gr7import joblib8 9# Load pre-trained model and scaler10model = load_model('diabetes_model.h5')11scaler = joblib.load('scaler.pkl')12 13def predict_diabetes(pregnancies, glucose, insulin, bmi, age):14 """Predict diabetes probability from input features"""15 # Create input array16 input_data = np.array([[pregnancies, glucose, insulin, bmi, age]])17 18 # Scale features19 scaled_data = scaler.transform(input_data)20 21 # Make prediction22 probability = model.predict(scaled_data, verbose=0)[0][0]23 24 # Interpret results25 status = "Diabetic" if probability >= 0.5 else "Not Diabetic"26 confidence = probability if probability >= 0.5 else 1 - probability27 28 # Create explanation29 explanation = f"""30 ### Prediction: {status} 31 Confidence: {confidence:.1%} 32 33 #### Key factors contributing to this prediction:34 - Glucose level: **{'High' if glucose > 140 else 'Normal'}** ({glucose} mg/dL)35 - BMI: **{'Obese' if bmi >= 30 else 'Overweight' if bmi >= 25 else 'Normal'}** ({bmi})36 - Age: {age} years37 - Insulin: {insulin} μU/mL38 - Pregnancies: {pregnancies}39 """40 41 # Create bar chart of feature importance42 features = ['Pregnancies', 'Glucose', 'Insulin', 'BMI', 'Age']43 importance = [0.15, 0.45, 0.10, 0.20, 0.10] # Example weights44 45 return {46 "probability": float(probability),47 "status": status,48 "explanation": explanation,49 "importance": (features, importance)50 }51 52# Create Gradio interface53inputs = [54 gr.Slider(0, 15, step=1, label="Number of Pregnancies"),55 gr.Slider(50, 200, value=120, label="Glucose Level (mg/dL)"),56 gr.Slider(0, 300, value=80, label="Insulin Level (μU/mL)"),57 gr.Slider(15, 50, value=32, label="BMI (kg/m²)"),58 gr.Slider(20, 100, value=33, label="Age (years)")59]60 61outputs = [62 gr.Label(label="Diabetes Probability"),63 gr.Markdown(label="Explanation"),64 gr.BarPlot(x="Feature", y="Importance", label="Feature Importance")65]66 67title = "Diabetes Prediction App"68description = "Early detection of diabetes using machine learning. Based on research: Khanam, J.J. & Foo, S.Y. (2021)"69article = """70**About this model**: 71- Trained on Pima Indians Diabetes Dataset72- Neural Network with 88.6% accuracy73- Predicts diabetes risk using 5 key health parameters74"""75 76gr.Interface(77 fn=predict_diabetes,78 inputs=inputs,79 outputs=outputs,80 title=title,81 description=description,82 article=article,83 examples=[84 [0, 90, 80, 24, 25],85 [3, 150, 95, 32, 35],86 [6, 180, 150, 38, 45]87 ]88).launch()