sunbal7 commited on
Commit
876dc5b
Β·
verified Β·
1 Parent(s): 7d698a4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -10
app.py CHANGED
@@ -10,10 +10,10 @@ from groq import Groq
10
  # Load environment variables
11
  load_dotenv()
12
 
13
- # Set up the Streamlit page
14
  st.set_page_config(page_title="🌿 Leaf Disease Detector", layout="wide")
15
  st.markdown("<h1 style='text-align: center;'>🌿 Plant Leaf Disease Detection</h1>", unsafe_allow_html=True)
16
- st.markdown("<p style='text-align: center;'>Upload a leaf image to detect plant diseases and receive treatment advice.</p>", unsafe_allow_html=True)
17
  st.markdown("---")
18
 
19
  # Initialize Groq client
@@ -24,7 +24,7 @@ except Exception as e:
24
  st.error(f"Failed to initialize Groq client: {str(e)}")
25
  client = None
26
 
27
- # Define a simple CNN model (dummy for demonstration)
28
  class PlantDiseaseModel(nn.Module):
29
  def __init__(self, num_classes=28):
30
  super(PlantDiseaseModel, self).__init__()
@@ -52,7 +52,7 @@ def load_model():
52
 
53
  model = load_model()
54
 
55
- # Preprocessing image
56
  def preprocess_image(image):
57
  transform = transforms.Compose([
58
  transforms.Resize((256, 256)),
@@ -75,7 +75,7 @@ disease_classes = [
75
  "Tomato Target Spot", "Tomato Yellow Leaf Curl Virus", "Tomato Mosaic Virus"
76
  ]
77
 
78
- # Predict disease
79
  def classify_disease(image):
80
  try:
81
  img_tensor = preprocess_image(image)
@@ -88,7 +88,7 @@ def classify_disease(image):
88
  st.error(f"Error during classification: {str(e)}")
89
  return "Unknown"
90
 
91
- # Get disease info
92
  def get_disease_info(disease_name):
93
  if not client:
94
  return {
@@ -117,11 +117,16 @@ def get_disease_info(disease_name):
117
  "description": "Unable to fetch disease info. Please try again later.",
118
  }
119
 
120
- # Main App
121
  def main():
122
- uploaded_file = st.file_uploader("πŸ“· Upload a leaf image", type=["jpg", "jpeg", "png"])
123
 
124
  if uploaded_file:
 
 
 
 
 
125
  try:
126
  image = Image.open(uploaded_file).convert("RGB")
127
  st.image(image, caption="Uploaded Leaf Image", width=400)
@@ -136,12 +141,12 @@ def main():
136
 
137
  col1, col2 = st.columns([1, 2])
138
  with col1:
139
- status = "Healthy βœ…" if disease_name.lower() == "healthy" else "Diseased ⚠️"
140
  st.markdown(f"**Status:** {status}")
141
  st.markdown(f"**Detected Disease:** `{disease_name}`")
142
 
143
  with col2:
144
- st.markdown("**πŸ“‹ Details:**")
145
  st.markdown(info["description"])
146
  except Exception as e:
147
  st.error(f"Error processing image: {str(e)}")
 
10
  # Load environment variables
11
  load_dotenv()
12
 
13
+ # Page settings
14
  st.set_page_config(page_title="🌿 Leaf Disease Detector", layout="wide")
15
  st.markdown("<h1 style='text-align: center;'>🌿 Plant Leaf Disease Detection</h1>", unsafe_allow_html=True)
16
+ st.markdown("<p style='text-align: center;'>Upload a leaf image to detect plant diseases and get treatment guidance.</p>", unsafe_allow_html=True)
17
  st.markdown("---")
18
 
19
  # Initialize Groq client
 
24
  st.error(f"Failed to initialize Groq client: {str(e)}")
25
  client = None
26
 
27
+ # Dummy CNN model
28
  class PlantDiseaseModel(nn.Module):
29
  def __init__(self, num_classes=28):
30
  super(PlantDiseaseModel, self).__init__()
 
52
 
53
  model = load_model()
54
 
55
+ # Preprocessing
56
  def preprocess_image(image):
57
  transform = transforms.Compose([
58
  transforms.Resize((256, 256)),
 
75
  "Tomato Target Spot", "Tomato Yellow Leaf Curl Virus", "Tomato Mosaic Virus"
76
  ]
77
 
78
+ # Predict class
79
  def classify_disease(image):
80
  try:
81
  img_tensor = preprocess_image(image)
 
88
  st.error(f"Error during classification: {str(e)}")
89
  return "Unknown"
90
 
91
+ # Fetch disease info
92
  def get_disease_info(disease_name):
93
  if not client:
94
  return {
 
117
  "description": "Unable to fetch disease info. Please try again later.",
118
  }
119
 
120
+ # Main app
121
  def main():
122
+ uploaded_file = st.file_uploader("πŸ“· Upload a leaf image", type=["jpeg", "png", "jpg"])
123
 
124
  if uploaded_file:
125
+ filename = uploaded_file.name.lower()
126
+ if not (filename.endswith(".jpg") or filename.endswith(".jpeg") or filename.endswith(".png")):
127
+ st.error("Only JPG, JPEG, and PNG files are allowed.")
128
+ st.stop()
129
+
130
  try:
131
  image = Image.open(uploaded_file).convert("RGB")
132
  st.image(image, caption="Uploaded Leaf Image", width=400)
 
141
 
142
  col1, col2 = st.columns([1, 2])
143
  with col1:
144
+ status = "βœ… Healthy" if disease_name.lower() == "healthy" else "⚠️ Diseased"
145
  st.markdown(f"**Status:** {status}")
146
  st.markdown(f"**Detected Disease:** `{disease_name}`")
147
 
148
  with col2:
149
+ st.markdown("**πŸ“‹ Disease Info:**")
150
  st.markdown(info["description"])
151
  except Exception as e:
152
  st.error(f"Error processing image: {str(e)}")