Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -10,7 +10,7 @@ from groq import Groq
|
|
10 |
# Load environment variables
|
11 |
load_dotenv()
|
12 |
|
13 |
-
# Streamlit
|
14 |
st.set_page_config(page_title="Leaves Disease Detection", layout="wide")
|
15 |
st.title("πΏ Leaves Disease Detection")
|
16 |
st.write("Upload an image of a plant leaf to check for diseases and get treatment recommendations.")
|
@@ -23,7 +23,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__()
|
@@ -36,14 +36,14 @@ class PlantDiseaseModel(nn.Module):
|
|
36 |
nn.Linear(128 * 32 * 32, 512), nn.ReLU(), nn.Dropout(0.5),
|
37 |
nn.Linear(512, num_classes)
|
38 |
)
|
39 |
-
|
40 |
def forward(self, x):
|
41 |
x = self.features(x)
|
42 |
x = x.view(x.size(0), -1)
|
43 |
x = self.classifier(x)
|
44 |
return x
|
45 |
|
46 |
-
#
|
47 |
@st.cache_resource
|
48 |
def load_model():
|
49 |
model = PlantDiseaseModel()
|
@@ -62,7 +62,7 @@ def preprocess_image(image):
|
|
62 |
])
|
63 |
return transform(image).unsqueeze(0)
|
64 |
|
65 |
-
#
|
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 |
-
# Classify
|
79 |
def classify_disease(image):
|
80 |
try:
|
81 |
img_tensor = preprocess_image(image)
|
@@ -88,16 +88,17 @@ 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 {
|
95 |
"description": "API connection not available. Please check your GROQ_API_KEY.",
|
96 |
}
|
|
|
97 |
try:
|
98 |
if disease_name.lower() == "healthy":
|
99 |
return {
|
100 |
-
"description": "The plant appears to be healthy. No treatment needed.",
|
101 |
}
|
102 |
|
103 |
response = client.chat.completions.create(
|
@@ -105,7 +106,7 @@ def get_disease_info(disease_name):
|
|
105 |
{"role": "system", "content": "You are a plant pathologist assistant."},
|
106 |
{"role": "user", "content": f"Describe {disease_name} in plants including symptoms, treatment, and prevention."}
|
107 |
],
|
108 |
-
model="
|
109 |
temperature=0.3,
|
110 |
max_tokens=1024
|
111 |
)
|
@@ -116,14 +117,14 @@ def get_disease_info(disease_name):
|
|
116 |
"description": "Unable to fetch disease info. Please try again later.",
|
117 |
}
|
118 |
|
119 |
-
# Main
|
120 |
def main():
|
121 |
uploaded_file = st.file_uploader("Upload a leaf image", type=["jpg", "jpeg", "png"])
|
122 |
-
|
123 |
if uploaded_file:
|
124 |
try:
|
125 |
image = Image.open(uploaded_file).convert("RGB")
|
126 |
-
st.image(image, caption="Uploaded Leaf Image",
|
127 |
|
128 |
if st.button("π Predict Disease"):
|
129 |
with st.spinner("Analyzing the leaf..."):
|
|
|
10 |
# Load environment variables
|
11 |
load_dotenv()
|
12 |
|
13 |
+
# Set up Streamlit app
|
14 |
st.set_page_config(page_title="Leaves Disease Detection", layout="wide")
|
15 |
st.title("πΏ Leaves Disease Detection")
|
16 |
st.write("Upload an image of a plant leaf to check for diseases and get treatment recommendations.")
|
|
|
23 |
st.error(f"Failed to initialize Groq client: {str(e)}")
|
24 |
client = None
|
25 |
|
26 |
+
# Dummy CNN model definition
|
27 |
class PlantDiseaseModel(nn.Module):
|
28 |
def __init__(self, num_classes=28):
|
29 |
super(PlantDiseaseModel, self).__init__()
|
|
|
36 |
nn.Linear(128 * 32 * 32, 512), nn.ReLU(), nn.Dropout(0.5),
|
37 |
nn.Linear(512, num_classes)
|
38 |
)
|
39 |
+
|
40 |
def forward(self, x):
|
41 |
x = self.features(x)
|
42 |
x = x.view(x.size(0), -1)
|
43 |
x = self.classifier(x)
|
44 |
return x
|
45 |
|
46 |
+
# Load model
|
47 |
@st.cache_resource
|
48 |
def load_model():
|
49 |
model = PlantDiseaseModel()
|
|
|
62 |
])
|
63 |
return transform(image).unsqueeze(0)
|
64 |
|
65 |
+
# Disease labels
|
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 |
+
# Classify image
|
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 from Groq
|
92 |
def get_disease_info(disease_name):
|
93 |
if not client:
|
94 |
return {
|
95 |
"description": "API connection not available. Please check your GROQ_API_KEY.",
|
96 |
}
|
97 |
+
|
98 |
try:
|
99 |
if disease_name.lower() == "healthy":
|
100 |
return {
|
101 |
+
"description": "The plant appears to be healthy. No treatment is needed.",
|
102 |
}
|
103 |
|
104 |
response = client.chat.completions.create(
|
|
|
106 |
{"role": "system", "content": "You are a plant pathologist assistant."},
|
107 |
{"role": "user", "content": f"Describe {disease_name} in plants including symptoms, treatment, and prevention."}
|
108 |
],
|
109 |
+
model="llama-3.3-70b-versatile",
|
110 |
temperature=0.3,
|
111 |
max_tokens=1024
|
112 |
)
|
|
|
117 |
"description": "Unable to fetch disease info. Please try again later.",
|
118 |
}
|
119 |
|
120 |
+
# Main UI
|
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", use_container_width=True)
|
128 |
|
129 |
if st.button("π Predict Disease"):
|
130 |
with st.spinner("Analyzing the leaf..."):
|