Spaces:
Sleeping
Sleeping
| import numpy as np | |
| import pandas as pd | |
| import tensorflow as tf | |
| from tensorflow.keras.models import Sequential | |
| from tensorflow.keras.layers import Dense | |
| from sklearn.model_selection import train_test_split | |
| from sklearn.preprocessing import MinMaxScaler | |
| import gradio as gr | |
| # Load dataset | |
| url = "https://raw.githubusercontent.com/jbrownlee/Datasets/master/pima-indians-diabetes.data.csv" | |
| column_names = ["Pregnancies", "Glucose", "BloodPressure", "SkinThickness", "Insulin", | |
| "BMI", "DiabetesPedigreeFunction", "Age", "Outcome"] | |
| df = pd.read_csv(url, header=None, names=column_names) | |
| # Replace zero values with mean | |
| cols_with_zero = ["Glucose", "BloodPressure", "SkinThickness", "Insulin", "BMI"] | |
| df[cols_with_zero] = df[cols_with_zero].replace(0, np.nan) | |
| df.fillna(df.mean(), inplace=True) | |
| # Feature selection | |
| selected_features = ["Pregnancies", "Glucose", "Insulin", "BMI", "Age"] | |
| X = df[selected_features] | |
| y = df["Outcome"] | |
| # Normalize | |
| scaler = MinMaxScaler() | |
| X_scaled = scaler.fit_transform(X) | |
| # Train-test split | |
| X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.2, random_state=42) | |
| # Build ANN model | |
| model = Sequential([ | |
| Dense(12, activation='relu', input_shape=(X_train.shape[1],)), | |
| Dense(8, activation='relu'), | |
| Dense(1, activation='sigmoid') | |
| ]) | |
| # Compile and train | |
| model.compile(optimizer=tf.keras.optimizers.SGD(learning_rate=0.01), | |
| loss='binary_crossentropy', | |
| metrics=['accuracy']) | |
| model.fit(X_train, y_train, epochs=400, batch_size=16, verbose=0) | |
| # Prediction function | |
| def predict_diabetes(pregnancies, glucose, insulin, bmi, age): | |
| input_data = np.array([[pregnancies, glucose, insulin, bmi, age]]) | |
| input_scaled = scaler.transform(input_data) | |
| prediction = model.predict(input_scaled)[0][0] | |
| return "Diabetic" if prediction >= 0.5 else "Not Diabetic" | |
| # Gradio Interface | |
| iface = gr.Interface( | |
| fn=predict_diabetes, | |
| inputs=[ | |
| gr.Number(label="Pregnancies"), | |
| gr.Number(label="Glucose"), | |
| gr.Number(label="Insulin"), | |
| gr.Number(label="BMI"), | |
| gr.Number(label="Age") | |
| ], | |
| outputs="text", | |
| title="Diabetes Prediction using ANN", | |
| description="Enter medical values to predict whether a person has diabetes" | |
| ) | |
| iface.launch() | |