Spaces:
Sleeping
Sleeping
| from flask import Flask, request, jsonify | |
| import pickle | |
| import numpy as np | |
| from flask_cors import CORS | |
| app = Flask(__name__) | |
| CORS(app) | |
| # Load the trained model and scaler | |
| 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) | |
| def predict(): | |
| try: | |
| # Get input data from the request | |
| 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']) | |
| ] | |
| # Convert features to numpy array and reshape for prediction | |
| features = np.array(features).reshape(1, -1) | |
| # Scale the input features | |
| scaled_features = scaler.transform(features) | |
| # Make prediction | |
| prediction = model.predict(scaled_features) | |
| # Return raw model output | |
| return jsonify({'prediction': prediction[0]}) | |
| except Exception as e: | |
| return jsonify({'error': str(e)}), 400 | |
| if __name__ == '__main__': | |
| import os | |
| port = int(os.environ.get('PORT', 7860)) | |
| app.run(host='0.0.0.0', port=port, debug=True) |