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