BalajiM's picture
Upload app.py
1fc233f verified
Raw
History Blame Contribute Delete
1.91 kB
import streamlit as st
from transformers import pipeline
from PIL import Image
# 1. Setup the Page UI
st.set_page_config(page_title="Nano Botanic Nursery - Disease Classifier", page_icon="🌿")
st.title("🌿 Smart Nursery: Plant Health Monitor")
st.write("Upload a clear photo of a leaf (e.g., samanthi, roses, or standard crops) and our AI will detect early signs of disease.")
# 2. Load the Model (Cached to prevent reloading on every click)
@st.cache_resource
def load_disease_classifier():
# We use a pipeline specifically configured for the plant disease detection model
return pipeline("image-classification", model="Diginsa/Plant-Disease-Detection-Project")
classifier = load_disease_classifier()
# 3. Image Upload Interface
uploaded_file = st.file_uploader("Upload Leaf Image (JPG/PNG)", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
# Display the uploaded image
image = Image.open(uploaded_file)
st.image(image, caption="Uploaded Leaf", use_column_width=True)
# 4. Trigger the AI Inference
if st.button("Analyze Leaf"):
with st.spinner("Analyzing biological structures..."):
# Run the Hugging Face pipeline
results = classifier(image)
# Parse and display the primary diagnosis
st.subheader("Diagnosis:")
top_result = results[0]
label = top_result['label'].replace('_', ' ') # Clean up the technical label
confidence = top_result['score'] * 100
st.success(f"**{label}** ({confidence:.1f}% confidence)")
# Show secondary possibilities for transparency
with st.expander("See detailed probability breakdown"):
for res in results[1:4]:
clean_label = res['label'].replace('_', ' ')
st.write(f"- {clean_label}: {res['score']*100:.1f}%")