Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,11 @@
|
|
| 1 |
-
|
| 2 |
import streamlit as st
|
| 3 |
import PIL.Image as Image
|
| 4 |
import numpy as np
|
| 5 |
import pandas as pd
|
| 6 |
import requests
|
| 7 |
from io import BytesIO
|
| 8 |
-
from fastai.vision.all import load_learner
|
|
|
|
| 9 |
|
| 10 |
# Initialize Streamlit app
|
| 11 |
st.title("White Blood Cell Classifier")
|
|
@@ -13,42 +13,55 @@ st.title("White Blood Cell Classifier")
|
|
| 13 |
# Load the FastAI model for WBC identification
|
| 14 |
fastai_model = load_learner('model1.pkl')
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
{'Cell Type': categories, 'Probability': probs.tolist()}
|
| 38 |
-
)
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
st.success(f"Predicted Class: {most_likely_class}")
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
st.table(results_df)
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
|
|
|
|
|
|
|
|
|
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import PIL.Image as Image
|
| 3 |
import numpy as np
|
| 4 |
import pandas as pd
|
| 5 |
import requests
|
| 6 |
from io import BytesIO
|
| 7 |
+
from fastai.vision.all import load_learner, untar_data, URLs
|
| 8 |
+
import gradio as gr
|
| 9 |
|
| 10 |
# Initialize Streamlit app
|
| 11 |
st.title("White Blood Cell Classifier")
|
|
|
|
| 13 |
# Load the FastAI model for WBC identification
|
| 14 |
fastai_model = load_learner('model1.pkl')
|
| 15 |
|
| 16 |
+
# Pre-load some example images with their corresponding labels
|
| 17 |
+
example_images = {
|
| 18 |
+
"Eosinophil": "Eosinophil.jpg",
|
| 19 |
+
"Lymphocyte": "Lymphocyte.jpg",
|
| 20 |
+
"Monocyte": "Monocyte.jpg",
|
| 21 |
+
"Neutrophil": "Neutrophil.jpg",
|
| 22 |
+
}
|
| 23 |
|
| 24 |
+
# Provide a choice of example images
|
| 25 |
+
st.sidebar.header("Example Images")
|
| 26 |
+
example_choice = st.sidebar.selectbox(
|
| 27 |
+
"Select an example image to classify",
|
| 28 |
+
list(example_images.keys())
|
| 29 |
+
)
|
| 30 |
|
| 31 |
+
# Display the chosen example image
|
| 32 |
+
example_url = example_images[example_choice]
|
| 33 |
+
response = requests.get(example_url)
|
| 34 |
+
example_image = Image.open(BytesIO(response.content))
|
| 35 |
+
st.sidebar.image(example_image, caption=f"Example: {example_choice}", use_column_width=True)
|
| 36 |
+
|
| 37 |
+
# File uploader for image input
|
| 38 |
+
uploaded_file = st.file_uploader("Upload your own image for classification", type=["jpg", "png"])
|
| 39 |
|
| 40 |
+
# Check if an image has been uploaded or an example has been selected
|
| 41 |
+
image_to_classify = example_image if uploaded_file is None else Image.open(uploaded_file)
|
| 42 |
|
| 43 |
+
# Display the image being classified
|
| 44 |
+
st.image(image_to_classify, caption="Image for Classification", use_column_width=True)
|
| 45 |
|
| 46 |
+
# Perform inference with the FastAI model
|
| 47 |
+
pred, idx, probs = fastai_model.predict(image_to_classify)
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
# Display a subheader for results
|
| 50 |
+
st.subheader("White Blood Cell Classification Results")
|
| 51 |
|
| 52 |
+
# Define categories for classification
|
| 53 |
+
categories = ('EOSINOPHIL', 'LYMPHOCYTE', 'MONOCYTE', 'NEUTROPHIL')
|
|
|
|
| 54 |
|
| 55 |
+
# Create a DataFrame with classification probabilities
|
| 56 |
+
results_df = pd.DataFrame({'Cell Type': categories, 'Probability': probs.tolist()})
|
|
|
|
| 57 |
|
| 58 |
+
# Display probabilities as a bar chart
|
| 59 |
+
st.bar_chart(results_df.set_index('Cell Type'))
|
| 60 |
|
| 61 |
+
# Highlight the most likely class
|
| 62 |
+
most_likely_class = categories[idx]
|
| 63 |
+
st.success(f"Predicted Class: {most_likely_class}")
|
| 64 |
|
| 65 |
+
# Additional information about probabilities
|
| 66 |
+
st.write("Detailed Classification Results:")
|
| 67 |
+
st.table(results_df)
|