Update app.py
Browse files
app.py
CHANGED
|
@@ -1,67 +1,68 @@
|
|
| 1 |
-
import numpy as np
|
| 2 |
-
from flask import Flask, request, jsonify, render_template
|
| 3 |
-
import joblib
|
| 4 |
-
|
| 5 |
-
# Initialize Flask app
|
| 6 |
-
app = Flask(__name__)
|
| 7 |
-
|
| 8 |
-
# Load the trained Random Forest model
|
| 9 |
-
rf_model = joblib.load('random_forest_model.pkl')
|
| 10 |
-
|
| 11 |
-
# Encoder classes
|
| 12 |
-
soil_type_classes = ['Alluvial Soil', 'Black Soil', 'Clay Soil', 'Red Soil']
|
| 13 |
-
crop_classes = ['All vegetables Tea Coffee Rubber Coconut Cashew Avocado',
|
| 14 |
-
'Cotton Blackgram Oilseeds Pigeonpea',
|
| 15 |
-
'Cotton Jowar Pigeonpea Blackgram',
|
| 16 |
-
'Cotton Rice Pigeonpea Blackgram Sunflower',
|
| 17 |
-
'Cotton Sorghum CerealCrops Blackgram',
|
| 18 |
-
'Cotton Sugarcane Pigeonpea Sorghum',
|
| 19 |
-
'Pearlmillet Basil Blackgram Sorghum',
|
| 20 |
-
'Pearlmillet Maize Pigeonpea Greengram Garlic',
|
| 21 |
-
'Pearlmillet Ragi Groundnut Potato All vegetables',
|
| 22 |
-
'Soybean Pigeonpea Millets Greengram',
|
| 23 |
-
'Soybean Pigeonpea Maize Sorghum']
|
| 24 |
-
|
| 25 |
-
# Route for the home page
|
| 26 |
-
@app.route('/')
|
| 27 |
-
def index():
|
| 28 |
-
return render_template('index.html', soil_types=soil_type_classes)
|
| 29 |
-
|
| 30 |
-
# Route to handle the prediction
|
| 31 |
-
@app.route('/predict', methods=['POST'])
|
| 32 |
-
def predict():
|
| 33 |
-
try:
|
| 34 |
-
# Get form data
|
| 35 |
-
soil_type = request.form.get('soil_type')
|
| 36 |
-
soil_depth = float(request.form.get('soil_depth'))
|
| 37 |
-
ph = float(request.form.get('ph'))
|
| 38 |
-
bulk_density = float(request.form.get('bulk_density'))
|
| 39 |
-
ec = float(request.form.get('ec'))
|
| 40 |
-
organic_carbon = float(request.form.get('organic_carbon'))
|
| 41 |
-
soil_moisture_retention = float(request.form.get('soil_moisture_retention'))
|
| 42 |
-
available_water_capacity = float(request.form.get('available_water_capacity'))
|
| 43 |
-
infiltration_rate = float(request.form.get('infiltration_rate'))
|
| 44 |
-
clay_percentage = float(request.form.get('clay_percentage'))
|
| 45 |
-
|
| 46 |
-
# Encode soil type
|
| 47 |
-
soil_type_encoded = soil_type_classes.index(soil_type)
|
| 48 |
-
|
| 49 |
-
# Create feature array
|
| 50 |
-
features = np.array([[soil_type_encoded, soil_depth, ph, bulk_density, ec, organic_carbon,
|
| 51 |
-
soil_moisture_retention, available_water_capacity, infiltration_rate, clay_percentage]])
|
| 52 |
-
|
| 53 |
-
# Make prediction
|
| 54 |
-
predicted_crop_index = rf_model.predict(features)[0]
|
| 55 |
-
predicted_crop = crop_classes[predicted_crop_index]
|
| 56 |
-
|
| 57 |
-
# Split the crops into separate columns (based on spaces)
|
| 58 |
-
predicted_crop_list = predicted_crop.split()
|
| 59 |
-
|
| 60 |
-
# Return the predicted crops as a list to the front-end
|
| 61 |
-
return jsonify({'predicted_crop_list': predicted_crop_list})
|
| 62 |
-
|
| 63 |
-
except Exception as e:
|
| 64 |
-
return jsonify({'error': str(e)})
|
| 65 |
-
|
| 66 |
-
if __name__ == '__main__':
|
| 67 |
-
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
from flask import Flask, request, jsonify, render_template
|
| 3 |
+
import joblib
|
| 4 |
+
|
| 5 |
+
# Initialize Flask app
|
| 6 |
+
app = Flask(__name__)
|
| 7 |
+
|
| 8 |
+
# Load the trained Random Forest model
|
| 9 |
+
rf_model = joblib.load('random_forest_model.pkl')
|
| 10 |
+
|
| 11 |
+
# Encoder classes
|
| 12 |
+
soil_type_classes = ['Alluvial Soil', 'Black Soil', 'Clay Soil', 'Red Soil']
|
| 13 |
+
crop_classes = ['All vegetables Tea Coffee Rubber Coconut Cashew Avocado',
|
| 14 |
+
'Cotton Blackgram Oilseeds Pigeonpea',
|
| 15 |
+
'Cotton Jowar Pigeonpea Blackgram',
|
| 16 |
+
'Cotton Rice Pigeonpea Blackgram Sunflower',
|
| 17 |
+
'Cotton Sorghum CerealCrops Blackgram',
|
| 18 |
+
'Cotton Sugarcane Pigeonpea Sorghum',
|
| 19 |
+
'Pearlmillet Basil Blackgram Sorghum',
|
| 20 |
+
'Pearlmillet Maize Pigeonpea Greengram Garlic',
|
| 21 |
+
'Pearlmillet Ragi Groundnut Potato All vegetables',
|
| 22 |
+
'Soybean Pigeonpea Millets Greengram',
|
| 23 |
+
'Soybean Pigeonpea Maize Sorghum']
|
| 24 |
+
|
| 25 |
+
# Route for the home page
|
| 26 |
+
@app.route('/')
|
| 27 |
+
def index():
|
| 28 |
+
return render_template('index.html', soil_types=soil_type_classes)
|
| 29 |
+
|
| 30 |
+
# Route to handle the prediction
|
| 31 |
+
@app.route('/predict', methods=['POST'])
|
| 32 |
+
def predict():
|
| 33 |
+
try:
|
| 34 |
+
# Get form data
|
| 35 |
+
soil_type = request.form.get('soil_type')
|
| 36 |
+
soil_depth = float(request.form.get('soil_depth'))
|
| 37 |
+
ph = float(request.form.get('ph'))
|
| 38 |
+
bulk_density = float(request.form.get('bulk_density'))
|
| 39 |
+
ec = float(request.form.get('ec'))
|
| 40 |
+
organic_carbon = float(request.form.get('organic_carbon'))
|
| 41 |
+
soil_moisture_retention = float(request.form.get('soil_moisture_retention'))
|
| 42 |
+
available_water_capacity = float(request.form.get('available_water_capacity'))
|
| 43 |
+
infiltration_rate = float(request.form.get('infiltration_rate'))
|
| 44 |
+
clay_percentage = float(request.form.get('clay_percentage'))
|
| 45 |
+
|
| 46 |
+
# Encode soil type
|
| 47 |
+
soil_type_encoded = soil_type_classes.index(soil_type)
|
| 48 |
+
|
| 49 |
+
# Create feature array
|
| 50 |
+
features = np.array([[soil_type_encoded, soil_depth, ph, bulk_density, ec, organic_carbon,
|
| 51 |
+
soil_moisture_retention, available_water_capacity, infiltration_rate, clay_percentage]])
|
| 52 |
+
|
| 53 |
+
# Make prediction
|
| 54 |
+
predicted_crop_index = rf_model.predict(features)[0]
|
| 55 |
+
predicted_crop = crop_classes[predicted_crop_index]
|
| 56 |
+
|
| 57 |
+
# Split the crops into separate columns (based on spaces)
|
| 58 |
+
predicted_crop_list = predicted_crop.split()
|
| 59 |
+
|
| 60 |
+
# Return the predicted crops as a list to the front-end
|
| 61 |
+
return jsonify({'predicted_crop_list': predicted_crop_list})
|
| 62 |
+
|
| 63 |
+
except Exception as e:
|
| 64 |
+
return jsonify({'error': str(e)})
|
| 65 |
+
|
| 66 |
+
if __name__ == '__main__':
|
| 67 |
+
port = int(os.environ.get("PORT", 7860))
|
| 68 |
+
app.run(host="0.0.0.0", port=port)
|