Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -10,10 +10,10 @@ from groq import Groq
|
|
10 |
# Load environment variables
|
11 |
load_dotenv()
|
12 |
|
13 |
-
#
|
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
|
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 |
-
#
|
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
|
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
|
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 |
-
#
|
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
|
121 |
def main():
|
122 |
-
uploaded_file = st.file_uploader("π· Upload a leaf image", type=["
|
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
|
140 |
st.markdown(f"**Status:** {status}")
|
141 |
st.markdown(f"**Detected Disease:** `{disease_name}`")
|
142 |
|
143 |
with col2:
|
144 |
-
st.markdown("**π
|
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)}")
|