Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import joblib
|
| 3 |
+
import numpy as np
|
| 4 |
+
import pandas as pd
|
| 5 |
+
|
| 6 |
+
# --- 1. Load Model and Dataset for Feature Information ---
|
| 7 |
+
|
| 8 |
+
@st.cache_data
|
| 9 |
+
def load_data_and_model():
|
| 10 |
+
"""
|
| 11 |
+
Loads the saved model and the dataset from the Excel file.
|
| 12 |
+
Using st.cache_data to avoid reloading on every interaction.
|
| 13 |
+
"""
|
| 14 |
+
try:
|
| 15 |
+
# Load the pre-trained Voting Classifier model
|
| 16 |
+
model = joblib.load('voting_classifier_model.joblib')
|
| 17 |
+
except FileNotFoundError:
|
| 18 |
+
st.error("The model file 'voting_classifier_model.joblib' was not found.")
|
| 19 |
+
st.info("Please ensure the model file is in the same directory as this script.")
|
| 20 |
+
st.stop()
|
| 21 |
+
|
| 22 |
+
try:
|
| 23 |
+
# Load your specific dataset to get feature names and default values
|
| 24 |
+
df = pd.read_excel('breast-cancer.xls')
|
| 25 |
+
# Assuming the first column is 'id' and the second is 'diagnosis' (the target)
|
| 26 |
+
# The rest are the features.
|
| 27 |
+
feature_names = df.columns[2:].tolist()
|
| 28 |
+
|
| 29 |
+
# Store the dataframe for calculating min/max/mean values for sliders
|
| 30 |
+
feature_data = df[feature_names]
|
| 31 |
+
|
| 32 |
+
except FileNotFoundError:
|
| 33 |
+
st.error("The dataset file 'breast-cancer.xls' was not found.")
|
| 34 |
+
st.info("Please ensure your Excel file is in the same directory as this script.")
|
| 35 |
+
st.stop()
|
| 36 |
+
except Exception as e:
|
| 37 |
+
st.error(f"Could not load or process the dataset file. Error: {e}")
|
| 38 |
+
st.stop()
|
| 39 |
+
|
| 40 |
+
return model, feature_names, feature_data
|
| 41 |
+
|
| 42 |
+
model, feature_names, feature_data = load_data_and_model()
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
# --- 2. Streamlit App Interface ---
|
| 46 |
+
|
| 47 |
+
st.set_page_config(page_title="Breast Cancer Predictor", layout="wide")
|
| 48 |
+
|
| 49 |
+
# Main Title
|
| 50 |
+
st.title("🔬 Breast Cancer Prediction Interface")
|
| 51 |
+
st.markdown("""
|
| 52 |
+
This application uses your pre-trained model to predict whether a breast tumor is **Malignant** or **Benign**.
|
| 53 |
+
The input fields below are based on the columns from your `breast-cancer.xls` file.
|
| 54 |
+
""")
|
| 55 |
+
|
| 56 |
+
st.write("---")
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
# --- 3. User Input via Sliders ---
|
| 60 |
+
|
| 61 |
+
st.sidebar.header("Input Tumor Features")
|
| 62 |
+
st.sidebar.markdown("Use the sliders to provide the feature values.")
|
| 63 |
+
|
| 64 |
+
# Dictionary to hold the user's input
|
| 65 |
+
input_features = {}
|
| 66 |
+
|
| 67 |
+
# Create sliders for all features based on your Excel file
|
| 68 |
+
for feature in feature_names:
|
| 69 |
+
# Set min/max/default values from the actual data for better usability
|
| 70 |
+
min_val = float(feature_data[feature].min())
|
| 71 |
+
max_val = float(feature_data[feature].max())
|
| 72 |
+
mean_val = float(feature_data[feature].mean())
|
| 73 |
+
|
| 74 |
+
# Create a slider for each feature
|
| 75 |
+
input_features[feature] = st.sidebar.slider(
|
| 76 |
+
label=f"{feature.replace('_', ' ').title()}",
|
| 77 |
+
min_value=min_val,
|
| 78 |
+
max_value=max_val,
|
| 79 |
+
value=mean_val,
|
| 80 |
+
key=f"slider_{feature}"
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
st.sidebar.write("---")
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
# --- 4. Prediction Logic ---
|
| 87 |
+
|
| 88 |
+
# Convert the dictionary of input features into a NumPy array
|
| 89 |
+
# The order of features must match the order in the feature_names list
|
| 90 |
+
input_data = np.array([list(input_features.values())])
|
| 91 |
+
|
| 92 |
+
# Main section for displaying inputs and results
|
| 93 |
+
st.header("Prediction Results")
|
| 94 |
+
col1, col2 = st.columns([2, 1])
|
| 95 |
+
|
| 96 |
+
with col1:
|
| 97 |
+
st.subheader("Current Input Values")
|
| 98 |
+
st.json(input_features)
|
| 99 |
+
|
| 100 |
+
# "Predict" button
|
| 101 |
+
if st.button("✨ Predict Diagnosis", key="predict_button"):
|
| 102 |
+
try:
|
| 103 |
+
# Make prediction. This returns the string label directly (e.g., 'M' or 'B').
|
| 104 |
+
prediction_label = model.predict(input_data)[0]
|
| 105 |
+
|
| 106 |
+
# Get prediction probabilities. The order corresponds to model.classes_
|
| 107 |
+
prediction_proba = model.predict_proba(input_data)[0]
|
| 108 |
+
|
| 109 |
+
with col2:
|
| 110 |
+
st.subheader("Diagnosis")
|
| 111 |
+
# Display the predicted label directly
|
| 112 |
+
# We check for 'M' or 'B' as is common in this dataset
|
| 113 |
+
if prediction_label.upper() == 'M':
|
| 114 |
+
st.error("Predicted Diagnosis: **Malignant**")
|
| 115 |
+
else:
|
| 116 |
+
st.success("Predicted Diagnosis: **Benign**")
|
| 117 |
+
|
| 118 |
+
st.subheader("Prediction Confidence")
|
| 119 |
+
# Get the class labels from the model itself to ensure correct order
|
| 120 |
+
class_labels = list(model.classes_)
|
| 121 |
+
|
| 122 |
+
# Display probabilities for each class using the model's class order
|
| 123 |
+
for i, label in enumerate(class_labels):
|
| 124 |
+
display_label = "Malignant" if label.upper() == 'M' else "Benign"
|
| 125 |
+
st.write(f"Confidence for **{display_label}**: `{prediction_proba[i]:.2%}`")
|
| 126 |
+
|
| 127 |
+
except Exception as e:
|
| 128 |
+
st.error(f"An error occurred during prediction: {e}")
|
| 129 |
+
|
| 130 |
+
st.write("---")
|