-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
267 lines (212 loc) · 10.8 KB
/
app.py
File metadata and controls
267 lines (212 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import os
import pickle
import warnings
import numpy as np
import pandas as pd
import seaborn as sns
import streamlit as st
import plotly.express as px
import matplotlib.pyplot as plt
import base64
st.set_page_config(page_title='Cropper System ● Gurmehar Singh ● CO21318', page_icon='🌱', layout='centered', initial_sidebar_state="collapsed")
def load_model(modelfile):
loaded_model = pickle.load(open(modelfile, 'rb'))
return loaded_model
# Function to encode the image
def get_base64_of_bin_file(bin_file):
with open(bin_file, 'rb') as f:
data = f.read()
return base64.b64encode(data).decode()
# Set background image from a local file
def set_background(png_file):
bin_str = get_base64_of_bin_file(png_file)
background_image = f"""
<style>
.stApp {{
background-image: url("data:image/png;base64,{bin_str}");
background-size: cover;
}}
/* Hide background image for mobile view */
@media (max-width: 768px) {{
.stApp {{
background-image: none;
background-color: black;
}}
}}
</style>
"""
st.markdown(background_image, unsafe_allow_html=True)
def main():
header_html = '''
<style>
/* Custom header styles */
.custom-header {
background-color: black; /* Dark background */
color: white;
text-align: center;
margin-top: -110px;
padding: 0px;
font-size: 40px;
# border-bottom: 2px solid white; /* Example color */
font-family: monospace;
}
</style>
<div class="custom-header">
🌾 Cropper System 🌾
</div>
'''
st.markdown(header_html, unsafe_allow_html=True)
hide_streamlit_style = '''
<style>
header {visibility: hidden;}
</style>
'''
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
set_background('crop.png')
# title_html = '''
# <div>
# <h1 style='color: MEDIUMSEAGREEN; text-align: left; margin-top: 0px;'>-Cropper System-</h1>
# <div/>
# '''
# st.markdown('''<br/>''', unsafe_allow_html=True)
# st.markdown(title_html, unsafe_allow_html=True)
file_path = 'Crop_recommendation.csv'
if not os.path.exists(file_path):
st.error(f"File not found: {file_path}")
return
dset = pd.read_csv(file_path)
menu = ['Crop Predictor', 'Dataset', 'Analysis', 'About']
choice = st.sidebar.selectbox("Menu", menu)
if choice == 'Dataset':
st.subheader('Dataset')
st.dataframe(dset.head(10))
elif choice == 'Analysis':
st.subheader('Data Analysis')
st.markdown(''' <div> <br/> <div/>''', unsafe_allow_html=True)
st.write("### Summary Statistics")
st.write(dset.describe())
st.markdown(''' <div> <br/> <div/>''', unsafe_allow_html=True)
st.write("### Correlation Matrix")
corr = dset.iloc[:, :-1].corr()
fig, ax = plt.subplots()
cmap_choice = st.selectbox("Choose a colormap for the correlation matrix",
['coolwarm', 'viridis', 'plasma', 'inferno', 'magma', 'cividis'])
sns.heatmap(corr, annot=True, ax=ax, cmap=cmap_choice)
st.pyplot(fig)
st.markdown(''' <div> <br/> <div/>''', unsafe_allow_html=True)
st.write("### Pair Plot - 1")
important_features = ['N', 'P', 'K']
fig2 = sns.pairplot(dset[important_features + ['label']], hue='label')
st.pyplot(fig2)
st.markdown(''' <div> <br/> <div/>''', unsafe_allow_html=True)
st.write("### Pair Plot - 2")
important_features_ = ['temperature', 'ph', 'rainfall']
fig2_ = sns.pairplot(dset[important_features_ + ['label']], hue='label')
st.pyplot(fig2_)
st.markdown(''' <div> <br/> <div/>''', unsafe_allow_html=True)
st.write("### Scatter Plot")
fig4 = px.bar(dset, x='humidity', y='label', color='label', title='Humidity vs. Label')
st.plotly_chart(fig4)
st.markdown(''' <div> <br/> <div/>''', unsafe_allow_html=True)
st.write("### Distribution of Each Feature")
for col in dset.columns[:-1]: # Exclude the label column
fig3, ax3 = plt.subplots()
sns.histplot(dset[col], kde=True, ax=ax3)
st.pyplot(fig3)
elif choice == 'Crop Predictor':
col1, col2 = st.columns([2, 2])
with col1:
with st.expander("ℹ️ Information", expanded=True):
st.write('''
Crop recommenders are integral to the success of precision agriculture, drawing on a diverse array of factors to offer targeted advice. Precision agriculture strives to uncover these factors for each unique site, fine-tuning crop selection decisions. Despite the advancements brought by this individualized approach, continuous monitoring of system outcomes remains critical. It's essential to acknowledge that precision agriculture systems vary widely.
In agriculture, the need for accurate and precise recommendations is paramount, as mistakes can result in significant resource and financial losses.
''')
with col2:
st.subheader('Discover the best crop to cultivate on your farm 👨🏻🌾')
N = st.number_input('NITROGEN', 1, 10000)
P = st.number_input('PHOSPHORUS', 1, 10000)
K = st.number_input('POTASSIUM', 1, 10000)
temp = st.number_input('TEMPERATURE', 0.0, 100000.0)
humidity = st.number_input('Humidity in %', 0.0, 100000.0)
ph = st.number_input("POTENZ HYDROGEN (Ph)", 0.0, 100000.0)
rainfall = st.number_input('RAINFALL in mm', 0.0, 100000.0)
feature_list = [N, P, K, temp, humidity, ph, rainfall]
single_pred = np.array(feature_list).reshape(1, -1)
if st.button('Predict'):
loaded_model = load_model('model.pkl')
prediction = loaded_model.predict(single_pred)
col1.write('''
## RESULTS 🔎
''')
col1.success(f'🌾 {prediction.item().title()} IS RECOMMENDED BY THE 1st BEST A.I. MODEL FOR YOUR FARM 🌾')
st.markdown('''<br/>''', unsafe_allow_html=True)
loaded_model2 = load_model('model_best_2nd.pkl')
prediction2 = loaded_model2.predict(single_pred)
col1.success(f'🌾 {prediction2.item().title()} IS RECOMMENDED BY THE 2nd BEST A.I. MODEL FOR YOUR FARM 🌾')
st.markdown('''<br/>''', unsafe_allow_html=True)
loaded_model3 = load_model('model_best_3rd.pkl')
prediction3 = loaded_model3.predict(single_pred)
col1.success(f'🌾 {prediction3.item().title()} IS RECOMMENDED BY THE 3rd BEST A.I. MODEL FOR YOUR FARM 🌾')
st.warning('NOTE: This A.I. application is for educational purposes only!')
hide_menu_style = '''
<style>
#MainMenu {
visibility: hidden;
}
</style>
'''
elif choice == 'About':
about_html = '''
<div>
<h3 style='color: white; text-align: left;'>About Cropper Sytem</h3>
<div/>
'''
st.markdown(about_html, unsafe_allow_html=True)
st.write('''
Welcome to the Cropper System, a cutting-edge AI-driven tool designed to assist farmers in making informed decisions about crop selection. Our system leverages advanced machine learning algorithms to analyze soil and environmental conditions, providing precise crop recommendations tailored to your farm's unique characteristics.
''')
about_html = '''
<div>
<h3 style='color: white; text-align: left;'>Key Features</h3>
<div/>
'''
st.markdown(about_html, unsafe_allow_html=True)
st.write('''
🌾 Exploratory Data Analysis (EDA): We perform thorough data cleaning, visualization, and statistical analysis to understand the underlying patterns in agricultural data.
🌾 Machine Learning Models: We employ models such as Logistic Regression, Decision Trees, Random Forest, and K-Nearest Neighbors (KNN) to predict the best crops for your land.
🌾 Seamless Integration: Using 'make_pipeline', we ensure efficient preprocessing, training, and evaluation of models, reducing data leakage and enhancing prediction accuracy.
🌾 User-Friendly Interface: Our platform is designed to be intuitive and accessible, allowing farmers to input key parameters and receive recommendations easily.
''')
about_html = '''
<div>
<h3 style='color: white; text-align: left;'>Mission</h3>
<div/>
'''
st.markdown(about_html, unsafe_allow_html=True)
st.write('''
At Cropper System, our mission is to empower farmers with the tools and knowledge they need to optimize crop production and sustainability. By integrating technology and agriculture, we aim to contribute to the advancement of precision farming and ensure food security.
''')
about_html = '''
<div>
<h3 style='color: white; text-align: left;'>Disclaimer</h3>
<div/>
'''
st.markdown(about_html, unsafe_allow_html=True)
st.write('''
Please note that while our recommendations are based on sophisticated algorithms and extensive data analysis, they are intended for educational purposes. We advise farmers to consider local conditions and expert advice before making final decisions.
Thank you for using Cropper System. We are committed to continuous improvement and welcome your feedback.
''')
st.markdown('''<br/>''', unsafe_allow_html=True)
st.write('''
🌾 © 2024 Cropper System • Created by Gurmehar Singh • CO21318 🌾
''')
hide_menu_style = '''
<style>
#MainMenu {
visibility: hidden;
}
</style>
'''
st.markdown(hide_menu_style, unsafe_allow_html=True)
if __name__ == '__main__':
main()