File size: 3,371 Bytes
7175271 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | import streamlit as st
import os
import requests
import tensorflow as tf
from tensorflow.keras.models import load_model
import numpy as np
from PIL import Image
# π§ Configure the Streamlit page
st.set_page_config(page_title="Leukemia Subtype Detection", layout="centered")
st.markdown("<h1 style='text-align: center; color: #d6336c;'>ποΈ Leukemia Subtype Detection</h1>", unsafe_allow_html=True)
st.markdown("<p style='text-align: center; font-size:18px;'>Upload a blood smear image and detect the leukemia subtype using deep learning models.</p>", unsafe_allow_html=True)
# π Hugging Face Base URL (confirmed valid)
HF_BASE = "https://huggingface.co/GarimaSharma75/Leukemia_Subtype/resolve/main/"
SAVE_DIR = "models"
# π Class labels
CLASS_NAMES = ["Early Pre-B ALL", "Pre-B ALL", "Pro-B ALL", "Healthy"]
# π’ Model files hosted on Hugging Face
model_files = {
"DenseNet121": "DenseNet121_model.keras",
"MobileNetV2": "MobileNetV2_model.keras",
"VGG16": "VGG16_model.keras",
"CustomCNN": "CustomCNN_model.keras"
}
# π₯ Download the model file from Hugging Face if it's not saved locally
def download_if_not_exists(filename):
os.makedirs(SAVE_DIR, exist_ok=True)
filepath = os.path.join(SAVE_DIR, filename)
if not os.path.exists(filepath):
url = HF_BASE + filename
with st.spinner(f"π₯ Downloading `{filename}`..."):
response = requests.get(url)
if response.status_code == 200:
with open(filepath, "wb") as f:
f.write(response.content)
else:
st.error(f"β Failed to download {filename} from Hugging Face.")
st.stop()
return filepath
# π§Ό Image preprocessing
def preprocess(img):
img = img.resize((224, 224))
img_array = np.array(img) / 255.0
return np.expand_dims(img_array, axis=0)
# π Sidebar: Model selection and info
with st.sidebar:
st.markdown("## π§ Select Model")
selected_model = st.selectbox("Choose one model to run", list(model_files.keys()))
st.markdown("### βΉοΈ About the Models")
st.info("""
β’ **DenseNet121** β Deep CNN with dense connections
β’ **MobileNetV2** β Lightweight CNN
β’ **VGG16** β Classic 16-layer CNN
β’ **CustomCNN** β Custom-built architecture
""")
st.markdown("---")
st.markdown("Made by **Garima Sharma** π")
# π€ Upload image
uploaded_file = st.file_uploader("π€ Upload a blood smear image (JPG/PNG)", type=["jpg", "jpeg", "png"])
if uploaded_file:
img = Image.open(uploaded_file).convert("RGB")
st.image(img, caption="Uploaded Image", use_container_width=True)
if st.button("π Run Detection"):
with st.spinner("β³ Please wait while the model is downloading and predicting..."):
input_data = preprocess(img)
model_path = download_if_not_exists(model_files[selected_model])
model = load_model(model_path)
preds = model.predict(input_data)
pred_class = CLASS_NAMES[np.argmax(preds)]
prob = np.max(preds) * 100
st.success(f"β
**{selected_model}** predicts: **{pred_class}** with `{prob:.2f}%` confidence")
else:
st.warning("π Please upload an image to get started.")
|