dalybuilds commited on
Commit
117f15d
Β·
verified Β·
1 Parent(s): bb6d01d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +128 -0
app.py ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import torch
3
+ from PIL import Image
4
+ import io
5
+ import numpy as np
6
+ from model_utils import BugClassifier, generate_gradcam, get_severity_prediction
7
+ from transformers import AutoFeatureExtractor
8
+
9
+ # Page configuration
10
+ st.set_page_config(
11
+ page_title="Bug-O-Scope πŸ”πŸž",
12
+ page_icon="πŸ”",
13
+ layout="wide"
14
+ )
15
+
16
+ # Initialize session state
17
+ if 'model' not in st.session_state:
18
+ st.session_state.model = BugClassifier()
19
+ st.session_state.feature_extractor = AutoFeatureExtractor.from_pretrained("google/vit-base-patch16-224")
20
+
21
+ def main():
22
+ # Header
23
+ st.title("Bug-O-Scope πŸ”πŸž")
24
+ st.markdown("""
25
+ Welcome to Bug-O-Scope! Upload a picture of an insect to learn more about it.
26
+ This educational tool helps you identify bugs and understand their role in our ecosystem.
27
+ """)
28
+
29
+ # Sidebar
30
+ st.sidebar.header("About Bug-O-Scope")
31
+ st.sidebar.markdown("""
32
+ Bug-O-Scope is an AI-powered tool that helps you:
33
+ * πŸ” Identify insects from photos
34
+ * πŸ“š Learn about different species
35
+ * 🌍 Understand their ecological impact
36
+ * πŸ”¬ Compare different insects
37
+ """)
38
+
39
+ # Main content
40
+ tab1, tab2 = st.tabs(["Single Bug Analysis", "Bug Comparison"])
41
+
42
+ with tab1:
43
+ single_bug_analysis()
44
+
45
+ with tab2:
46
+ compare_bugs()
47
+
48
+ 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
+ image = Image.open(uploaded_file)
53
+ col1, col2 = st.columns(2)
54
+
55
+ with col1:
56
+ st.image(image, caption="Uploaded Image", use_column_width=True)
57
+
58
+ with col2:
59
+ with st.spinner("Analyzing your bug..."):
60
+ # Get predictions
61
+ prediction, confidence = st.session_state.model.predict(image)
62
+ severity = get_severity_prediction(prediction)
63
+
64
+ st.success("Analysis Complete!")
65
+ st.markdown(f"### Identified Species")
66
+ st.markdown(f"**{prediction}**")
67
+ st.markdown(f"Confidence: {confidence:.2f}%")
68
+
69
+ st.markdown("### Ecological Impact")
70
+ severity_color = {
71
+ "Low": "green",
72
+ "Medium": "orange",
73
+ "High": "red"
74
+ }
75
+ st.markdown(f"Severity: <span style='color: {severity_color[severity]}'>{severity}</span>",
76
+ unsafe_allow_html=True)
77
+
78
+ # Generate and display species information
79
+ st.markdown("### About This Species")
80
+ species_info = st.session_state.model.get_species_info(prediction)
81
+ st.markdown(species_info)
82
+
83
+ def compare_bugs():
84
+ col1, col2 = st.columns(2)
85
+
86
+ with col1:
87
+ file1 = st.file_uploader("Upload first bug photo", type=['png', 'jpg', 'jpeg'], key="compare1")
88
+ if file1:
89
+ image1 = Image.open(file1)
90
+ st.image(image1, caption="First Bug", use_column_width=True)
91
+
92
+ with col2:
93
+ file2 = st.file_uploader("Upload second bug photo", type=['png', 'jpg', 'jpeg'], key="compare2")
94
+ if file2:
95
+ image2 = Image.open(file2)
96
+ st.image(image2, caption="Second Bug", use_column_width=True)
97
+
98
+ if file1 and file2:
99
+ with st.spinner("Generating comparison..."):
100
+ # Get predictions for both images
101
+ pred1, conf1 = st.session_state.model.predict(image1)
102
+ pred2, conf2 = st.session_state.model.predict(image2)
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
+ with comp_col2:
118
+ st.markdown(f"**Species 2**: {pred2}")
119
+ st.markdown(f"Confidence: {conf2:.2f}%")
120
+ st.image(gradcam2, caption="Feature Highlights - Bug 2", use_column_width=True)
121
+
122
+ # Display comparison information
123
+ st.markdown("### Key Differences")
124
+ differences = st.session_state.model.compare_species(pred1, pred2)
125
+ st.markdown(differences)
126
+
127
+ if __name__ == "__main__":
128
+ main()