| from flask import Flask, request, jsonify
|
| import pickle
|
| import numpy as np
|
| from flask_cors import CORS
|
|
|
| app = Flask(__name__)
|
| CORS(app)
|
|
|
|
|
| with open('crop_recommendation_model.pkl', 'rb') as model_file:
|
| model = pickle.load(model_file)
|
|
|
| with open('scaler.pkl', 'rb') as scaler_file:
|
| scaler = pickle.load(scaler_file)
|
|
|
| @app.route('/predict', methods=['POST'])
|
| def predict():
|
| try:
|
|
|
| data = request.get_json()
|
| features = [
|
| float(data['N']),
|
| float(data['P']),
|
| float(data['K']),
|
| float(data['temperature']),
|
| float(data['humidity']),
|
| float(data['ph']),
|
| float(data['rainfall'])
|
| ]
|
|
|
|
|
| features = np.array(features).reshape(1, -1)
|
|
|
|
|
| scaled_features = scaler.transform(features)
|
|
|
|
|
| prediction = model.predict(scaled_features)
|
|
|
|
|
| return jsonify({'prediction': prediction[0]})
|
| except Exception as e:
|
| return jsonify({'error': str(e)}), 400
|
|
|
| if __name__ == '__main__':
|
| app.run(debug=True) |