From bfab7a44bce1cc5e5ab49d2b50c44929be0c5b56 Mon Sep 17 00:00:00 2001 From: "Engr.Jalal Saleem" <2022EE275@student.uet.edu.pk> Date: Thu, 20 Feb 2025 22:24:41 +0000 Subject: [PATCH] updating files --- .gitignore | 1 + app.py | 37 ++++---- index.html | 237 ++++++++++++++++++++++++----------------------- requirements.txt | 4 + 4 files changed, 146 insertions(+), 133 deletions(-) create mode 100644 .gitignore create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4994c67 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +chromium \ No newline at end of file diff --git a/app.py b/app.py index d8f05ac..b8221c3 100644 --- a/app.py +++ b/app.py @@ -1,23 +1,24 @@ -from flask import Flask,request,render_template +from flask import Flask, request, render_template import numpy as np import pandas import sklearn import pickle -# importing model -model = pickle.load(open('model.pkl','rb')) -sc = pickle.load(open('standscaler.pkl','rb')) -ms = pickle.load(open('minmaxscaler.pkl','rb')) +# Create the Flask app instance with the template and static folder set to the project root +app = Flask(__name__, template_folder='.', static_folder='.') -# creating flask app -app = Flask(__name__) +# Importing the model and scalers +model = pickle.load(open('model.pkl', 'rb')) +sc = pickle.load(open('standscaler.pkl', 'rb')) +ms = pickle.load(open('minmaxscaler.pkl', 'rb')) @app.route('/') def index(): return render_template("index.html") -@app.route("/predict",methods=['POST']) +@app.route("/predict", methods=['POST']) def predict(): + # Retrieve form data N = request.form['Nitrogen'] P = request.form['Phosporus'] K = request.form['Potassium'] @@ -26,28 +27,30 @@ def predict(): ph = request.form['Ph'] rainfall = request.form['Rainfall'] + # Prepare features for prediction feature_list = [N, P, K, temp, humidity, ph, rainfall] single_pred = np.array(feature_list).reshape(1, -1) + # Scaling features scaled_features = ms.transform(single_pred) final_features = sc.transform(scaled_features) prediction = model.predict(final_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 recommendation based on prediction output + 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" + } if prediction[0] in crop_dict: crop = crop_dict[prediction[0]] result = "{} is the best crop to be cultivated right there".format(crop) else: result = "Sorry, we could not determine the best crop to be cultivated with the provided data." - return render_template('index.html',result = result) + + return render_template("index.html", result=result) - - - -# python main if __name__ == "__main__": app.run(debug=True) \ No newline at end of file diff --git a/index.html b/index.html index 78ecc5e..b929840 100644 --- a/index.html +++ b/index.html @@ -3,130 +3,135 @@
-