CoolFace
Apppublic

Vikrant26/Farmers_help

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py56 linesDownload Raw Back to root
1
2from flask import Flask, request, render_template
3import numpy as np
4import pandas as pd
5import sklearn
6import pickle
7
8# importing model
9model = pickle.load(open('modelcr.pkl','rb'))
10sc = pickle.load(open('standscalercr.pkl','rb'))
11ms = pickle.load(open('minmaxscalercr.pkl','rb'))
12
13# creating flask app
14app = Flask(__name__, template_folder="template")
15
16
17@app.route('/')
18def index():
19    return render_template("index.html")
20
21@app.route("/predict",methods=['POST'])
22def predict():
23    N = request.form['Nitrogen']
24    P = request.form['Phosporus']
25    K = request.form['Potassium']
26    temp = request.form['Temperature']
27    humidity = request.form['Humidity']
28    ph = request.form['Ph']
29    rainfall = request.form['Rainfall']
30
31    feature_list = [N, P, K, temp, humidity, ph, rainfall]
32    single_pred = np.array(feature_list).reshape(1, -1)
33
34    scaled_features = ms.transform(single_pred)
35    final_features = sc.transform(scaled_features)
36    prediction = model.predict(final_features)
37
38    crop_dict = {1: "Rice", 2: "Maize", 3: "Jute", 4: "Cotton", 5: "Coconut", 6: "Papaya", 7: "Orange",
39                 8: "Apple", 9: "Muskmelon", 10: "Watermelon", 11: "Grapes", 12: "Mango", 13: "Banana",
40                 14: "Pomegranate", 15: "Lentil", 16: "Blackgram", 17: "Mungbean", 18: "Mothbeans",
41                 19: "Pigeonpeas", 20: "Kidneybeans", 21: "Chickpea", 22: "Coffee"}
42
43    if prediction[0] in crop_dict:
44        crop = crop_dict[prediction[0]]
45        result = "{} is the best crop to be cultivated right there".format(crop)
46    else:
47        result = "Sorry, we could not determine the best crop to be cultivated with the provided data."
48    return render_template('index.html',result = result)
49
50
51
52
53# python main
54if __name__ == "__main__":
55    app.run(debug=True)
56