Spaces:
Sleeping
Sleeping
| #1. Importing Lib | |
| import gradio as gr | |
| import numpy as np | |
| import pandas as pd | |
| from sklearn.model_selection import train_test_split | |
| from sklearn.linear_model import LogisticRegression | |
| from sklearn.metrics import accuracy_score,r2_score | |
| #2. Data Preprocessing | |
| df=pd.read_csv("heart.csv") | |
| # Spliting data into x and y (independent/dependent) | |
| x=df.drop("target",axis=1) | |
| y=df["target"] | |
| #3. Modeling Part | |
| x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_state=0) | |
| model=LogisticRegression() | |
| model.fit(x_train,y_train) | |
| model.fit(x_test,y_test) | |
| x_predict=model.predict(x_train) | |
| x_accuracy=accuracy_score(x_predict,y_train) | |
| y_predict=model.predict(x_test) | |
| y_accuracy=accuracy_score(y_predict,y_test) | |
| #4. UI for Model using gradio | |
| # Function to make predictions | |
| def predict_heart_disease(age, sex, cp, trestbps, chol, fbs, restecg, thalach, exang, oldpeak, slope, ca, thal): | |
| input_data = np.array([[age, sex, cp, trestbps, chol, fbs, restecg, thalach, exang, oldpeak, slope, ca, thal]]) | |
| prediction = model.predict(input_data) | |
| if prediction[0] == 0: | |
| return "Person does not have Heart Disease" | |
| else: | |
| return "Person has Heart Disease" | |
| # Create the Gradio interface | |
| iface = gr.Interface( | |
| fn=predict_heart_disease, # Function that makes predictions | |
| inputs=[ | |
| gr.Slider(minimum=29, maximum=77, step=1, label="Age"), | |
| gr.Dropdown([0, 1], label="Sex (0 = Female, 1 = Male)"), | |
| gr.Dropdown([0, 1, 2, 3], label="Chest Pain Type (cp)"), | |
| gr.Slider(minimum=90, maximum=200, step=1, label="Resting Blood Pressure (trestbps)"), | |
| gr.Slider(minimum=120, maximum=600, step=1, label="Serum Cholesterol (chol)"), | |
| gr.Dropdown([0, 1], label="Fasting Blood Sugar (fbs)"), | |
| gr.Dropdown([0, 1,2], label="Resting Electrocardiographic Results (restecg)"), | |
| gr.Slider(minimum=70, maximum=202, step=1, label="Maximum Heart Rate Achieved (thalach)"), | |
| gr.Dropdown([0, 1], label="Exercise Induced Angina (exang)"), | |
| gr.Slider(minimum=0.0, maximum=6.2, step=0.1, label="Oldpeak (depression induced by exercise)"), | |
| gr.Dropdown([0, 1, 2], label="Slope of the Peak Exercise ST Segment (slope)"), | |
| gr.Dropdown([0, 1, 2,3,4], label="Number of Major Vessels Colored by Fluoroscopy (ca)"), | |
| gr.Dropdown([0, 1, 2, 3], label="Thalassemia (thal)") | |
| ], # Input fields | |
| outputs="text" # Output the prediction result as text | |
| ) | |
| # Launch the Gradio UI | |
| iface.launch() | |