Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -15,8 +15,11 @@ st.set_page_config(
|
|
15 |
|
16 |
# Initialize session state
|
17 |
if 'model' not in st.session_state:
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
20 |
|
21 |
def main():
|
22 |
# Header
|
@@ -49,36 +52,41 @@ def single_bug_analysis():
|
|
49 |
uploaded_file = st.file_uploader("Upload a bug photo", type=['png', 'jpg', 'jpeg'], key="single")
|
50 |
|
51 |
if uploaded_file:
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
with
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
|
|
77 |
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
|
|
|
|
|
|
|
|
82 |
|
83 |
def compare_bugs():
|
84 |
col1, col2 = st.columns(2)
|
@@ -86,43 +94,56 @@ def compare_bugs():
|
|
86 |
with col1:
|
87 |
file1 = st.file_uploader("Upload first bug photo", type=['png', 'jpg', 'jpeg'], key="compare1")
|
88 |
if file1:
|
89 |
-
|
90 |
-
|
|
|
|
|
|
|
|
|
91 |
|
92 |
with col2:
|
93 |
file2 = st.file_uploader("Upload second bug photo", type=['png', 'jpg', 'jpeg'], key="compare2")
|
94 |
if file2:
|
95 |
-
|
96 |
-
|
|
|
|
|
|
|
|
|
97 |
|
98 |
if file1 and file2:
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
# Generate Grad-CAM visualizations
|
105 |
-
gradcam1 = generate_gradcam(image1, st.session_state.model)
|
106 |
-
gradcam2 = generate_gradcam(image2, st.session_state.model)
|
107 |
-
|
108 |
-
# Display results
|
109 |
-
st.markdown("### Comparison Results")
|
110 |
-
comp_col1, comp_col2 = st.columns(2)
|
111 |
-
|
112 |
-
with comp_col1:
|
113 |
-
st.markdown(f"**Species 1**: {pred1}")
|
114 |
-
st.markdown(f"Confidence: {conf1:.2f}%")
|
115 |
-
st.image(gradcam1, caption="Feature Highlights - Bug 1", use_column_width=True)
|
116 |
|
117 |
-
|
118 |
-
st.
|
119 |
-
st.
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
|
127 |
if __name__ == "__main__":
|
128 |
main()
|
|
|
15 |
|
16 |
# Initialize session state
|
17 |
if 'model' not in st.session_state:
|
18 |
+
try:
|
19 |
+
st.session_state.model = BugClassifier()
|
20 |
+
st.session_state.feature_extractor = AutoFeatureExtractor.from_pretrained("google/vit-base-patch16-224")
|
21 |
+
except Exception as e:
|
22 |
+
st.error(f"Error loading model: {str(e)}")
|
23 |
|
24 |
def main():
|
25 |
# Header
|
|
|
52 |
uploaded_file = st.file_uploader("Upload a bug photo", type=['png', 'jpg', 'jpeg'], key="single")
|
53 |
|
54 |
if uploaded_file:
|
55 |
+
try:
|
56 |
+
image = Image.open(uploaded_file)
|
57 |
+
col1, col2 = st.columns(2)
|
58 |
+
|
59 |
+
with col1:
|
60 |
+
st.image(image, caption="Uploaded Image", use_container_width=True)
|
61 |
+
|
62 |
+
with col2:
|
63 |
+
with st.spinner("Analyzing your bug..."):
|
64 |
+
# Get predictions
|
65 |
+
prediction, confidence = st.session_state.model.predict(image)
|
66 |
+
severity = get_severity_prediction(prediction)
|
67 |
+
|
68 |
+
st.success("Analysis Complete!")
|
69 |
+
st.markdown(f"### Identified Species")
|
70 |
+
st.markdown(f"**{prediction}**")
|
71 |
+
st.markdown(f"Confidence: {confidence:.2f}%")
|
72 |
+
|
73 |
+
st.markdown("### Ecological Impact")
|
74 |
+
severity_color = {
|
75 |
+
"Low": "green",
|
76 |
+
"Medium": "orange",
|
77 |
+
"High": "red"
|
78 |
+
}
|
79 |
+
st.markdown(f"Severity: <span style='color: {severity_color[severity]}'>{severity}</span>",
|
80 |
+
unsafe_allow_html=True)
|
81 |
|
82 |
+
# Generate and display species information
|
83 |
+
st.markdown("### About This Species")
|
84 |
+
species_info = st.session_state.model.get_species_info(prediction)
|
85 |
+
st.markdown(species_info)
|
86 |
+
|
87 |
+
except Exception as e:
|
88 |
+
st.error(f"Error processing image: {str(e)}")
|
89 |
+
st.info("Please try uploading a different image.")
|
90 |
|
91 |
def compare_bugs():
|
92 |
col1, col2 = st.columns(2)
|
|
|
94 |
with col1:
|
95 |
file1 = st.file_uploader("Upload first bug photo", type=['png', 'jpg', 'jpeg'], key="compare1")
|
96 |
if file1:
|
97 |
+
try:
|
98 |
+
image1 = Image.open(file1)
|
99 |
+
st.image(image1, caption="First Bug", use_container_width=True)
|
100 |
+
except Exception as e:
|
101 |
+
st.error(f"Error loading first image: {str(e)}")
|
102 |
+
return
|
103 |
|
104 |
with col2:
|
105 |
file2 = st.file_uploader("Upload second bug photo", type=['png', 'jpg', 'jpeg'], key="compare2")
|
106 |
if file2:
|
107 |
+
try:
|
108 |
+
image2 = Image.open(file2)
|
109 |
+
st.image(image2, caption="Second Bug", use_container_width=True)
|
110 |
+
except Exception as e:
|
111 |
+
st.error(f"Error loading second image: {str(e)}")
|
112 |
+
return
|
113 |
|
114 |
if file1 and file2:
|
115 |
+
try:
|
116 |
+
with st.spinner("Generating comparison..."):
|
117 |
+
# Get predictions for both images
|
118 |
+
pred1, conf1 = st.session_state.model.predict(image1)
|
119 |
+
pred2, conf2 = st.session_state.model.predict(image2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
|
121 |
+
# Generate Grad-CAM visualizations
|
122 |
+
gradcam1 = generate_gradcam(image1, st.session_state.model)
|
123 |
+
gradcam2 = generate_gradcam(image2, st.session_state.model)
|
124 |
+
|
125 |
+
# Display results
|
126 |
+
st.markdown("### Comparison Results")
|
127 |
+
comp_col1, comp_col2 = st.columns(2)
|
128 |
+
|
129 |
+
with comp_col1:
|
130 |
+
st.markdown(f"**Species 1**: {pred1}")
|
131 |
+
st.markdown(f"Confidence: {conf1:.2f}%")
|
132 |
+
st.image(gradcam1, caption="Feature Highlights - Bug 1", use_container_width=True)
|
133 |
+
|
134 |
+
with comp_col2:
|
135 |
+
st.markdown(f"**Species 2**: {pred2}")
|
136 |
+
st.markdown(f"Confidence: {conf2:.2f}%")
|
137 |
+
st.image(gradcam2, caption="Feature Highlights - Bug 2", use_container_width=True)
|
138 |
+
|
139 |
+
# Display comparison information
|
140 |
+
st.markdown("### Key Differences")
|
141 |
+
differences = st.session_state.model.compare_species(pred1, pred2)
|
142 |
+
st.markdown(differences)
|
143 |
+
|
144 |
+
except Exception as e:
|
145 |
+
st.error(f"Error comparing images: {str(e)}")
|
146 |
+
st.info("Please try uploading different images or try again.")
|
147 |
|
148 |
if __name__ == "__main__":
|
149 |
main()
|