Spaces:
Runtime error
Runtime error
Commit ·
e84179b
1
Parent(s): db11f8f
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import joblib
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
# Load your dataset here
|
| 7 |
+
file_path_gr = '/content/drive/MyDrive/Colab Notebooks/Homework_04/heart.dat'
|
| 8 |
+
df = pd.read_csv(file_path_gr, header=None, sep='\s+')
|
| 9 |
+
|
| 10 |
+
# Define the X variable with the feature columns
|
| 11 |
+
X = df.drop(df.columns[-1], axis=1)
|
| 12 |
+
|
| 13 |
+
# Load the saved models
|
| 14 |
+
best_models = [
|
| 15 |
+
("Logistic Regression", joblib.load('/content/drive/MyDrive/Colab Notebooks/Homework_04/Logistic Regression_best_model.pkl')),
|
| 16 |
+
("Support Vector Machine", joblib.load('/content/drive/MyDrive/Colab Notebooks/Homework_04/Support Vector Machine_best_model.pkl')),
|
| 17 |
+
("Decision Trees", joblib.load('/content/drive/MyDrive/Colab Notebooks/Homework_04/Decision Trees_best_model.pkl')),
|
| 18 |
+
("Random Forests", joblib.load('/content/drive/MyDrive/Colab Notebooks/Homework_04/Random Forests_best_model.pkl')),
|
| 19 |
+
("MLPClassifier", joblib.load('/content/drive/MyDrive/Colab Notebooks/Homework_04/MLPClassifier_best_model.pkl')),
|
| 20 |
+
("K-Nearest Neighbors", joblib.load('/content/drive/MyDrive/Colab Notebooks/Homework_04/K-Nearest Neighbors_best_model.pkl')),
|
| 21 |
+
("Linear Discriminant Analysis",joblib.load('/content/drive/MyDrive/Colab Notebooks/Homework_04/Linear Discriminant Analysis_best_model.pkl'))
|
| 22 |
+
]
|
| 23 |
+
|
| 24 |
+
# Load the saved scaler
|
| 25 |
+
scaler = joblib.load('/content/drive/MyDrive/Colab Notebooks/Homework_04/scaler.pkl')
|
| 26 |
+
|
| 27 |
+
attribute_names = [
|
| 28 |
+
"age", "sex", "chest pain type", "resting blood pressure",
|
| 29 |
+
"serum cholesterol", "fasting blood sugar > 120 mg/dl",
|
| 30 |
+
"resting ECG results", "maximum heart rate achieved",
|
| 31 |
+
"exercise induced angina", "oldpeak", "slope of peak exercise ST segment",
|
| 32 |
+
"number of major vessels", "thal"
|
| 33 |
+
]
|
| 34 |
+
|
| 35 |
+
def predict(*args):
|
| 36 |
+
user_features = list(args[:-1])
|
| 37 |
+
model_name = args[-1]
|
| 38 |
+
model = dict(best_models)[model_name]
|
| 39 |
+
input_data = np.array(user_features).reshape(1, -1)
|
| 40 |
+
input_data_scaled = scaler.transform(input_data)
|
| 41 |
+
probs = model.predict_proba(input_data_scaled)
|
| 42 |
+
return {f"Absence of heart disease" if i == 0 else "Presence of heart disease": p for i, p in enumerate(probs[0])}
|
| 43 |
+
|
| 44 |
+
# List of model names for the dropdown menu
|
| 45 |
+
model_names = [name for name, _ in best_models]
|
| 46 |
+
|
| 47 |
+
# Define inputs
|
| 48 |
+
user_features = [gr.inputs.Number(label=col_name) for col_name in attribute_names]
|
| 49 |
+
model_name = gr.inputs.Dropdown(choices=model_names, label="Model")
|
| 50 |
+
|
| 51 |
+
# Define output
|
| 52 |
+
output = gr.outputs.Label(num_top_classes=len(np.unique(df[df.columns[-1]])), label="Predicted Class Probabilities")
|
| 53 |
+
|
| 54 |
+
# Define examples
|
| 55 |
+
examples = [
|
| 56 |
+
[70.0, 1.0, 4.0, 130.0, 322.0, 0.0, 2.0, 109.0, 0.0, 2.4, 2.0, 3.0, 3.0, "Logistic Regression"],
|
| 57 |
+
[67.0, 0.0, 3.0, 115.0, 564.0, 0.0, 2.0, 160.0, 0.0, 1.6, 2.0, 0.0, 7.0, "Logistic Regression"]
|
| 58 |
+
]
|
| 59 |
+
|
| 60 |
+
# Create the Gradio interface
|
| 61 |
+
iface = gr.Interface(
|
| 62 |
+
fn=predict,
|
| 63 |
+
inputs=user_features + [model_name],
|
| 64 |
+
outputs=output,
|
| 65 |
+
title="Machine Learning Model",
|
| 66 |
+
description="Select a model and enter user features to predict class probabilities.",
|
| 67 |
+
examples=examples
|
| 68 |
+
).launch(debug=True)
|
| 69 |
+
|
| 70 |
+
iface.launch()
|