Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,8 +4,7 @@ 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
|
| 8 |
-
import gradio as gr
|
| 9 |
|
| 10 |
# Initialize Streamlit app
|
| 11 |
st.title("White Blood Cell Classifier")
|
|
@@ -13,55 +12,40 @@ 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
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
|
| 45 |
|
| 46 |
-
#
|
| 47 |
-
|
| 48 |
|
| 49 |
-
#
|
| 50 |
-
|
| 51 |
|
| 52 |
-
#
|
| 53 |
-
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
#
|
| 56 |
-
results_df
|
| 57 |
|
| 58 |
-
#
|
| 59 |
-
|
|
|
|
| 60 |
|
| 61 |
-
#
|
| 62 |
-
|
| 63 |
-
st.
|
| 64 |
|
| 65 |
-
|
| 66 |
-
st.
|
| 67 |
-
st.table(results_df)
|
|
|
|
| 4 |
import pandas as pd
|
| 5 |
import requests
|
| 6 |
from io import BytesIO
|
| 7 |
+
from fastai.vision.all import load_learner # Corrected import
|
|
|
|
| 8 |
|
| 9 |
# Initialize Streamlit app
|
| 10 |
st.title("White Blood Cell Classifier")
|
|
|
|
| 12 |
# Load the FastAI model for WBC identification
|
| 13 |
fastai_model = load_learner('model1.pkl')
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
# File uploader for image input
|
| 16 |
+
uploaded_file = st.file_uploader("Upload an image for classification", type=["jpg", "png"])
|
| 17 |
|
| 18 |
+
if uploaded_file:
|
| 19 |
+
# Open the uploaded image
|
| 20 |
+
image = Image.open(uploaded_file)
|
| 21 |
+
|
| 22 |
+
# Display the uploaded image with a caption
|
| 23 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 24 |
|
| 25 |
+
# Perform inference with the FastAI model
|
| 26 |
+
pred, idx, probs = fastai_model.predict(image)
|
| 27 |
|
| 28 |
+
# Display a title for the results section
|
| 29 |
+
st.subheader("White Blood Cell Classification Results")
|
| 30 |
|
| 31 |
+
# Define categories for classification
|
| 32 |
+
categories = ('EOSINOPHIL', 'LYMPHOCYTE', 'MONOCYTE', 'NEUTROPHIL')
|
| 33 |
|
| 34 |
+
# Create a DataFrame with classification probabilities
|
| 35 |
+
results_df = pd.DataFrame(
|
| 36 |
+
{'Cell Type': categories, 'Probability': probs.tolist()}
|
| 37 |
+
)
|
| 38 |
|
| 39 |
+
# Display the probabilities as a bar chart
|
| 40 |
+
st.bar_chart(results_df.set_index('Cell Type'))
|
| 41 |
|
| 42 |
+
# Highlight the most likely class
|
| 43 |
+
most_likely_class = categories[idx]
|
| 44 |
+
st.success(f"Predicted Class: {most_likely_class}")
|
| 45 |
|
| 46 |
+
# Additional information about the probabilities
|
| 47 |
+
st.write("Detailed Classification Results:")
|
| 48 |
+
st.table(results_df)
|
| 49 |
|
| 50 |
+
else:
|
| 51 |
+
st.warning("Upload an image to start classification.")
|
|
|