Sadashiv commited on
Commit
50e17b2
1 Parent(s): 8293c81

crop recommendation complete

Browse files
Files changed (7) hide show
  1. app.py +38 -1
  2. artifacts.py +29 -0
  3. config.py +6 -0
  4. main.py +0 -25
  5. requirements.txt +4 -1
  6. templates/crop_recommendation_input.html +1 -1
  7. utils.py +1 -1
app.py CHANGED
@@ -1,5 +1,10 @@
 
 
1
  from flask import Flask, request, render_template, jsonify
2
  import requests
 
 
 
3
 
4
  app = Flask(__name__)
5
 
@@ -8,10 +13,42 @@ app = Flask(__name__)
8
  def home():
9
  return render_template('index.html')
10
 
11
- @app.route('/crop_recommendation')
12
  def crop_recommendation():
13
  return render_template('crop_recommendation_input.html')
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  @app.route('/fertilizer_recommendation')
16
  def fertilizer_recommendation():
17
  return render_template('fertilizer_recommendation_input.html')
 
1
+ from config import crop_model, crop_pipeline_encoder, crop_label_encoder
2
+ from utils import retrieve_image_by_name_from_mongodb, retrieve_data
3
  from flask import Flask, request, render_template, jsonify
4
  import requests
5
+ import os
6
+ import numpy as np
7
+ import base64
8
 
9
  app = Flask(__name__)
10
 
 
13
  def home():
14
  return render_template('index.html')
15
 
16
+ @app.route('/crop_recommendation', methods=['GET', 'POST'])
17
  def crop_recommendation():
18
  return render_template('crop_recommendation_input.html')
19
 
20
+ @app.route("/crop_recommendation_output", methods=['GET', 'POST'])
21
+ def crop_recommendation_output():
22
+ temperature = request.form.get("temperature")
23
+ humidity = request.form.get("humidity")
24
+ ph = request.form.get("ph")
25
+ nitrogen = request.form.get("nitrogen")
26
+ potassium = request.form.get("potassium")
27
+ phosphorous = request.form.get("phosphorous")
28
+ rain_fall = request.form.get("rain_fall")
29
+
30
+ input_list = [nitrogen, phosphorous, potassium, temperature, humidity, ph, rain_fall]
31
+ input_array = np.array(input_list).reshape(-1, 7).astype(int)
32
+
33
+ transformed_data = crop_pipeline_encoder.transform(input_array)
34
+ model_prediction = crop_model.predict(transformed_data).astype(int)
35
+
36
+ label = crop_label_encoder.inverse_transform(model_prediction)
37
+
38
+ # retrieving the image from mongodb dabase
39
+ image_data = retrieve_image_by_name_from_mongodb(database_name=os.getenv("CROP_DB_NAME"),
40
+ collection_name=os.getenv("CROP_IMAGE_COLLECTION_NAME"),
41
+ file_name=str(label[0]))
42
+
43
+ # encoding the byte data recieved from the mongodb
44
+ image_data_base64 = base64.b64encode(image_data).decode('utf-8')
45
+
46
+ # retrieving text data from mongodb
47
+ crop_details = retrieve_data(database_name=os.getenv("CROP_DB_NAME"), collection_name= os.getenv("CROP_INFO_COLLECTION_NAME"), search_query=label[0])
48
+
49
+ return render_template('crop_recommendation_output.html', image_data_base64=image_data_base64, input_file_name=label[0], crop_details=crop_details)
50
+
51
+
52
  @app.route('/fertilizer_recommendation')
53
  def fertilizer_recommendation():
54
  return render_template('fertilizer_recommendation_input.html')
