isshagle's picture
Update app.py
066c261
raw
history blame
No virus
2.76 kB
import gradio as gr
import pandas as pd
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import accuracy_score
# Load the breast cancer dataset
data = load_breast_cancer()
X = pd.DataFrame(data.data, columns=data.feature_names)
y = pd.Series(data.target)
# Split the dataset 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)
# Standardize the features
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# Create and train a logistic regression model
model = LogisticRegression()
model.fit(X_train, y_train)
# Define a function for prediction
def predict_cancer(*input_data):
# Preprocess the input data
input_data = [float(val) for val in input_data]
input_data = scaler.transform([input_data]) # Standardize input data
prediction = model.predict(input_data)
if prediction[0] == 0:
result = "Malignant"
else:
result = "Benign"
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
#return (result, accuracy_score)
return f"Prediction: {result}, Accuracy: {accuracy:.2f}"
# Create a Gradio interface
input_component = gr.Interface(
fn=predict_cancer,
inputs=[gr.inputs.Number(label=feature) for feature in data.feature_names],
outputs="text",
examples = [
[13.21, 25.25, 84.1, 537.9, 0.08791, 0.05205, 0.02772, 0.02068, 0.1619, 0.05584, 0.2084, 1.35, 1.314, 17.58, 0.003047, 0.00781, 0.00511, 0.003749, 0.006471, 0.001095, 14.87, 38.97, 99.59, 698.8, 0.1162, 0.1711, 0.2282, 0.1282, 0.2871, 0.06917],
[9.029, 17.33, 58.79, 250.5, 0.1066, 0.1413, 0.313, 0.04375, 0.2111, 0.08046, 0.3274, 1.194, 2.157, 20.96, 0.01284, 0.03088, 0.05025, 0.01055, 0.02461, 0.004711, 10.31, 22.65, 65.5, 324.7, 0.1482, 0.4365, 1.252, 0.175, 0.4228, 0.1175],
[20.29, 14.34, 135.1, 1297, 0.1003, 0.1328, 0.198, 0.1043, 0.1809, 0.05883, 0.7572, 0.7813, 5.438, 94.44, 0.01149, 0.02461, 0.05688, 0.01885, 0.01756, 0.005115, 22.65, 18.1, 146.7, 1600, 0.1412, 0.2772, 0.8216, 0.1571, 0.3108, 0.1259],
[12.34, 12.27, 78.94, 468.5, 0.09003, 0.06307, 0.02958, 0.02647, 0.1689, 0.05808, 0.1166, 0.4957, 1.344, 8.966, 0.006808, 0.0187, 0.0198, 0.01185, 0.02037, 0.0027, 13.61, 19.27, 87.22, 564.9, 0.1292, 0.2074, 0.1791, 0.107, 0.311, 0.07592]
],
description = " Press flag if any erroneous output comes ",
theme=gr.themes.Soft(),
title = "Breast Cancer Predictor Model",
live = True
)
# Launch the Gradio interface
input_component.launch(inline = False)