-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
47 lines (36 loc) · 1.85 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
from flask import Flask,render_template,request
import pickle
import numpy as np
import pandas
import sklearn
import os
model_path = os.path.join(os.path.dirname(__file__), 'model.pkl')
with open(model_path, 'rb') as model_file:
model = pickle.load(model_file)
#creating a flask app
app = Flask(__name__) #This line creates an instance of the Flask class and assigns it to the variable app. This instance represents your Flask web application.
@app.route('/') #root route
def index():
return render_template('index.html')
@app.route("/predict",methods=['POST'])
def predict():
N = float(request.form['Nitrogen'])
P = float(request.form['Phosporus'])
K = float(request.form['Potassium'])
temp = float(request.form['Temperature'])
humid = float(request.form['Humidity'])
ph = float(request.form['Ph'])
rain = float(request.form['Rainfall'])
feature_list = [N, P, K, temp, humid, ph, rain]
features = np.array(feature_list).reshape(1, -1)
prediction = model.predict(features)
crop_dict={20: 'rice', 11: 'maize', 3: 'chickpea', 9: 'kidneybeans', 18: 'pigeonpeas', 13: 'mothbeans', 14: 'mungbean', 2: 'blackgram', 10: 'lentil', 19: 'pomegranate', 1: 'banana', 12: 'mango', 7: 'grapes', 21: 'watermelon', 15: 'muskmelon', 0: 'apple', 16: 'orange', 17: 'papaya', 4: 'coconut', 6: 'cotton', 8: 'jute', 5: 'coffee'}
if prediction[0] in crop_dict:
predicted_crop = crop_dict[prediction[0]]
result = "{} is the best crop for the cultivation".format(predicted_crop)
else:
result = "Please Check all the values or else Sorry we are not able to recommend the crop"
return render_template('index.html',result = result)
# python main
if __name__ == "__main__":
app.run(debug=True, port=7300) # here we use debug = true because if any error occurs then we are telling to show that error in browser itself