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("

๐ŸŽ—๏ธ Leukemia Subtype Detection

", unsafe_allow_html=True) st.markdown("

Upload a blood smear image and detect the leukemia subtype using deep learning models.

", 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.")