Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#1. Importing Lib
|
| 2 |
+
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import numpy as np
|
| 5 |
+
import pandas as pd
|
| 6 |
+
from sklearn.model_selection import train_test_split
|
| 7 |
+
from sklearn.linear_model import LogisticRegression
|
| 8 |
+
from sklearn.metrics import accuracy_score,r2_score
|
| 9 |
+
|
| 10 |
+
#2. Data Preprocessing
|
| 11 |
+
|
| 12 |
+
df=pd.read_csv("heart.csv")
|
| 13 |
+
|
| 14 |
+
# Spliting data into x and y (independent/dependent)
|
| 15 |
+
|
| 16 |
+
x=df.drop("target",axis=1)
|
| 17 |
+
y=df["target"]
|
| 18 |
+
|
| 19 |
+
#3. Modeling Part
|
| 20 |
+
|
| 21 |
+
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_state=0)
|
| 22 |
+
model=LogisticRegression()
|
| 23 |
+
|
| 24 |
+
model.fit(x_train,y_train)
|
| 25 |
+
model.fit(x_test,y_test)
|
| 26 |
+
|
| 27 |
+
x_predict=model.predict(x_train)
|
| 28 |
+
x_accuracy=accuracy_score(x_predict,y_train)
|
| 29 |
+
|
| 30 |
+
y_predict=model.predict(x_test)
|
| 31 |
+
y_accuracy=accuracy_score(y_predict,y_test)
|
| 32 |
+
|
| 33 |
+
#4. UI for Model using gradio
|
| 34 |
+
|
| 35 |
+
# Function to make predictions
|
| 36 |
+
def predict_heart_disease(age, sex, cp, trestbps, chol, fbs, restecg, thalach, exang, oldpeak, slope, ca, thal):
|
| 37 |
+
input_data = np.array([[age, sex, cp, trestbps, chol, fbs, restecg, thalach, exang, oldpeak, slope, ca, thal]])
|
| 38 |
+
prediction = model.predict(input_data)
|
| 39 |
+
|
| 40 |
+
if prediction[0] == 0:
|
| 41 |
+
return "Person does not have Heart Disease"
|
| 42 |
+
else:
|
| 43 |
+
return "Person has Heart Disease"
|
| 44 |
+
|
| 45 |
+
# Create the Gradio interface
|
| 46 |
+
iface = gr.Interface(
|
| 47 |
+
fn=predict_heart_disease, # Function that makes predictions
|
| 48 |
+
inputs=[
|
| 49 |
+
gr.Slider(minimum=29, maximum=77, step=1, label="Age"),
|
| 50 |
+
gr.Dropdown([0, 1], label="Sex (0 = Female, 1 = Male)"),
|
| 51 |
+
gr.Dropdown([0, 1, 2, 3], label="Chest Pain Type (cp)"),
|
| 52 |
+
gr.Slider(minimum=90, maximum=200, step=1, label="Resting Blood Pressure (trestbps)"),
|
| 53 |
+
gr.Slider(minimum=120, maximum=600, step=1, label="Serum Cholesterol (chol)"),
|
| 54 |
+
gr.Dropdown([0, 1], label="Fasting Blood Sugar (fbs)"),
|
| 55 |
+
gr.Dropdown([0, 1,2], label="Resting Electrocardiographic Results (restecg)"),
|
| 56 |
+
gr.Slider(minimum=70, maximum=202, step=1, label="Maximum Heart Rate Achieved (thalach)"),
|
| 57 |
+
gr.Dropdown([0, 1], label="Exercise Induced Angina (exang)"),
|
| 58 |
+
gr.Slider(minimum=0.0, maximum=6.2, step=0.1, label="Oldpeak (depression induced by exercise)"),
|
| 59 |
+
gr.Dropdown([0, 1, 2], label="Slope of the Peak Exercise ST Segment (slope)"),
|
| 60 |
+
gr.Dropdown([0, 1, 2,3,4], label="Number of Major Vessels Colored by Fluoroscopy (ca)"),
|
| 61 |
+
gr.Dropdown([0, 1, 2, 3], label="Thalassemia (thal)")
|
| 62 |
+
], # Input fields
|
| 63 |
+
outputs="text" # Output the prediction result as text
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
# Launch the Gradio UI
|
| 67 |
+
iface.launch()
|