artifacts.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ MODEL_NAME = "model.pkl"
4
+ TARGET_ENCODER_OBJECT_NAME = "target_encoder.pkl"
5
+ TRANSFORMER_OJBCET_NAME = "transformer.pkl"
6
+
7
+ crop_recommendation_artifacts_path = "./crop-recommendation/saved_models"
8
+ fertilizer_recommendation_artifacts_path = "./Fertilizer-Recommendation/saved_models"
9
+
10
+
11
+ ## crop recommendation artifacts
12
+ latest_crop_recommendation_artifacts = max(os.listdir(crop_recommendation_artifacts_path)) #0, 1, 2
13
+
14
+ latest_crop_recommendation_artifacts_path = os.path.join(crop_recommendation_artifacts_path, latest_crop_recommendation_artifacts)
15
+
16
+ crop_model_path = os.path.join(latest_crop_recommendation_artifacts_path, 'model', MODEL_NAME)
17
+ crop_transformer_path = os.path.join(latest_crop_recommendation_artifacts_path,'transformer', TRANSFORMER_OJBCET_NAME)
18
+ crop_target_encoder_path = os.path.join(latest_crop_recommendation_artifacts_path, 'target_encoder', TARGET_ENCODER_OBJECT_NAME)
19
+
20
+
21
+ ## fertilizer recommendation artifacts
22
+ latest_fertilizer_recommendation_artifacts = max(os.listdir(fertilizer_recommendation_artifacts_path)) #0, 1, 2
23
+
24
+ latest_fertilizer_recommendation_artifacts_path = os.path.join(fertilizer_recommendation_artifacts_path, latest_fertilizer_recommendation_artifacts)
25
+
26
+ fertilizer_model_path = os.path.join(latest_fertilizer_recommendation_artifacts_path, 'model', MODEL_NAME)
27
+ fertilizer_transformer_path = os.path.join(latest_fertilizer_recommendation_artifacts_path,'transformer', TRANSFORMER_OJBCET_NAME)
28
+ fertilizer_target_encoder_path = os.path.join(latest_fertilizer_recommendation_artifacts_path, 'target_encoder', TARGET_ENCODER_OBJECT_NAME)
29
+
config.py ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ from artifacts import crop_model_path, crop_transformer_path, crop_target_encoder_path
2
+ from utils import load_model_and_encoders
3
+
4
+ crop_model, crop_pipeline_encoder, crop_label_encoder = load_model_and_encoders(model_path=crop_model_path,
5
+ transformer_path=crop_transformer_path,
6
+ target_encoder_path=crop_target_encoder_path)
main.py DELETED
@@ -1,25 +0,0 @@
1
- import pymongo
2
-
3
- # Provide the mongodb localhost url to connect python to mongodb.
4
- client = pymongo.MongoClient("mongodb://localhost:27017/neurolabDB")
5
-
6
- # Database Name
7
- dataBase = client["neurolabDB"]
8
-
9
- # Collection Name
10
- collection = dataBase['Products']
11
-
12
- # Sample data
13
- d = {'companyName': 'iNeuron',
14
- 'product': 'Affordable AI',
15
- 'courseOffered': 'Machine Learning with Deployment'}
16
-
17
- # Insert above records in the collection
18
- rec = collection.insert_one(d)
19
-
20
- # Lets Verify all the record at once present in the record with all the fields
21
- all_record = collection.find()
22
-
23
- # Printing all records present in the collection
24
- for idx, record in enumerate(all_record):
25
- print(f"{idx}: {record}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
requirements.txt CHANGED
@@ -1,4 +1,7 @@
1
  pymongo
2
  flask
3
  requests
4
- numpy
 
 
 
 
1
  pymongo
2
  flask
3
  requests
4
+ numpy
5
+ python-dotenv
6
+ scikit-learn
7
+ dill
templates/crop_recommendation_input.html CHANGED
@@ -8,7 +8,7 @@
8
  <div
9
  class="container recommendation-input-container d-flex justify-content-center"
10
  >
11
- <form class="row g-3" method="GET" action="/crop_recommendation_output">
12
  <h3 class="text-center">AI-Powered Crop Recommendations</h3>
13
  <div class="col-md-4" name="temperature">
14
  <label for="temperature" class="form-label">Temperature</label>
 
8
  <div
9
  class="container recommendation-input-container d-flex justify-content-center"
10
  >
11
+ <form class="row g-3" method="POST" action="/crop_recommendation_output">
12
  <h3 class="text-center">AI-Powered Crop Recommendations</h3>
13
  <div class="col-md-4" name="temperature">
14
  <label for="temperature" class="form-label">Temperature</label>
utils.py CHANGED
@@ -53,4 +53,4 @@ def retrieve_data(database_name, collection_name, search_query):
53
  result = collection.find_one(search_query)
54
 
55
  client.close()
56
- return result['crop_info']
 
53
  result = collection.find_one(search_query)
54
 
55
  client.close()
56
+ return result['data_info']