Bug-O-Scope / app.py
dalybuilds's picture
Create app.py
117f15d verified
raw
history blame
4.68 kB
import streamlit as st
import torch
from PIL import Image
import io
import numpy as np
from model_utils import BugClassifier, generate_gradcam, get_severity_prediction
from transformers import AutoFeatureExtractor
# Page configuration
st.set_page_config(
page_title="Bug-O-Scope πŸ”πŸž",
page_icon="πŸ”",
layout="wide"
)
# Initialize session state
if 'model' not in st.session_state:
st.session_state.model = BugClassifier()
st.session_state.feature_extractor = AutoFeatureExtractor.from_pretrained("google/vit-base-patch16-224")
def main():
# Header
st.title("Bug-O-Scope πŸ”πŸž")
st.markdown("""
Welcome to Bug-O-Scope! Upload a picture of an insect to learn more about it.
This educational tool helps you identify bugs and understand their role in our ecosystem.
""")
# Sidebar
st.sidebar.header("About Bug-O-Scope")
st.sidebar.markdown("""
Bug-O-Scope is an AI-powered tool that helps you:
* πŸ” Identify insects from photos
* πŸ“š Learn about different species
* 🌍 Understand their ecological impact
* πŸ”¬ Compare different insects
""")
# Main content
tab1, tab2 = st.tabs(["Single Bug Analysis", "Bug Comparison"])
with tab1:
single_bug_analysis()
with tab2:
compare_bugs()
def single_bug_analysis():
uploaded_file = st.file_uploader("Upload a bug photo", type=['png', 'jpg', 'jpeg'], key="single")
if uploaded_file:
image = Image.open(uploaded_file)
col1, col2 = st.columns(2)
with col1:
st.image(image, caption="Uploaded Image", use_column_width=True)
with col2:
with st.spinner("Analyzing your bug..."):
# Get predictions
prediction, confidence = st.session_state.model.predict(image)
severity = get_severity_prediction(prediction)
st.success("Analysis Complete!")
st.markdown(f"### Identified Species")
st.markdown(f"**{prediction}**")
st.markdown(f"Confidence: {confidence:.2f}%")
st.markdown("### Ecological Impact")
severity_color = {
"Low": "green",
"Medium": "orange",
"High": "red"
}
st.markdown(f"Severity: <span style='color: {severity_color[severity]}'>{severity}</span>",
unsafe_allow_html=True)
# Generate and display species information
st.markdown("### About This Species")
species_info = st.session_state.model.get_species_info(prediction)
st.markdown(species_info)
def compare_bugs():
col1, col2 = st.columns(2)
with col1:
file1 = st.file_uploader("Upload first bug photo", type=['png', 'jpg', 'jpeg'], key="compare1")
if file1:
image1 = Image.open(file1)
st.image(image1, caption="First Bug", use_column_width=True)
with col2:
file2 = st.file_uploader("Upload second bug photo", type=['png', 'jpg', 'jpeg'], key="compare2")
if file2:
image2 = Image.open(file2)
st.image(image2, caption="Second Bug", use_column_width=True)
if file1 and file2:
with st.spinner("Generating comparison..."):
# Get predictions for both images
pred1, conf1 = st.session_state.model.predict(image1)
pred2, conf2 = st.session_state.model.predict(image2)
# Generate Grad-CAM visualizations
gradcam1 = generate_gradcam(image1, st.session_state.model)
gradcam2 = generate_gradcam(image2, st.session_state.model)
# Display results
st.markdown("### Comparison Results")
comp_col1, comp_col2 = st.columns(2)
with comp_col1:
st.markdown(f"**Species 1**: {pred1}")
st.markdown(f"Confidence: {conf1:.2f}%")
st.image(gradcam1, caption="Feature Highlights - Bug 1", use_column_width=True)
with comp_col2:
st.markdown(f"**Species 2**: {pred2}")
st.markdown(f"Confidence: {conf2:.2f}%")
st.image(gradcam2, caption="Feature Highlights - Bug 2", use_column_width=True)
# Display comparison information
st.markdown("### Key Differences")
differences = st.session_state.model.compare_species(pred1, pred2)
st.markdown(differences)
if __name__ == "__main__":
main()