Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,161 +1,65 @@
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from PIL import Image
|
| 3 |
-
import
|
| 4 |
-
from model_utils import BugClassifier
|
| 5 |
-
from transformers import AutoFeatureExtractor
|
| 6 |
|
| 7 |
-
# Page configuration
|
| 8 |
st.set_page_config(
|
| 9 |
page_title="Bug-O-Scope ππ",
|
| 10 |
page_icon="π",
|
| 11 |
-
layout="wide"
|
| 12 |
-
initial_sidebar_state="expanded"
|
| 13 |
)
|
| 14 |
|
| 15 |
-
# Initialize session state
|
| 16 |
@st.cache_resource
|
| 17 |
def load_model():
|
| 18 |
-
|
| 19 |
-
return BugClassifier(), AutoFeatureExtractor.from_pretrained("google/vit-base-patch16-224")
|
| 20 |
-
except Exception as e:
|
| 21 |
-
st.error(f"Error loading model: {str(e)}")
|
| 22 |
-
return None, None
|
| 23 |
-
|
| 24 |
-
if 'model' not in st.session_state:
|
| 25 |
-
st.session_state.model, st.session_state.feature_extractor = load_model()
|
| 26 |
|
| 27 |
def main():
|
| 28 |
-
# Header
|
| 29 |
st.title("Bug-O-Scope ππ")
|
| 30 |
-
st.markdown(
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
| 34 |
|
| 35 |
-
# Sidebar
|
| 36 |
st.sidebar.header("About Bug-O-Scope")
|
| 37 |
-
st.sidebar.markdown(
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
# Main content
|
| 46 |
tab1, tab2 = st.tabs(["Single Bug Analysis", "Bug Comparison"])
|
| 47 |
|
| 48 |
with tab1:
|
| 49 |
-
single_bug_analysis()
|
| 50 |
|
| 51 |
with tab2:
|
| 52 |
-
|
| 53 |
|
| 54 |
-
def single_bug_analysis():
|
| 55 |
-
"
|
| 56 |
-
uploaded_file = st.file_uploader("Upload a bug photo", type=['png', 'jpg', 'jpeg'], key="single")
|
| 57 |
-
|
| 58 |
-
if uploaded_file:
|
| 59 |
-
try:
|
| 60 |
-
image = Image.open(uploaded_file)
|
| 61 |
-
col1, col2 = st.columns(2)
|
| 62 |
-
|
| 63 |
-
with col1:
|
| 64 |
-
st.image(image, caption="Uploaded Image", use_container_width=True)
|
| 65 |
-
|
| 66 |
-
with col2:
|
| 67 |
-
with st.spinner("Analyzing your bug..."):
|
| 68 |
-
# Get predictions
|
| 69 |
-
prediction, confidence = st.session_state.model.predict(image)
|
| 70 |
-
severity = get_severity_prediction(prediction)
|
| 71 |
-
|
| 72 |
-
st.success("Analysis Complete!")
|
| 73 |
-
st.markdown(f"### Identified Species")
|
| 74 |
-
st.markdown(f"**{prediction}**")
|
| 75 |
-
st.markdown(f"Confidence: {confidence:.2f}%")
|
| 76 |
-
|
| 77 |
-
st.markdown("### Ecological Impact")
|
| 78 |
-
severity_color = {
|
| 79 |
-
"Low": "green",
|
| 80 |
-
"Medium": "orange",
|
| 81 |
-
"High": "red"
|
| 82 |
-
}
|
| 83 |
-
st.markdown(
|
| 84 |
-
f"Severity: <span style='color: {severity_color[severity]}'>{severity}</span>",
|
| 85 |
-
unsafe_allow_html=True
|
| 86 |
-
)
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
st.markdown(species_info)
|
| 92 |
-
|
| 93 |
-
# Display Grad-CAM visualization
|
| 94 |
-
st.markdown("### Feature Highlights")
|
| 95 |
-
gradcam = st.session_state.model.get_gradcam(image)
|
| 96 |
-
st.image(gradcam, caption="Important Features", use_container_width=True)
|
| 97 |
-
|
| 98 |
-
except Exception as e:
|
| 99 |
-
st.error(f"Error processing image: {str(e)}")
|
| 100 |
-
st.info("Please try uploading a different image.")
|
| 101 |
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
file1 = st.file_uploader("Upload first bug photo", type=['png', 'jpg', 'jpeg'], key="compare1")
|
| 108 |
-
if file1:
|
| 109 |
-
try:
|
| 110 |
-
image1 = Image.open(file1)
|
| 111 |
-
st.image(image1, caption="First Bug", use_container_width=True)
|
| 112 |
-
except Exception as e:
|
| 113 |
-
st.error(f"Error loading first image: {str(e)}")
|
| 114 |
-
return
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
image2 = Image.open(file2)
|
| 121 |
-
st.image(image2, caption="Second Bug", use_container_width=True)
|
| 122 |
-
except Exception as e:
|
| 123 |
-
st.error(f"Error loading second image: {str(e)}")
|
| 124 |
-
return
|
| 125 |
-
|
| 126 |
-
if file1 and file2:
|
| 127 |
-
try:
|
| 128 |
-
with st.spinner("Generating comparison..."):
|
| 129 |
-
# Get predictions for both images
|
| 130 |
-
pred1, conf1 = st.session_state.model.predict(image1)
|
| 131 |
-
pred2, conf2 = st.session_state.model.predict(image2)
|
| 132 |
-
|
| 133 |
-
# Generate Grad-CAM visualizations
|
| 134 |
-
gradcam1 = st.session_state.model.get_gradcam(image1)
|
| 135 |
-
gradcam2 = st.session_state.model.get_gradcam(image2)
|
| 136 |
-
|
| 137 |
-
# Display results
|
| 138 |
-
st.markdown("### Comparison Results")
|
| 139 |
-
comp_col1, comp_col2 = st.columns(2)
|
| 140 |
-
|
| 141 |
-
with comp_col1:
|
| 142 |
-
st.markdown(f"**Species 1**: {pred1}")
|
| 143 |
-
st.markdown(f"Confidence: {conf1:.2f}%")
|
| 144 |
-
st.image(gradcam1, caption="Feature Highlights - Bug 1", use_container_width=True)
|
| 145 |
-
|
| 146 |
-
with comp_col2:
|
| 147 |
-
st.markdown(f"**Species 2**: {pred2}")
|
| 148 |
-
st.markdown(f"Confidence: {conf2:.2f}%")
|
| 149 |
-
st.image(gradcam2, caption="Feature Highlights - Bug 2", use_container_width=True)
|
| 150 |
-
|
| 151 |
-
# Display comparison information
|
| 152 |
-
st.markdown("### Key Differences")
|
| 153 |
-
differences = st.session_state.model.compare_species(pred1, pred2)
|
| 154 |
-
st.markdown(differences)
|
| 155 |
-
|
| 156 |
-
except Exception as e:
|
| 157 |
-
st.error(f"Error comparing images: {str(e)}")
|
| 158 |
-
st.info("Please try uploading different images or try again.")
|
| 159 |
|
| 160 |
if __name__ == "__main__":
|
| 161 |
main()
|
|
|
|
| 1 |
+
# 1. app.py
|
| 2 |
+
# Main Streamlit app for UI and user interaction
|
| 3 |
import streamlit as st
|
| 4 |
from PIL import Image
|
| 5 |
+
from dataset_utils import load_insect_dataset, preprocess_image
|
| 6 |
+
from model_utils import BugClassifier
|
|
|
|
| 7 |
|
|
|
|
| 8 |
st.set_page_config(
|
| 9 |
page_title="Bug-O-Scope ππ",
|
| 10 |
page_icon="π",
|
| 11 |
+
layout="wide"
|
|
|
|
| 12 |
)
|
| 13 |
|
|
|
|
| 14 |
@st.cache_resource
|
| 15 |
def load_model():
|
| 16 |
+
return BugClassifier()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
def main():
|
|
|
|
| 19 |
st.title("Bug-O-Scope ππ")
|
| 20 |
+
st.markdown(
|
| 21 |
+
"""
|
| 22 |
+
Welcome to Bug-O-Scope! Upload a photo of an insect to identify it and learn about its ecological importance.
|
| 23 |
+
"""
|
| 24 |
+
)
|
| 25 |
|
|
|
|
| 26 |
st.sidebar.header("About Bug-O-Scope")
|
| 27 |
+
st.sidebar.markdown(
|
| 28 |
+
"""
|
| 29 |
+
Bug-O-Scope is powered by AI to help you:
|
| 30 |
+
- Identify insects
|
| 31 |
+
- Learn about species
|
| 32 |
+
- Understand their ecological roles
|
| 33 |
+
"""
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
model = load_model()
|
| 37 |
|
|
|
|
| 38 |
tab1, tab2 = st.tabs(["Single Bug Analysis", "Bug Comparison"])
|
| 39 |
|
| 40 |
with tab1:
|
| 41 |
+
single_bug_analysis(model)
|
| 42 |
|
| 43 |
with tab2:
|
| 44 |
+
st.markdown("Bug comparison feature coming soon!")
|
| 45 |
|
| 46 |
+
def single_bug_analysis(model):
|
| 47 |
+
uploaded_file = st.file_uploader("Upload a bug photo", type=['png', 'jpg', 'jpeg'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
if uploaded_file:
|
| 50 |
+
image = Image.open(uploaded_file)
|
| 51 |
+
st.image(image, caption="Uploaded Image", use_container_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
with st.spinner("Analyzing your bug..."):
|
| 54 |
+
prediction, confidence = model.predict(image)
|
| 55 |
+
st.success("Analysis Complete!")
|
| 56 |
+
st.markdown(f"**Identified Species**: {prediction}")
|
| 57 |
+
st.markdown(f"**Confidence**: {confidence:.2f}%")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
+
if prediction != "Unknown Insect":
|
| 60 |
+
species_info = model.get_species_info(prediction)
|
| 61 |
+
st.markdown("### About This Species")
|
| 62 |
+
st.markdown(species_info)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
if __name__ == "__main__":
|
| 65 |
main()
|