-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
50 lines (39 loc) · 1.63 KB
/
app.py
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
from flask import Flask, request, render_template
import numpy as np
import pickle
import sklearn
model = pickle.load(open('crop_recommendation.pkl', 'rb'))
sc = pickle.load(open('standardscaler.pkl', 'rb'))
mx = pickle.load(open('minmaxscaler.pkl', 'rb'))
app = Flask(__name__)
@app.route('/')
def index():
return render_template("index.html")
@app.route('/home')
def home():
return render_template("home.html")
@app.route("/predict", methods=['POST'])
def predict():
N = request.form['Nitrogen']
P = request.form['Phosporus']
K = request.form['Potassium']
temp = request.form['Temperature']
humidity = request.form['Humidity']
ph = request.form['pH']
rainfall = request.form['Rainfall']
feature_list = [N, P, K, temp, humidity, ph, rainfall]
single_pred = np.array(feature_list).reshape(1, -1)
mx_features = mx.transform(single_pred)
sc_mx_features = sc.transform(mx_features)
prediction = model.predict(sc_mx_features)
crop_dict = {
1: "Rice", 2: "Maize", 3: "Jute", 4: "Cotton", 5: "Coconut", 6: "Papaya", 7: "Orange",
8: "Apple", 9: "Muskmelon", 10: "Watermelon", 11: "Grapes", 12: "Mango", 13: "Banana",
14: "Pomegranate", 15: "Lentil", 16: "Blackgram", 17: "Mungbean", 18: "Mothbeans",
19: "Pigeonpeas", 20: "Kidneybeans", 21: "Chickpea", 22: "Coffee"
}
crop = crop_dict.get(prediction[0], "Unknown crop")
result = f"{crop} is the best crop to be cultivated right there" if crop != "Unknown crop" else "Could not determine the best crop."
return render_template('home.html', result=result)
if __name__ == "__main__":
app.run(debug=True)