Spaces:
Sleeping
Sleeping
File size: 1,356 Bytes
137d29b 2841d7b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | 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)
@app.route('/predict', methods=['POST'])
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) |