Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,218 +1,240 @@
|
|
| 1 |
-
# app.py
|
| 2 |
import streamlit as st
|
| 3 |
-
import numpy as np
|
| 4 |
-
import os
|
| 5 |
import tensorflow as tf
|
| 6 |
-
import
|
|
|
|
|
|
|
| 7 |
from PIL import Image
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
|
| 13 |
-
# Set page configuration
|
| 14 |
st.set_page_config(
|
| 15 |
-
page_title="Breast Cancer Prediction",
|
| 16 |
-
page_icon="
|
| 17 |
-
layout="wide"
|
| 18 |
-
initial_sidebar_state="expanded"
|
| 19 |
)
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
def preprocess_image(image):
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
""
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
#
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
.
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
# Status indicator
|
| 150 |
-
status = "✅ Model loaded successfully" if model else "❌ Model failed to load"
|
| 151 |
-
st.info(status)
|
| 152 |
-
|
| 153 |
-
# Create two columns for layout
|
| 154 |
-
col1, col2 = st.columns([1, 1])
|
| 155 |
-
|
| 156 |
-
# Input column
|
| 157 |
-
with col1:
|
| 158 |
-
st.subheader("Patient Information")
|
| 159 |
-
|
| 160 |
-
# Input fields
|
| 161 |
-
age = st.number_input("Patient Age", min_value=18, max_value=100, value=45)
|
| 162 |
-
tumor_size = st.number_input("Tumor Size (mm)", min_value=0.1, value=15.0)
|
| 163 |
-
|
| 164 |
-
# Image upload
|
| 165 |
-
uploaded_file = st.file_uploader(
|
| 166 |
-
"Upload Medical Image",
|
| 167 |
-
type=["jpg", "jpeg", "png"],
|
| 168 |
-
help="Supported formats: JPG, JPEG, PNG"
|
| 169 |
)
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
)
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
st.
|
| 215 |
-
|
| 216 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
st.markdown("---")
|
| 218 |
-
st.caption("
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
| 2 |
import tensorflow as tf
|
| 3 |
+
import numpy as np
|
| 4 |
+
import joblib
|
| 5 |
+
import json
|
| 6 |
from PIL import Image
|
| 7 |
+
import pandas as pd
|
| 8 |
+
from lifelines import CoxPHFitter
|
| 9 |
|
| 10 |
+
# ---------------------------------------------------
|
| 11 |
+
# CONFIG
|
| 12 |
+
# ---------------------------------------------------
|
| 13 |
|
|
|
|
| 14 |
st.set_page_config(
|
| 15 |
+
page_title="Breast Cancer Survival Prediction",
|
| 16 |
+
page_icon="🧬",
|
| 17 |
+
layout="wide"
|
|
|
|
| 18 |
)
|
| 19 |
|
| 20 |
+
# CNN_MODEL_PATH = "best_breast_cancer_cnn.keras"
|
| 21 |
+
CNN_MODEL_PATH = "final_combined_model.keras"
|
| 22 |
+
DNN_MODEL_PATH = "survival_model.keras"
|
| 23 |
+
|
| 24 |
+
SCALER_PATH = "scaler.pkl"
|
| 25 |
+
FEATURES_PATH = "features.json"
|
| 26 |
+
|
| 27 |
+
DATASET_PATH = 'processed_breast_cancer_data(1).csv'
|
| 28 |
+
TIME_COL = "Overall_Survival_Months"
|
| 29 |
+
EVENT_COL = "Event"
|
| 30 |
+
ID_COL = "Patient_ID"
|
| 31 |
+
|
| 32 |
+
# ---------------------------------------------------
|
| 33 |
+
# LOAD MODELS
|
| 34 |
+
# ---------------------------------------------------
|
| 35 |
+
|
| 36 |
+
@st.cache_resource
|
| 37 |
+
def load_cnn():
|
| 38 |
+
return tf.keras.models.load_model(CNN_MODEL_PATH, compile=False)
|
| 39 |
+
|
| 40 |
+
@st.cache_resource
|
| 41 |
+
def load_dnn():
|
| 42 |
+
return tf.keras.models.load_model(DNN_MODEL_PATH, compile=False)
|
| 43 |
+
|
| 44 |
+
# ---------------------------------------------------
|
| 45 |
+
# LOAD SURVIVAL ASSETS (COMPUTE BRESLOW BASELINE)
|
| 46 |
+
# ---------------------------------------------------
|
| 47 |
+
|
| 48 |
+
@st.cache_resource
|
| 49 |
+
def load_survival_assets():
|
| 50 |
+
|
| 51 |
+
scaler = joblib.load(SCALER_PATH)
|
| 52 |
+
features = json.load(open(FEATURES_PATH))
|
| 53 |
+
|
| 54 |
+
df = pd.read_csv(DATASET_PATH)
|
| 55 |
+
|
| 56 |
+
feature_df = df[features].copy()
|
| 57 |
+
feature_df["duration"] = df[TIME_COL]
|
| 58 |
+
feature_df["event"] = df[EVENT_COL]
|
| 59 |
+
|
| 60 |
+
cox = CoxPHFitter()
|
| 61 |
+
cox.fit(feature_df, duration_col="duration", event_col="event")
|
| 62 |
+
|
| 63 |
+
baseline = cox.baseline_cumulative_hazard_
|
| 64 |
+
|
| 65 |
+
breslow_times = baseline.index.values
|
| 66 |
+
breslow_H0 = baseline.values.flatten()
|
| 67 |
+
|
| 68 |
+
return scaler, features, breslow_times, breslow_H0
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
cnn_model = load_cnn()
|
| 72 |
+
dnn_model = load_dnn()
|
| 73 |
+
scaler, feature_cols, breslow_times, breslow_H0 = load_survival_assets()
|
| 74 |
+
|
| 75 |
+
# ---------------------------------------------------
|
| 76 |
+
# IMAGE PREPROCESSING
|
| 77 |
+
# ---------------------------------------------------
|
| 78 |
+
|
| 79 |
def preprocess_image(image):
|
| 80 |
+
|
| 81 |
+
if image.mode != "L":
|
| 82 |
+
image = image.convert("L")
|
| 83 |
+
|
| 84 |
+
image = image.resize((224, 224))
|
| 85 |
+
|
| 86 |
+
img = np.array(image) / 255.0
|
| 87 |
+
img = img[np.newaxis, ..., np.newaxis]
|
| 88 |
+
|
| 89 |
+
return img
|
| 90 |
+
|
| 91 |
+
# ---------------------------------------------------
|
| 92 |
+
# CNN PREDICTION
|
| 93 |
+
# ---------------------------------------------------
|
| 94 |
+
|
| 95 |
+
def predict_cancer(image):
|
| 96 |
+
|
| 97 |
+
img = preprocess_image(image)
|
| 98 |
+
|
| 99 |
+
pred = cnn_model.predict(img, verbose=0)[0][0]
|
| 100 |
+
|
| 101 |
+
result = "Malignant" if pred > 0.5 else "Benign"
|
| 102 |
+
|
| 103 |
+
confidence = pred if pred > 0.5 else 1 - pred
|
| 104 |
+
|
| 105 |
+
return result, confidence, pred
|
| 106 |
+
|
| 107 |
+
# ---------------------------------------------------
|
| 108 |
+
# SURVIVAL FUNCTION
|
| 109 |
+
# ---------------------------------------------------
|
| 110 |
+
|
| 111 |
+
def survival_prob(risk, t):
|
| 112 |
+
|
| 113 |
+
idx = np.searchsorted(breslow_times, t, side="right") - 1
|
| 114 |
+
|
| 115 |
+
if idx < 0:
|
| 116 |
+
return 1.0
|
| 117 |
+
|
| 118 |
+
h0 = breslow_H0[idx]
|
| 119 |
+
|
| 120 |
+
return float(np.exp(-h0 * np.exp(risk)))
|
| 121 |
+
|
| 122 |
+
# ---------------------------------------------------
|
| 123 |
+
# SURVIVAL PREDICTION
|
| 124 |
+
# ---------------------------------------------------
|
| 125 |
+
|
| 126 |
+
def predict_survival(feature_values):
|
| 127 |
+
|
| 128 |
+
row = np.array([feature_values], dtype=np.float32)
|
| 129 |
+
|
| 130 |
+
row = scaler.transform(row)
|
| 131 |
+
|
| 132 |
+
risk = float(dnn_model.predict(row, verbose=0)[0][0])
|
| 133 |
+
|
| 134 |
+
s1 = survival_prob(risk, 12) * 100
|
| 135 |
+
s3 = survival_prob(risk, 36) * 100
|
| 136 |
+
s5 = survival_prob(risk, 60) * 100
|
| 137 |
+
|
| 138 |
+
return risk, s1, s3, s5
|
| 139 |
+
|
| 140 |
+
# ---------------------------------------------------
|
| 141 |
+
# UI
|
| 142 |
+
# ---------------------------------------------------
|
| 143 |
+
|
| 144 |
+
st.title("🧬 Breast Cancer AI Diagnosis & Survival System")
|
| 145 |
+
|
| 146 |
+
st.markdown(
|
| 147 |
+
"""
|
| 148 |
+
This system integrates two AI models:
|
| 149 |
+
|
| 150 |
+
• **CNN model** → detects tumor malignancy from medical images
|
| 151 |
+
• **Survival DNN** → predicts patient survival probabilities
|
| 152 |
+
"""
|
| 153 |
+
)
|
| 154 |
+
|
| 155 |
+
tab1, tab2 = st.tabs(["🔬 Image Diagnosis", "📈 Survival Analysis"])
|
| 156 |
+
|
| 157 |
+
# ---------------------------------------------------
|
| 158 |
+
# TAB 1 : IMAGE PREDICTION
|
| 159 |
+
# ---------------------------------------------------
|
| 160 |
+
|
| 161 |
+
with tab1:
|
| 162 |
+
|
| 163 |
+
st.header("Tumor Image Classification")
|
| 164 |
+
|
| 165 |
+
uploaded = st.file_uploader(
|
| 166 |
+
"Upload Histopathology Image",
|
| 167 |
+
type=["png", "jpg", "jpeg"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
)
|
| 169 |
+
|
| 170 |
+
if uploaded:
|
| 171 |
+
|
| 172 |
+
image = Image.open(uploaded)
|
| 173 |
+
|
| 174 |
+
st.image(image, width=300)
|
| 175 |
+
|
| 176 |
+
if st.button("Analyze Image"):
|
| 177 |
+
|
| 178 |
+
result, conf, score = predict_cancer(image)
|
| 179 |
+
|
| 180 |
+
st.subheader("Prediction Result")
|
| 181 |
+
|
| 182 |
+
col1, col2 = st.columns(2)
|
| 183 |
+
|
| 184 |
+
col1.metric("Diagnosis", result)
|
| 185 |
+
|
| 186 |
+
col2.metric("Confidence", f"{conf*100:.2f}%")
|
| 187 |
+
|
| 188 |
+
st.write("Prediction Score:", round(score, 4))
|
| 189 |
+
|
| 190 |
+
# ---------------------------------------------------
|
| 191 |
+
# TAB 2 : SURVIVAL ANALYSIS
|
| 192 |
+
# ---------------------------------------------------
|
| 193 |
+
|
| 194 |
+
with tab2:
|
| 195 |
+
|
| 196 |
+
st.header("Patient Survival Prediction")
|
| 197 |
+
|
| 198 |
+
st.write("Enter patient clinical features")
|
| 199 |
+
|
| 200 |
+
inputs = []
|
| 201 |
+
|
| 202 |
+
cols = st.columns(3)
|
| 203 |
+
|
| 204 |
+
for i, f in enumerate(feature_cols):
|
| 205 |
+
|
| 206 |
+
value = cols[i % 3].number_input(
|
| 207 |
+
f,
|
| 208 |
+
value=0.0,
|
| 209 |
+
step=0.1
|
| 210 |
)
|
| 211 |
+
|
| 212 |
+
inputs.append(value)
|
| 213 |
+
|
| 214 |
+
if st.button("Predict Survival"):
|
| 215 |
+
|
| 216 |
+
risk, s1, s3, s5 = predict_survival(inputs)
|
| 217 |
+
|
| 218 |
+
st.subheader("Risk Score")
|
| 219 |
+
|
| 220 |
+
st.metric("Risk Score", round(risk, 4))
|
| 221 |
+
|
| 222 |
+
st.subheader("Survival Probability")
|
| 223 |
+
|
| 224 |
+
c1, c2, c3 = st.columns(3)
|
| 225 |
+
|
| 226 |
+
c1.metric("1-Year Survival", f"{s1:.1f}%")
|
| 227 |
+
c2.metric("3-Year Survival", f"{s3:.1f}%")
|
| 228 |
+
c3.metric("5-Year Survival", f"{s5:.1f}%")
|
| 229 |
+
|
| 230 |
+
if risk > 0:
|
| 231 |
+
st.error("High Risk Category")
|
| 232 |
+
else:
|
| 233 |
+
st.success("Low Risk Category")
|
| 234 |
+
|
| 235 |
+
# ---------------------------------------------------
|
| 236 |
+
# FOOTER
|
| 237 |
+
# ---------------------------------------------------
|
| 238 |
+
|
| 239 |
st.markdown("---")
|
| 240 |
+
st.caption("AI-assisted clinical decision support system")
|