Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -10,10 +10,11 @@ from groq import Groq
|
|
10 |
# Load environment variables
|
11 |
load_dotenv()
|
12 |
|
13 |
-
# Set up Streamlit
|
14 |
-
st.set_page_config(page_title="
|
15 |
-
st.
|
16 |
-
st.
|
|
|
17 |
|
18 |
# Initialize Groq client
|
19 |
try:
|
@@ -23,7 +24,7 @@ except Exception as e:
|
|
23 |
st.error(f"Failed to initialize Groq client: {str(e)}")
|
24 |
client = None
|
25 |
|
26 |
-
#
|
27 |
class PlantDiseaseModel(nn.Module):
|
28 |
def __init__(self, num_classes=28):
|
29 |
super(PlantDiseaseModel, self).__init__()
|
@@ -43,7 +44,6 @@ class PlantDiseaseModel(nn.Module):
|
|
43 |
x = self.classifier(x)
|
44 |
return x
|
45 |
|
46 |
-
# Load model
|
47 |
@st.cache_resource
|
48 |
def load_model():
|
49 |
model = PlantDiseaseModel()
|
@@ -52,7 +52,7 @@ def load_model():
|
|
52 |
|
53 |
model = load_model()
|
54 |
|
55 |
-
#
|
56 |
def preprocess_image(image):
|
57 |
transform = transforms.Compose([
|
58 |
transforms.Resize((256, 256)),
|
@@ -62,7 +62,7 @@ def preprocess_image(image):
|
|
62 |
])
|
63 |
return transform(image).unsqueeze(0)
|
64 |
|
65 |
-
# Disease
|
66 |
disease_classes = [
|
67 |
"Healthy", "Apple Scab", "Apple Black Rot", "Apple Cedar Rust",
|
68 |
"Cherry Powdery Mildew", "Corn Gray Leaf Spot", "Corn Common Rust",
|
@@ -75,7 +75,7 @@ disease_classes = [
|
|
75 |
"Tomato Target Spot", "Tomato Yellow Leaf Curl Virus", "Tomato Mosaic Virus"
|
76 |
]
|
77 |
|
78 |
-
#
|
79 |
def classify_disease(image):
|
80 |
try:
|
81 |
img_tensor = preprocess_image(image)
|
@@ -88,11 +88,11 @@ 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 {
|
95 |
-
"description": "API
|
96 |
}
|
97 |
|
98 |
try:
|
@@ -112,39 +112,37 @@ def get_disease_info(disease_name):
|
|
112 |
)
|
113 |
return {"description": response.choices[0].message.content}
|
114 |
except Exception as e:
|
115 |
-
st.error(f"Error fetching disease
|
116 |
return {
|
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=["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",
|
128 |
|
129 |
if st.button("π Predict Disease"):
|
130 |
-
with st.spinner("Analyzing
|
131 |
disease_name = classify_disease(image)
|
132 |
info = get_disease_info(disease_name)
|
133 |
|
|
|
134 |
st.subheader("π¬ Prediction Results")
|
135 |
-
|
|
|
136 |
with col1:
|
137 |
-
status = "Healthy" if disease_name.lower() == "healthy" else "Diseased"
|
138 |
st.markdown(f"**Status:** {status}")
|
139 |
-
st.markdown(f"**Detected Disease:** {disease_name}")
|
140 |
-
with col2:
|
141 |
-
if disease_name.lower() == "healthy":
|
142 |
-
st.success("β
Plant is Healthy")
|
143 |
-
else:
|
144 |
-
st.warning("β οΈ Disease Detected")
|
145 |
|
146 |
-
|
147 |
-
|
|
|
148 |
except Exception as e:
|
149 |
st.error(f"Error processing image: {str(e)}")
|
150 |
|
|
|
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
|
20 |
try:
|
|
|
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__()
|
|
|
44 |
x = self.classifier(x)
|
45 |
return x
|
46 |
|
|
|
47 |
@st.cache_resource
|
48 |
def load_model():
|
49 |
model = PlantDiseaseModel()
|
|
|
52 |
|
53 |
model = load_model()
|
54 |
|
55 |
+
# Preprocessing image
|
56 |
def preprocess_image(image):
|
57 |
transform = transforms.Compose([
|
58 |
transforms.Resize((256, 256)),
|
|
|
62 |
])
|
63 |
return transform(image).unsqueeze(0)
|
64 |
|
65 |
+
# Disease classes
|
66 |
disease_classes = [
|
67 |
"Healthy", "Apple Scab", "Apple Black Rot", "Apple Cedar Rust",
|
68 |
"Cherry Powdery Mildew", "Corn Gray Leaf Spot", "Corn Common Rust",
|
|
|
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 |
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 {
|
95 |
+
"description": "API not available. Check GROQ_API_KEY.",
|
96 |
}
|
97 |
|
98 |
try:
|
|
|
112 |
)
|
113 |
return {"description": response.choices[0].message.content}
|
114 |
except Exception as e:
|
115 |
+
st.error(f"Error fetching disease info: {str(e)}")
|
116 |
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=["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)
|
128 |
|
129 |
if st.button("π Predict Disease"):
|
130 |
+
with st.spinner("Analyzing..."):
|
131 |
disease_name = classify_disease(image)
|
132 |
info = get_disease_info(disease_name)
|
133 |
|
134 |
+
st.markdown("---")
|
135 |
st.subheader("π¬ Prediction Results")
|
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)}")
|
148 |
|