Delete src
Browse files- src/app.py +0 -238
src/app.py
DELETED
@@ -1,238 +0,0 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import joblib
|
3 |
-
import numpy as np
|
4 |
-
import matplotlib.pyplot as plt
|
5 |
-
import matplotlib.colors as mcolors
|
6 |
-
|
7 |
-
st.sidebar.title("Crop & Fertilizer Recommendation System for Sustainable Agriculture")
|
8 |
-
app_mode = st.sidebar.radio("Select Page",["HOME","CROP RECOMMENDATION","FERTILIZER RECOMMENDATION"])
|
9 |
-
|
10 |
-
from PIL import Image
|
11 |
-
img = Image.open("Diseases.png")
|
12 |
-
|
13 |
-
st.image(img)
|
14 |
-
|
15 |
-
if(app_mode=="HOME"):
|
16 |
-
st.markdown("<h1 style='text-align: center;'>Crop & Fertilizer Recommendation System for Sustainable Agriculture", unsafe_allow_html=True)
|
17 |
-
|
18 |
-
elif(app_mode=="CROP RECOMMENDATION"):
|
19 |
-
st.header("Crop Recommendation System")
|
20 |
-
dtc = joblib.load("crop_prediction_model.pkl")
|
21 |
-
scaler = joblib.load("crop_scaler.pkl")
|
22 |
-
crop_dict = {1: 'rice', 2: 'maize', 3: 'chickpea', 4: 'kidneybeans', 5: 'pigeonpeas',
|
23 |
-
6: 'mothbeans', 7: 'mungbean', 8: 'blackgram', 9: 'lentil', 10: 'pomegranate',
|
24 |
-
11: 'banana', 12: 'mango', 13: 'grapes', 14: 'watermelon', 15: 'muskmelon',
|
25 |
-
16: 'apple', 17: 'orange', 18: 'papaya', 19: 'coconut', 20: 'cotton',
|
26 |
-
21: 'jute', 22: 'coffee'}
|
27 |
-
N = st.number_input("Nitrogen (N)", min_value=0.0,value=0.0)
|
28 |
-
P = st.number_input("Phosphorus (P)",min_value=0.0, value=0.0)
|
29 |
-
K = st.number_input("Potassium (K)", min_value=0.0, value=0.0)
|
30 |
-
temp = st.number_input("Temperature (°C)", value=0.0)
|
31 |
-
hum = st.number_input("Humidity (%)",min_value=0.0, max_value=100.0,value=0.0)
|
32 |
-
ph = st.number_input("pH Level", min_value=0.0, max_value=14.0, value=0.0)
|
33 |
-
rain = st.number_input("Rainfall (mm)",min_value=0.0, max_value=1200.00, value=0.0)
|
34 |
-
def crop_rec(N, P, K, temp, hum, ph, rain):
|
35 |
-
features = np.array([[N, P, K, temp, hum, ph, rain]])
|
36 |
-
transformed_features = scaler.transform(features)
|
37 |
-
prediction = dtc.predict(transformed_features)
|
38 |
-
crop = crop_dict.get(prediction[0], "Unknown Crop")
|
39 |
-
return f"{crop} is the recommended crop for the given conditions."
|
40 |
-
if st.button("Predict"):
|
41 |
-
col1, col2 = st.columns(2)
|
42 |
-
with col1:
|
43 |
-
labels = ['Nitrogen (N)', 'Phosphorus (P)', 'Potassium (K)']
|
44 |
-
sizes = [N, P, K]
|
45 |
-
colors = ['#ff9999', '#66b3ff', '#99ff99']
|
46 |
-
|
47 |
-
def custom_autopct(pct, allsizes):
|
48 |
-
total = sum(allsizes)
|
49 |
-
absolute = round(pct / 100. * total, 1)
|
50 |
-
closest_value = min(allsizes, key=lambda x: abs(x - absolute))
|
51 |
-
return f' {closest_value} units ({pct:.1f}%)'
|
52 |
-
|
53 |
-
fig, ax = plt.subplots(figsize=(6, 6), dpi=100)
|
54 |
-
|
55 |
-
ax.pie(sizes, labels=labels, colors=colors,
|
56 |
-
autopct=lambda pct: custom_autopct(pct, sizes),
|
57 |
-
startangle=90, wedgeprops=dict(width=0.3))
|
58 |
-
plt.title('NPK Composition', size=20, color='blue', fontweight='bold')
|
59 |
-
plt.axis('equal')
|
60 |
-
st.pyplot(fig)
|
61 |
-
|
62 |
-
rfall = (rain / 1200) * 100
|
63 |
-
sizes = [rfall, 100 - rfall] # Example values
|
64 |
-
colors = ['#66b3ff', '#D3D3D3']
|
65 |
-
fig, ax = plt.subplots()
|
66 |
-
ax.pie(sizes, colors=colors,
|
67 |
-
startangle=90) # Edge color for visibility
|
68 |
-
plt.title(f'Rain Fall : {rain} mm')
|
69 |
-
plt.axis('equal')
|
70 |
-
st.pyplot(fig)
|
71 |
-
|
72 |
-
with col2:
|
73 |
-
sizes = [hum, 100 - hum] # Example values
|
74 |
-
colors = ['#4169E1', '#D3D3D3']
|
75 |
-
fig, ax = plt.subplots()
|
76 |
-
ax.pie(sizes, colors=colors,
|
77 |
-
startangle=90) # Edge color for visibility
|
78 |
-
plt.title(f'Humidity : {hum} %')
|
79 |
-
plt.axis('equal')
|
80 |
-
st.pyplot(fig)
|
81 |
-
|
82 |
-
|
83 |
-
sizes = [temp, 100-temp] # Example values
|
84 |
-
colors = ['#FFA500', '#D3D3D3']
|
85 |
-
fig, ax = plt.subplots()
|
86 |
-
ax.pie(sizes, colors=colors,
|
87 |
-
startangle=90) # Edge color for visibility
|
88 |
-
plt.title(f'Temperature : {temp}°C')
|
89 |
-
plt.axis('equal')
|
90 |
-
st.pyplot(fig)
|
91 |
-
|
92 |
-
ph_values = np.linspace(0, 14, 100)
|
93 |
-
|
94 |
-
# Define a colormap for pH scale
|
95 |
-
colors = [
|
96 |
-
(1, 0, 0), # Red (Strong Acid, pH 0)
|
97 |
-
(1, 0.5, 0), # Orange
|
98 |
-
(1, 1, 0), # Yellow
|
99 |
-
(0, 1, 0), # Green (Neutral, pH 7)
|
100 |
-
(0, 0, 1), # Blue (Weak Base)
|
101 |
-
(0.5, 0, 1) # Purple (Strong Base, pH 14)
|
102 |
-
]
|
103 |
-
cmap = mcolors.LinearSegmentedColormap.from_list("pH Scale", colors, N=100)
|
104 |
-
|
105 |
-
# Create a figure
|
106 |
-
fig, ax = plt.subplots(figsize=(10, 1))
|
107 |
-
|
108 |
-
# Create a gradient color bar
|
109 |
-
gradient = np.linspace(0, 1, 100).reshape(1, -1)
|
110 |
-
ax.imshow(gradient, aspect="auto", cmap=cmap, extent=[0, 14, 0, 1])
|
111 |
-
|
112 |
-
# Set x-axis labels for pH values
|
113 |
-
ax.set_xticks(np.arange(0, 15, 1))
|
114 |
-
ax.set_xticklabels(np.arange(0, 15, 1))
|
115 |
-
ax.set_yticks([]) # Hide y-axis
|
116 |
-
|
117 |
-
# Mark a specific pH point
|
118 |
-
ph_point = ph # Change this to mark another pH value
|
119 |
-
ax.scatter(ph_point, 0.5, color="black", s=100, label=f'pH {ph_point}')
|
120 |
-
ax.vlines(ph_point, 0, 1, color="black", linestyle="dashed", linewidth=1)
|
121 |
-
|
122 |
-
# Add title and legend
|
123 |
-
ax.set_title("pH Scale Visualization", fontsize=12)
|
124 |
-
ax.legend(loc="upper right")
|
125 |
-
|
126 |
-
st.pyplot(fig)
|
127 |
-
|
128 |
-
|
129 |
-
result = crop_rec(N, P, K, temp, hum, ph, rain)
|
130 |
-
st.success(result)
|
131 |
-
|
132 |
-
elif(app_mode=="FERTILIZER RECOMMENDATION"):
|
133 |
-
st.header("Fertilizer Recommendation System")
|
134 |
-
# Load the model and scaler
|
135 |
-
model = joblib.load('fertilizer_prediction_model.pkl')
|
136 |
-
sc = joblib.load('fertilizer_scaler.pkl')
|
137 |
-
|
138 |
-
# Fertilizer dictionary
|
139 |
-
fert_dict = {1: 'Urea', 2: 'DAP', 3: '14-35-14', 4: '28-28', 5: '17-17-17', 6: '20-20', 7: '10-26-26'}
|
140 |
-
|
141 |
-
# Soil type dictionary
|
142 |
-
soil_type_dict = {0: 'Black', 1: 'Clayey', 2: 'Loamy', 3: 'Red', 4:'Sandy'}
|
143 |
-
|
144 |
-
# Crop type dictionary
|
145 |
-
crop_type_dict = {
|
146 |
-
0: "Barley",
|
147 |
-
1: "Cotton",
|
148 |
-
2: "Ground Nuts",
|
149 |
-
3: "Maize",
|
150 |
-
4: "Millets",
|
151 |
-
5: "Oil seeds",
|
152 |
-
6: "Paddy",
|
153 |
-
7: "Pulses",
|
154 |
-
8: "Sugarcane",
|
155 |
-
9: "Tobacco",
|
156 |
-
10: "Wheat"
|
157 |
-
}
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
# Function to recommend fertilizer
|
162 |
-
def recommend_fertilizer(Temparature, Humidity, Moisture, Soil_Type, Crop_Type, Nitrogen, Potassium, Phosphorous):
|
163 |
-
features = np.array([[Temparature, Humidity, Moisture, Soil_Type, Crop_Type, Nitrogen, Potassium, Phosphorous]])
|
164 |
-
transformed_features = sc.transform(features)
|
165 |
-
prediction = model.predict(transformed_features).reshape(1, -1)
|
166 |
-
fertilizer = [fert_dict[i] for i in prediction[0]]
|
167 |
-
return f"{fertilizer[0]} is the best fertilizer for the given conditions"
|
168 |
-
|
169 |
-
|
170 |
-
# Streamlit app
|
171 |
-
Temparature = st.number_input('Temperature', min_value=0.0, max_value=100.0, value=0.0)
|
172 |
-
Humidity = st.number_input('Humidity', min_value=0.0, max_value=100.0, value=0.0)
|
173 |
-
Moisture = st.number_input('Moisture', min_value=0.0, max_value=100.0, value=0.0)
|
174 |
-
Soil_Type = st.selectbox('Soil Type', options=list(soil_type_dict.values()))
|
175 |
-
Crop_Type = st.selectbox('Crop Type', options=list(crop_type_dict.values()))
|
176 |
-
Nitrogen = st.number_input('Nitrogen', min_value=0.0, max_value=100.0, value=0.0)
|
177 |
-
Potassium = st.number_input('Potassium', min_value=0.0, max_value=100.0, value=0.0)
|
178 |
-
Phosphorous = st.number_input('Phosphorous', min_value=0.0, max_value=100.0, value=0.0)
|
179 |
-
|
180 |
-
if st.button('Recommend Fertilizer'):
|
181 |
-
col1, col2 = st.columns(2)
|
182 |
-
|
183 |
-
with col1:
|
184 |
-
labels = ['Nitrogen (N)', 'Phosphorus (P)', 'Potassium (K)']
|
185 |
-
sizes = [Nitrogen, Potassium, Phosphorous]
|
186 |
-
colors = ['#ff9999', '#66b3ff', '#99ff99']
|
187 |
-
|
188 |
-
def custom_autopct(pct, allsizes):
|
189 |
-
total = sum(allsizes)
|
190 |
-
absolute = round(pct / 100. * total, 1)
|
191 |
-
closest_value = min(allsizes, key=lambda x: abs(x - absolute))
|
192 |
-
return f' {closest_value} units ({pct:.1f}%)'
|
193 |
-
|
194 |
-
fig, ax = plt.subplots(figsize=(6, 6), dpi=100)
|
195 |
-
|
196 |
-
ax.pie(sizes, labels=labels, colors=colors,
|
197 |
-
autopct=lambda pct: custom_autopct(pct, sizes),
|
198 |
-
startangle=90, wedgeprops=dict(width=0.3))
|
199 |
-
plt.title('NPK Composition', size=20, color='blue', fontweight='bold')
|
200 |
-
plt.axis('equal')
|
201 |
-
st.pyplot(fig)
|
202 |
-
|
203 |
-
sizes = [Humidity, 100 - Humidity] # Example values
|
204 |
-
colors = ['#4169E1', '#D3D3D3']
|
205 |
-
fig, ax = plt.subplots()
|
206 |
-
ax.pie(sizes, colors=colors,
|
207 |
-
startangle=90) # Edge color for visibility
|
208 |
-
plt.title(f'Humidity : {Humidity} %')
|
209 |
-
plt.axis('equal')
|
210 |
-
st.pyplot(fig)
|
211 |
-
|
212 |
-
with col2:
|
213 |
-
sizes = [Moisture, 100 - Moisture] # Example values
|
214 |
-
colors = ['#4169E1', '#D3D3D3']
|
215 |
-
fig, ax = plt.subplots()
|
216 |
-
ax.pie(sizes, colors=colors,
|
217 |
-
startangle=90) # Edge color for visibility
|
218 |
-
plt.title(f'Moisture : {Moisture} %')
|
219 |
-
plt.axis('equal')
|
220 |
-
st.pyplot(fig)
|
221 |
-
|
222 |
-
sizes = [Temparature, 100 - Temparature] # Example values
|
223 |
-
colors = ['#FFA500', '#D3D3D3']
|
224 |
-
fig, ax = plt.subplots()
|
225 |
-
ax.pie(sizes, colors=colors,
|
226 |
-
startangle=90) # Edge color for visibility
|
227 |
-
plt.title(f'Temperature : {Temparature}°C')
|
228 |
-
plt.axis('equal')
|
229 |
-
st.pyplot(fig)
|
230 |
-
|
231 |
-
|
232 |
-
# Convert soil and crop types to their numerical values
|
233 |
-
Soil_Type_num = list(soil_type_dict.keys())[list(soil_type_dict.values()).index(Soil_Type)]
|
234 |
-
Crop_Type_num = list(crop_type_dict.keys())[list(crop_type_dict.values()).index(Crop_Type)]
|
235 |
-
|
236 |
-
result = recommend_fertilizer(Temparature, Humidity, Moisture, Soil_Type_num, Crop_Type_num, Nitrogen,
|
237 |
-
Potassium, Phosphorous)
|
238 |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