Fouzanjaved commited on
Commit
9d7fab8
·
verified ·
1 Parent(s): f9fbabc

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -0
app.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import pandas as pd
3
+ import tensorflow as tf
4
+ from tensorflow.keras.models import Sequential
5
+ from tensorflow.keras.layers import Dense
6
+ from sklearn.model_selection import train_test_split
7
+ from sklearn.preprocessing import MinMaxScaler
8
+ import gradio as gr
9
+
10
+ # Load dataset
11
+ url = "https://raw.githubusercontent.com/jbrownlee/Datasets/master/pima-indians-diabetes.data.csv"
12
+ column_names = ["Pregnancies", "Glucose", "BloodPressure", "SkinThickness", "Insulin",
13
+ "BMI", "DiabetesPedigreeFunction", "Age", "Outcome"]
14
+ df = pd.read_csv(url, header=None, names=column_names)
15
+
16
+ # Replace zero values with mean
17
+ cols_with_zero = ["Glucose", "BloodPressure", "SkinThickness", "Insulin", "BMI"]
18
+ df[cols_with_zero] = df[cols_with_zero].replace(0, np.nan)
19
+ df.fillna(df.mean(), inplace=True)
20
+
21
+ # Feature selection
22
+ selected_features = ["Pregnancies", "Glucose", "Insulin", "BMI", "Age"]
23
+ X = df[selected_features]
24
+ y = df["Outcome"]
25
+
26
+ # Normalize
27
+ scaler = MinMaxScaler()
28
+ X_scaled = scaler.fit_transform(X)
29
+
30
+ # Train-test split
31
+ X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.2, random_state=42)
32
+
33
+ # Build ANN model
34
+ model = Sequential([
35
+ Dense(12, activation='relu', input_shape=(X_train.shape[1],)),
36
+ Dense(8, activation='relu'),
37
+ Dense(1, activation='sigmoid')
38
+ ])
39
+
40
+ # Compile and train
41
+ model.compile(optimizer=tf.keras.optimizers.SGD(learning_rate=0.01),
42
+ loss='binary_crossentropy',
43
+ metrics=['accuracy'])
44
+ model.fit(X_train, y_train, epochs=400, batch_size=16, verbose=0)
45
+
46
+ # Prediction function
47
+ def predict_diabetes(pregnancies, glucose, insulin, bmi, age):
48
+ input_data = np.array([[pregnancies, glucose, insulin, bmi, age]])
49
+ input_scaled = scaler.transform(input_data)
50
+ prediction = model.predict(input_scaled)[0][0]
51
+ return "Diabetic" if prediction >= 0.5 else "Not Diabetic"
52
+
53
+ # Gradio Interface
54
+ iface = gr.Interface(
55
+ fn=predict_diabetes,
56
+ inputs=[
57
+ gr.Number(label="Pregnancies"),
58
+ gr.Number(label="Glucose"),
59
+ gr.Number(label="Insulin"),
60
+ gr.Number(label="BMI"),
61
+ gr.Number(label="Age")
62
+ ],
63
+ outputs="text",
64
+ title="Diabetes Prediction using ANN",
65
+ description="Enter medical values to predict whether a person has diabetes"
66
+ )
67
+
68
+ iface.launch()