HarmanpreetKaur commited on
Commit
3065e85
·
verified ·
1 Parent(s): d921041

Upload 5 files

Browse files
PlantDiseaseDetection.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
app.py ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ from PIL import Image
4
+ import numpy as np
5
+ import tensorflow as tf
6
+ import streamlit as st
7
+ import matplotlib.pyplot as plt
8
+ from groq import Groq
9
+
10
+ # Set page configuration
11
+ st.set_page_config(page_title="Plant Disease Classifier", page_icon="🌿", layout="wide")
12
+
13
+ # Get the directory of the current file
14
+ working_dir = os.path.dirname(os.path.abspath(__file__))
15
+
16
+ # Use os.path.join for cross-platform compatibility
17
+ model_path = os.path.join(working_dir, 'plant_disease_prediction_model.h5')
18
+ class_indices_path = os.path.join(working_dir, 'class_indices.json')
19
+
20
+ # Load the pre-trained model
21
+ model = tf.keras.models.load_model(model_path)
22
+
23
+ # Loading the class names
24
+ with open(class_indices_path, 'r') as f:
25
+ class_indices = json.load(f)
26
+
27
+ # Function to Load and Preprocess the Image using Pillow
28
+ def load_and_preprocess_image(image_path, target_size=(224, 224)):
29
+ img = Image.open(image_path)
30
+ img = img.resize(target_size)
31
+ img_array = np.array(img)
32
+ img_array = np.expand_dims(img_array, axis=0)
33
+ img_array = img_array.astype('float32') / 255.0
34
+ return img_array
35
+
36
+ # Function to Predict the Class of an Image
37
+ def predict_image_class(model, image_path, class_indices):
38
+ preprocessed_img = load_and_preprocess_image(image_path)
39
+ predictions = model.predict(preprocessed_img)
40
+ predicted_class_index = np.argmax(predictions, axis=1)[0]
41
+ predicted_class_name = class_indices[str(predicted_class_index)]
42
+ return predicted_class_name # Only returning the name of the disease
43
+
44
+ # Function to Get Solution from LLM
45
+ def get_solution_from_llm(disease_name):
46
+ client = Groq(api_key="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")
47
+ prompt = f"The plant disease detected is {disease_name}. Provide detailed steps to mitigate the disease and improve plant health."
48
+
49
+ completion = client.chat.completions.create(
50
+ model="llama-3.1-70b-versatile",
51
+ messages=[{"role": "system", "content": "You are a helpful assistant."},
52
+ {"role": "user", "content": prompt}],
53
+ temperature=1,
54
+ max_tokens=1024,
55
+ top_p=1,
56
+ stream=False,
57
+ stop=None,
58
+ )
59
+
60
+ solution = completion.choices[0].message.content
61
+ return solution
62
+
63
+ # Sidebar Content
64
+ st.sidebar.title("🌿 About the App")
65
+ st.sidebar.write("This app uses AI to classify plant diseases and provide solutions.")
66
+ st.sidebar.subheader("How It Works:")
67
+ st.sidebar.write("""
68
+ 1. Upload a clear image of a plant leaf.
69
+ 2. The model predicts the disease using a pre-trained AI.
70
+ 3. You get detailed mitigation solutions powered by LLMs.
71
+ """)
72
+ st.sidebar.markdown("**Technologies Used:**")
73
+ st.sidebar.write("- TensorFlow\n- PIL\n- Groq API\n- Streamlit")
74
+ st.sidebar.markdown("**Project By:** Harman")
75
+
76
+ # Main Page
77
+ st.markdown("<h1 style='text-align: center; color: green;'>🌱 Plant Disease Classifier</h1>", unsafe_allow_html=True)
78
+
79
+ uploaded_image = st.file_uploader("Upload a leaf image (jpg, jpeg, png)", type=["jpg", "jpeg", "png"])
80
+
81
+ if uploaded_image:
82
+ # Display uploaded image (resized for smaller display)
83
+ image = Image.open(uploaded_image)
84
+ col1, col2 = st.columns(2, gap="large")
85
+
86
+ with col1:
87
+ st.markdown("<h3 style='text-align: center;'>Uploaded Image</h3>", unsafe_allow_html=True)
88
+ resized_img = image.resize((200, 200)) # Resized to 200x200 for smaller display
89
+ st.image(resized_img, use_column_width=True)
90
+
91
+ with col2:
92
+ st.markdown("<h3 style='text-align: center;'>Prediction</h3>", unsafe_allow_html=True)
93
+ if st.button("Classify"):
94
+ with st.spinner("Analyzing the image..."):
95
+ prediction = predict_image_class(model, uploaded_image, class_indices) # Only name of the disease
96
+ st.success(f"**Disease Detected:** {prediction}")
97
+
98
+ st.markdown("---")
99
+ st.markdown("<h3>Solution for the Problem:</h3>", unsafe_allow_html=True)
100
+ solution = get_solution_from_llm(prediction)
101
+ st.write(solution)
102
+
103
+ # Toggle Solution Visibility
104
+ with st.expander("Want to learn more? Click here!"):
105
+ st.markdown(solution)
106
+
107
+ # Download Button
108
+ st.download_button(
109
+ label="Download Solution as Text",
110
+ data=solution,
111
+ file_name=f"{prediction}_solution.txt",
112
+ mime="text/plain"
113
+ )
114
+ else:
115
+ st.warning("Please upload an image to proceed.")
116
+
117
+ # Footer
118
+ st.markdown("<hr>", unsafe_allow_html=True)
119
+ st.markdown(
120
+ "<p style='text-align: center; font-size: 14px;'>Made with ❤️ by Harman</p>",
121
+ unsafe_allow_html=True
122
+ )
class_indices.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"0": "Apple___Apple_scab", "1": "Apple___Black_rot", "2": "Apple___Cedar_apple_rust", "3": "Apple___healthy", "4": "Blueberry___healthy", "5": "Cherry_(including_sour)___Powdery_mildew", "6": "Cherry_(including_sour)___healthy", "7": "Corn_(maize)___Cercospora_leaf_spot Gray_leaf_spot", "8": "Corn_(maize)___Common_rust_", "9": "Corn_(maize)___Northern_Leaf_Blight", "10": "Corn_(maize)___healthy", "11": "Grape___Black_rot", "12": "Grape___Esca_(Black_Measles)", "13": "Grape___Leaf_blight_(Isariopsis_Leaf_Spot)", "14": "Grape___healthy", "15": "Orange___Haunglongbing_(Citrus_greening)", "16": "Peach___Bacterial_spot", "17": "Peach___healthy", "18": "Pepper,_bell___Bacterial_spot", "19": "Pepper,_bell___healthy", "20": "Potato___Early_blight", "21": "Potato___Late_blight", "22": "Potato___healthy", "23": "Raspberry___healthy", "24": "Soybean___healthy", "25": "Squash___Powdery_mildew", "26": "Strawberry___Leaf_scorch", "27": "Strawberry___healthy", "28": "Tomato___Bacterial_spot", "29": "Tomato___Early_blight", "30": "Tomato___Late_blight", "31": "Tomato___Leaf_Mold", "32": "Tomato___Septoria_leaf_spot", "33": "Tomato___Spider_mites Two-spotted_spider_mite", "34": "Tomato___Target_Spot", "35": "Tomato___Tomato_Yellow_Leaf_Curl_Virus", "36": "Tomato___Tomato_mosaic_virus", "37": "Tomato___healthy"}
plant_disease_prediction_model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b0d196c3af57c9db66072e031922fedef1a0980f2ff5d859ec203f72a31f0646
3
+ size 573706416
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ streamlit
2
+ kaggle
3
+ pandas
4
+ scikit-learn
5
+ tensorflow
6
+ groq
7
+ matplotlib