ML / app.py
jimmy624135's picture
Update app.py
dec90b1 verified
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import confusion_matrix, classification_report
import gradio as gr
def process_and_evaluate(file):
# Load the dataset
df = pd.read_csv(file)
# Encode categorical features
categorical_columns = df.select_dtypes(include=['object']).columns
label_encoders = {}
for col in categorical_columns:
le = LabelEncoder()
df[col] = le.fit_transform(df[col])
label_encoders[col] = le
# Define the target and features
target = 'target' # Assuming the target column is named 'target'
X = df.drop(columns=[target])
y = df[target]
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Train a RandomForestClassifier
clf = RandomForestClassifier(random_state=42)
clf.fit(X_train, y_train)
# Predict on the test set
y_pred = clf.predict(X_test)
# Compute the confusion matrix
conf_matrix = confusion_matrix(y_test, y_pred)
# Print the classification report
classification_rep = classification_report(y_test, y_pred)
return classification_rep
# Create the Gradio interface
inputs = gr.File(label="Upload CSV File")
outputs = gr.Textbox(label="Classification Report")
gr.Interface(fn=process_and_evaluate, inputs=inputs, outputs=outputs, title="Heart Disease Prediction",
description="Upload a CSV file containing heart disease data to get the classification report.").launch()