| | import numpy as np |
| | from flask import Flask, request, jsonify, render_template |
| | import joblib |
| | import os |
| |
|
| | |
| | app = Flask(__name__) |
| |
|
| | |
| | rf_model = joblib.load('random_forest_model.pkl') |
| |
|
| | |
| | soil_type_classes = ['Alluvial Soil', 'Black Soil', 'Clay Soil', 'Red Soil'] |
| | crop_classes = ['All vegetables Tea Coffee Rubber Coconut Cashew Avocado', |
| | 'Cotton Blackgram Oilseeds Pigeonpea', |
| | 'Cotton Jowar Pigeonpea Blackgram', |
| | 'Cotton Rice Pigeonpea Blackgram Sunflower', |
| | 'Cotton Sorghum CerealCrops Blackgram', |
| | 'Cotton Sugarcane Pigeonpea Sorghum', |
| | 'Pearlmillet Basil Blackgram Sorghum', |
| | 'Pearlmillet Maize Pigeonpea Greengram Garlic', |
| | 'Pearlmillet Ragi Groundnut Potato All vegetables', |
| | 'Soybean Pigeonpea Millets Greengram', |
| | 'Soybean Pigeonpea Maize Sorghum'] |
| |
|
| | |
| | @app.route('/') |
| | def index(): |
| | return render_template('index.html', soil_types=soil_type_classes) |
| |
|
| | |
| | @app.route('/predict', methods=['POST']) |
| | def predict(): |
| | try: |
| | |
| | soil_type = request.form.get('soil_type') |
| | soil_depth = float(request.form.get('soil_depth')) |
| | ph = float(request.form.get('ph')) |
| | bulk_density = float(request.form.get('bulk_density')) |
| | ec = float(request.form.get('ec')) |
| | organic_carbon = float(request.form.get('organic_carbon')) |
| | soil_moisture_retention = float(request.form.get('soil_moisture_retention')) |
| | available_water_capacity = float(request.form.get('available_water_capacity')) |
| | infiltration_rate = float(request.form.get('infiltration_rate')) |
| | clay_percentage = float(request.form.get('clay_percentage')) |
| |
|
| | |
| | soil_type_encoded = soil_type_classes.index(soil_type) |
| |
|
| | |
| | features = np.array([[soil_type_encoded, soil_depth, ph, bulk_density, ec, organic_carbon, |
| | soil_moisture_retention, available_water_capacity, infiltration_rate, clay_percentage]]) |
| |
|
| | |
| | predicted_crop_index = rf_model.predict(features)[0] |
| | predicted_crop = crop_classes[predicted_crop_index] |
| |
|
| | |
| | predicted_crop_list = predicted_crop.split() |
| |
|
| | |
| | return jsonify({'predicted_crop_list': predicted_crop_list}) |
| |
|
| | except Exception as e: |
| | return jsonify({'error': str(e)}) |
| |
|
| | if __name__ == '__main__': |
| | port = int(os.environ.get("PORT", 7860)) |
| | app.run(host="0.0.0.0", port=port) |
| |
|