Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
from sklearn.ensemble import RandomForestClassifier
|
4 |
+
from sklearn.model_selection import train_test_split
|
5 |
+
from sklearn.metrics import accuracy_score
|
6 |
+
|
7 |
+
# Load dataset (Pima Indians Diabetes dataset)
|
8 |
+
url = "https://raw.githubusercontent.com/jbrownlee/Datasets/master/pima-indians-diabetes.data.csv"
|
9 |
+
columns = ['Pregnancies', 'Glucose', 'BloodPressure', 'SkinThickness', 'Insulin', 'BMI', 'DiabetesPedigreeFunction', 'Age', 'Outcome']
|
10 |
+
data = pd.read_csv(url, names=columns)
|
11 |
+
|
12 |
+
# Split data into features and target
|
13 |
+
X = data.drop('Outcome', axis=1)
|
14 |
+
y = data['Outcome']
|
15 |
+
|
16 |
+
# Train-Test Split
|
17 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
18 |
+
|
19 |
+
# Create and train RandomForestClassifier
|
20 |
+
model = RandomForestClassifier(n_estimators=100, random_state=42)
|
21 |
+
model.fit(X_train, y_train)
|
22 |
+
|
23 |
+
# Check model accuracy
|
24 |
+
y_pred = model.predict(X_test)
|
25 |
+
accuracy = accuracy_score(y_test, y_pred)
|
26 |
+
print(f"Model Accuracy: {accuracy * 100:.2f}%")
|
27 |
+
|
28 |
+
# Function for prediction with debugging
|
29 |
+
def predict_diabetes(Pregnancies, Glucose, BloodPressure, SkinThickness, Insulin, BMI, DiabetesPedigreeFunction, Age):
|
30 |
+
input_data = [[Pregnancies, Glucose, BloodPressure, SkinThickness, Insulin, BMI, DiabetesPedigreeFunction, Age]]
|
31 |
+
prediction = model.predict(input_data)
|
32 |
+
return "Diabetic" if prediction[0] == 1 else "Not Diabetic"
|
33 |
+
|
34 |
+
# Create Gradio interface
|
35 |
+
with gr.Blocks() as demo:
|
36 |
+
gr.Markdown("# Diabetes Prediction App")
|
37 |
+
|
38 |
+
with gr.Row():
|
39 |
+
pregnancies = gr.Number(label="Pregnancies", value=0, precision=0)
|
40 |
+
glucose = gr.Number(label="Glucose", value=120)
|
41 |
+
blood_pressure = gr.Number(label="Blood Pressure", value=70)
|
42 |
+
skin_thickness = gr.Number(label="Skin Thickness", value=20)
|
43 |
+
|
44 |
+
with gr.Row():
|
45 |
+
insulin = gr.Number(label="Insulin", value=80)
|
46 |
+
bmi = gr.Number(label="BMI", value=30.0)
|
47 |
+
pedigree = gr.Number(label="Diabetes Pedigree Function", value=0.5)
|
48 |
+
age = gr.Number(label="Age", value=30, precision=0)
|
49 |
+
|
50 |
+
predict_button = gr.Button("Predict")
|
51 |
+
output = gr.Textbox(label="Prediction", placeholder="Prediction will appear here.")
|
52 |
+
|
53 |
+
# Connect button to prediction function
|
54 |
+
predict_button.click(
|
55 |
+
fn=predict_diabetes,
|
56 |
+
inputs=[pregnancies, glucose, blood_pressure, skin_thickness, insulin, bmi, pedigree, age],
|
57 |
+
outputs=output
|
58 |
+
)
|
59 |
+
|
60 |
+
# Launch the Gradio app
|
61 |
+
demo.launch()
|