Rajkhanke007 commited on
Commit
e6d0842
·
verified ·
1 Parent(s): dcb0464

Upload 4 files

Browse files
Files changed (4) hide show
  1. app.py +81 -0
  2. rf_ferti_name.pkl +3 -0
  3. rf_ferti_value.pkl +3 -0
  4. templates/index.html +209 -0
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, render_template, request
2
+ import joblib
3
+ import pandas as pd
4
+ import google.generativeai as genai
5
+
6
+
7
+ app = Flask(__name__)
8
+
9
+ # Load the trained Random Forest models
10
+ rf_ferti_name = joblib.load('rf_ferti_name.pkl')
11
+ rf_ferti_value = joblib.load('rf_ferti_value.pkl')
12
+
13
+ # Manually define the encodings based on the provided dictionaries
14
+ soil_type_encodings = {'Black': 0, 'Clayey': 1, 'Loamy': 2, 'Red': 3, 'Sandy': 4}
15
+ crop_type_encodings = {'Barley': 0, 'Cotton': 1, 'Ground Nuts': 2, 'Maize': 3, 'Millets': 4,
16
+ 'Oil seeds': 5, 'Other Variety': 6, 'Paddy': 7, 'Pulses': 8, 'Sugarcane': 9,
17
+ 'Tobacco': 10, 'Wheat': 11}
18
+ fertilizer_name_encodings = {'10-26-26': 0, '14-35-14': 1, '15-15-15': 2, '17-17-17': 3, '20-20': 4,
19
+ '20-20-20': 5, '28-28': 6, 'Ammonium sulfate': 7, 'Biofertilizer (e.g., Rhizobium)': 8,
20
+ 'Calcium nitrate': 9, 'DAP': 10, 'Ferrous sulfate': 11, 'Magnesium sulfate': 12,
21
+ 'Potassium chloride/Muriate of potash (MOP)': 13, 'Potassium sulfate/Sulfate of potash (SOP)': 14,
22
+ 'Rock phosphate (RP)': 15, 'Single superphosphate (SSP)': 16, 'Triple superphosphate (TSP)': 17,
23
+ 'Urea': 18, 'Zinc sulfate': 19}
24
+
25
+
26
+ # AI configuration
27
+ genai.configure(api_key='AIzaSyBtXV2xJbrWVV57B5RWy_meKXOA59HFMeY')
28
+ model = genai.GenerativeModel("gemini-1.5-flash")
29
+
30
+ def generate_ai_suggestions(pred_fertilizer_name):
31
+ prompt = (
32
+ f"For {pred_fertilizer_name} fertlizer, generate 3-4 sentences each on a new line, note text shoudl be jsutidied should not contian anyu special character"
33
+ )
34
+ response = model.generate_content(prompt)
35
+ return response.text
36
+
37
+
38
+
39
+ @app.route('/', methods=['GET', 'POST'])
40
+ def index():
41
+ if request.method == 'POST':
42
+ # Retrieve form data
43
+ temperature = float(request.form['temperature'])
44
+ humidity = float(request.form['humidity'])
45
+ moisture = float(request.form['moisture'])
46
+ soil_type = request.form['soil_type']
47
+ crop_type = request.form['crop_type']
48
+ nitrogen = float(request.form['nitrogen'])
49
+ potassium = float(request.form['potassium'])
50
+ phosphorous = float(request.form['phosphorous'])
51
+
52
+ # Encode categorical data
53
+ soil_type_encoded = soil_type_encodings.get(soil_type, -1)
54
+ crop_type_encoded = crop_type_encodings.get(crop_type, -1)
55
+
56
+ # Create a DataFrame for the input
57
+ user_input = pd.DataFrame({
58
+ 'Temperature': [temperature],
59
+ 'Humidity': [humidity],
60
+ 'Moisture': [moisture],
61
+ 'Nitrogen': [nitrogen],
62
+ 'Potassium': [potassium],
63
+ 'Phosphorous': [phosphorous],
64
+ 'Soil Type': [soil_type_encoded],
65
+ 'Crop Type': [crop_type_encoded]
66
+ })
67
+
68
+ # Predict Fertilizer Name
69
+ pred_fertilizer_name = rf_ferti_name.predict(user_input)[0]
70
+ pred_fertilizer_name = [name for name, value in fertilizer_name_encodings.items() if value == pred_fertilizer_name][0]
71
+
72
+ # Predict Fertilizer Quantity
73
+ pred_fertilizer_qty = rf_ferti_value.predict(user_input)[0]
74
+ pred_info = generate_ai_suggestions(pred_fertilizer_name)
75
+
76
+ return render_template('index.html', prediction=True, fertilizer_name=pred_fertilizer_name,
77
+ fertilizer_qty=pred_fertilizer_qty, optimal_usage=pred_fertilizer_qty,pred_info=pred_info)
78
+ return render_template('index.html', prediction=False)
79
+
80
+ if __name__ == '__main__':
81
+ app.run(debug=True)
rf_ferti_name.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:cc6660f582c74c073743822c359cecff86469b6fd0b9df2a5b1c6d30d9d6f766
3
+ size 18260649
rf_ferti_value.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1cbcee671a228c55f243ad16b5d71bc76655bfea88d5a99bf8e5a0a738cb9468
3
+ size 45422609
templates/index.html ADDED
@@ -0,0 +1,209 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+
4
+ <head>
5
+ <meta charset="UTF-8">
6
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
7
+ <title>Fertilizer Recommender & Usage Requirement Estimator</title>
8
+ <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
9
+ <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
10
+ <style>
11
+ body {
12
+ background-color: #f0f8f0;
13
+ font-family: Arial, sans-serif;
14
+ }
15
+
16
+ .container {
17
+ margin-top: 50px;
18
+ }
19
+
20
+ .heading {
21
+ color: #228B22;
22
+ font-weight: bold;
23
+ text-align: center;
24
+ margin-bottom: 20px;
25
+ }
26
+
27
+ .input-container {
28
+ border: 2px solid #228B22;
29
+ border-radius: 10px;
30
+ padding: 20px;
31
+ background-color: #ffffff;
32
+ }
33
+
34
+ .result-container {
35
+ display: flex;
36
+ justify-content: space-between;
37
+ margin-top: 20px;
38
+ margin-bottom: 30px; /* Margin below the result container */
39
+ }
40
+
41
+ .left-container,
42
+ .right-container {
43
+ width: 48%;
44
+ border: 2px solid #228B22;
45
+ border-radius: 10px;
46
+ padding: 20px;
47
+ background-color: #ffffff;
48
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); /* Added shadow for enhancement */
49
+ }
50
+
51
+ .predict-btn {
52
+ background-color: #228B22;
53
+ color: #ffffff;
54
+ font-weight: bold;
55
+ }
56
+
57
+ .gauge-heading {
58
+ color: #228B22;
59
+ font-weight: bold;
60
+ text-align: center;
61
+ margin-bottom: 20px;
62
+ }
63
+
64
+ .fertilizer-name {
65
+ color: #000000;
66
+ font-weight: bold;
67
+ font-size: 24px;
68
+ text-align: center;
69
+ margin: 10px 0;
70
+ }
71
+
72
+ .fertilizer-image {
73
+ width: 80%; /* Reduced width for better fit */
74
+ height: auto;
75
+ border-radius: 10px;
76
+ display: block; /* Center the image */
77
+ margin: 0 auto; /* Center alignment */
78
+ }
79
+
80
+ .fertilizer-info {
81
+ margin-top: 15px;
82
+ font-size: 16px;
83
+ color: #333333;
84
+ text-align: left;
85
+ background-color: #f9f9f9;
86
+ padding: 15px;
87
+ border-radius: 8px;
88
+ box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); /* Light shadow for better readability */
89
+ }
90
+
91
+ .gauge-container {
92
+ display: flex;
93
+ justify-content: center; /* Center gauge */
94
+ }
95
+ </style>
96
+ </head>
97
+
98
+ <body>
99
+ <div class="container">
100
+ <h1 class="heading">Fertilizer Recommender & Usage Requirement Estimator</h1>
101
+ <form method="post" class="input-container">
102
+ <div class="form-row">
103
+ <div class="form-group col-md-6">
104
+ <label for="temperature">Enter Temperature (°C)</label>
105
+ <input type="number" class="form-control" id="temperature" name="temperature" required>
106
+ </div>
107
+ <div class="form-group col-md-6">
108
+ <label for="humidity">Enter Humidity %</label>
109
+ <input type="number" class="form-control" id="humidity" name="humidity" required>
110
+ </div>
111
+ </div>
112
+ <div class="form-row">
113
+ <div class="form-group col-md-6">
114
+ <label for="moisture">Enter Soil Moisture (%)</label>
115
+ <input type="number" class="form-control" id="moisture" name="moisture" required>
116
+ </div>
117
+ <div class="form-group col-md-6">
118
+ <label for="soil_type">Select Soil Type</label>
119
+ <select class="form-control" id="soil_type" name="soil_type" required>
120
+ <option>Black</option>
121
+ <option>Sandy</option>
122
+ <option>Loamy</option>
123
+ <option>Clayey</option>
124
+ <option>Red</option>
125
+ </select>
126
+ </div>
127
+ </div>
128
+ <div class="form-row">
129
+ <div class="form-group col-md-6">
130
+ <label for="crop_type">Select Crop Type</label>
131
+ <select class="form-control" id="crop_type" name="crop_type" required>
132
+ <option>Barley</option>
133
+ <option>Cotton</option>
134
+ <option>Ground Nuts</option>
135
+ <option>Maize</option>
136
+ <option>Millets</option>
137
+ <option>Oil seeds</option>
138
+ <option>Other Variety</option>
139
+ <option>Paddy</option>
140
+ <option>Pulses</option>
141
+ <option>Sugarcane</option>
142
+ <option>Tobacco</option>
143
+ <option>Wheat</option>
144
+ </select>
145
+ </div>
146
+ <div class="form-group col-md-6">
147
+ <label for="nitrogen">Enter Nitrogen(N)</label>
148
+ <input type="number" class="form-control" id="nitrogen" name="nitrogen" required>
149
+ </div>
150
+ </div>
151
+ <div class="form-row">
152
+ <div class="form-group col-md-6">
153
+ <label for="potassium">Enter Potassium(K)</label>
154
+ <input type="number" class="form-control" id="potassium" name="potassium" required>
155
+ </div>
156
+ <div class="form-group col-md-6">
157
+ <label for="phosphorous">Enter Phosphorous(P)</label>
158
+ <input type="number" class="form-control" id="phosphorous" name="phosphorous" required>
159
+ </div>
160
+ </div>
161
+ <button type="submit" class="btn predict-btn btn-block">Recommend Fertilizer</button>
162
+ </form>
163
+
164
+ {% if prediction %}
165
+ <div class="result-container">
166
+ <div class="left-container">
167
+ <h3 class="heading">Recommended Fertilizer</h3>
168
+ <div class="fertilizer-name">{{ fertilizer_name }}</div>
169
+ <div class="fertilizer-info">{{ pred_info }}</div> <!-- Added this block to display AI-generated info -->
170
+ </div>
171
+ <div class="right-container">
172
+ <h3 class="gauge-heading">Optimal Fertilizer Usage (kg/Acres)</h3>
173
+ <div class="gauge-container">
174
+ <div id="gauge" style="width:300px;height:300px;"></div> <!-- Reduced gauge size -->
175
+ </div>
176
+ <script>
177
+ var optimalUsage = {{ optimal_usage }};
178
+ var gaugeData = [{
179
+ type: 'indicator',
180
+ mode: 'gauge+number',
181
+ value: optimalUsage,
182
+ gauge: {
183
+ axis: { range: [0, 100], tickwidth: 2, tickcolor: "darkblue" },
184
+ bar: { color: "blue" },
185
+ bgcolor: "white",
186
+ bordercolor: "black",
187
+ borderwidth: 2,
188
+ steps: [
189
+ { range: [0, 50], color: "lightgray" },
190
+ { range: [50, 100], color: "gray" }
191
+ ],
192
+ }
193
+ }];
194
+
195
+ var layout = {
196
+ width: 370,
197
+ height: 250,
198
+ margin: { t: 20, b: 10, l: 3 }
199
+ };
200
+
201
+ Plotly.newPlot('gauge', gaugeData, layout);
202
+ </script>
203
+ </div>
204
+ </div>
205
+ {% endif %}
206
+ </div>
207
+ </body>
208
+
209
+ </html>