Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pandas as pd
|
3 |
+
from sklearn.model_selection import train_test_split
|
4 |
+
from sklearn.linear_model import LogisticRegression
|
5 |
+
from sklearn.preprocessing import StandardScaler
|
6 |
+
from sklearn.metrics import accuracy_score
|
7 |
+
import gradio as gr
|
8 |
+
|
9 |
+
# Example dataset (replace with actual dataset)
|
10 |
+
# Load dataset
|
11 |
+
df = pd.read_csv('heart.csv')
|
12 |
+
|
13 |
+
# For demonstration, let's create a synthetic dataset
|
14 |
+
data = {
|
15 |
+
'age': np.random.randint(29, 77, 303),
|
16 |
+
'sex': np.random.randint(0, 2, 303),
|
17 |
+
'cp': np.random.randint(0, 4, 303),
|
18 |
+
'trestbps': np.random.randint(94, 200, 303),
|
19 |
+
'chol': np.random.randint(126, 564, 303),
|
20 |
+
'fbs': np.random.randint(0, 2, 303),
|
21 |
+
'restecg': np.random.randint(0, 2, 303),
|
22 |
+
'thalach': np.random.randint(71, 202, 303),
|
23 |
+
'exang': np.random.randint(0, 2, 303),
|
24 |
+
'oldpeak': np.random.uniform(0, 6.2, 303),
|
25 |
+
'slope': np.random.randint(0, 3, 303),
|
26 |
+
'ca': np.random.randint(0, 4, 303),
|
27 |
+
'thal': np.random.randint(1, 4, 303),
|
28 |
+
'target': np.random.randint(0, 2, 303)
|
29 |
+
}
|
30 |
+
|
31 |
+
df = pd.DataFrame(data)
|
32 |
+
|
33 |
+
# Define features and target
|
34 |
+
X = df.drop('target', axis=1)
|
35 |
+
y = df['target']
|
36 |
+
|
37 |
+
# Split data into training and testing sets
|
38 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
39 |
+
|
40 |
+
# Standardize the data
|
41 |
+
scaler = StandardScaler()
|
42 |
+
X_train = scaler.fit_transform(X_train)
|
43 |
+
X_test = scaler.transform(X_test)
|
44 |
+
|
45 |
+
# Create and train the logistic regression model
|
46 |
+
model = LogisticRegression()
|
47 |
+
model.fit(X_train, y_train)
|
48 |
+
|
49 |
+
# Evaluate the model
|
50 |
+
y_pred = model.predict(X_test)
|
51 |
+
accuracy = accuracy_score(y_test, y_pred)
|
52 |
+
print(f'Accuracy: {accuracy:.2f}')
|
53 |
+
|
54 |
+
# Function to predict heart disease
|
55 |
+
def predict_heart_disease(age, sex, cp, trestbps, chol, fbs, restecg, thalach, exang, oldpeak, slope, ca, thal):
|
56 |
+
# Create input array
|
57 |
+
input_data = np.array([age, sex, cp, trestbps, chol, fbs, restecg, thalach, exang, oldpeak, slope, ca, thal]).reshape(1, -1)
|
58 |
+
input_data = scaler.transform(input_data)
|
59 |
+
|
60 |
+
# Make prediction
|
61 |
+
prediction = model.predict(input_data)
|
62 |
+
if prediction[0] == 1:
|
63 |
+
return "The person has heart disease."
|
64 |
+
else:
|
65 |
+
return "The person does not have heart disease."
|
66 |
+
|
67 |
+
# Gradio integration
|
68 |
+
def gradio_predict_heart_disease(age, sex, cp, trestbps, chol, fbs, restecg, thalach, exang, oldpeak, slope, ca, thal):
|
69 |
+
return predict_heart_disease(age, sex, cp, trestbps, chol, fbs, restecg, thalach, exang, oldpeak, slope, ca, thal)
|
70 |
+
|
71 |
+
iface = gr.Interface(
|
72 |
+
fn=gradio_predict_heart_disease,
|
73 |
+
inputs=[
|
74 |
+
gr.components.Number(label="Age"),
|
75 |
+
gr.components.Radio(label="Sex", choices=[0, 1]),
|
76 |
+
gr.components.Dropdown(label="Chest Pain Type (cp)", choices=[0, 1, 2, 3]),
|
77 |
+
gr.components.Number(label="Resting Blood Pressure (trestbps)"),
|
78 |
+
gr.components.Number(label="Serum Cholestoral in mg/dl (chol)"),
|
79 |
+
gr.components.Radio(label="Fasting Blood Sugar > 120 mg/dl (fbs)", choices=[0, 1]),
|
80 |
+
gr.components.Radio(label="Resting Electrocardiographic Results (restecg)", choices=[0, 1]),
|
81 |
+
gr.components.Number(label="Maximum Heart Rate Achieved (thalach)"),
|
82 |
+
gr.components.Radio(label="Exercise Induced Angina (exang)", choices=[0, 1]),
|
83 |
+
gr.components.Number(label="ST depression induced by exercise relative to rest (oldpeak)"),
|
84 |
+
gr.components.Dropdown(label="Slope of the peak exercise ST segment (slope)", choices=[0, 1, 2]),
|
85 |
+
gr.components.Dropdown(label="Number of major vessels (0-3) colored by fluoroscopy (ca)", choices=[0, 1, 2, 3]),
|
86 |
+
gr.components.Dropdown(label="Thalassemia (thal)", choices=[1, 2, 3])
|
87 |
+
],
|
88 |
+
outputs="text"
|
89 |
+
)
|
90 |
+
|
91 |
+
iface.launch()
|