Spaces:
Sleeping
Sleeping
abhifdsdf commited on
Commit ·
137d29b
1
Parent(s): d14a71c
Add Flask app and model files for crop prediction API
Browse files- Dockerfile +12 -0
- app.py +46 -0
- crop_recommendation_model.pkl +3 -0
- requirements.txt +1 -0
- scaler.pkl +3 -0
Dockerfile
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.9-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
COPY requirements.txt .
|
| 6 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 7 |
+
|
| 8 |
+
COPY . .
|
| 9 |
+
|
| 10 |
+
EXPOSE 5000
|
| 11 |
+
|
| 12 |
+
CMD ["python", "app.py"]
|
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
import pickle
|
| 3 |
+
import numpy as np
|
| 4 |
+
from flask_cors import CORS
|
| 5 |
+
|
| 6 |
+
app = Flask(__name__)
|
| 7 |
+
CORS(app)
|
| 8 |
+
|
| 9 |
+
# Load the trained model and scaler
|
| 10 |
+
with open('crop_recommendation_model.pkl', 'rb') as model_file:
|
| 11 |
+
model = pickle.load(model_file)
|
| 12 |
+
|
| 13 |
+
with open('scaler.pkl', 'rb') as scaler_file:
|
| 14 |
+
scaler = pickle.load(scaler_file)
|
| 15 |
+
|
| 16 |
+
@app.route('/predict', methods=['POST'])
|
| 17 |
+
def predict():
|
| 18 |
+
try:
|
| 19 |
+
# Get input data from the request
|
| 20 |
+
data = request.get_json()
|
| 21 |
+
features = [
|
| 22 |
+
float(data['N']),
|
| 23 |
+
float(data['P']),
|
| 24 |
+
float(data['K']),
|
| 25 |
+
float(data['temperature']),
|
| 26 |
+
float(data['humidity']),
|
| 27 |
+
float(data['ph']),
|
| 28 |
+
float(data['rainfall'])
|
| 29 |
+
]
|
| 30 |
+
|
| 31 |
+
# Convert features to numpy array and reshape for prediction
|
| 32 |
+
features = np.array(features).reshape(1, -1)
|
| 33 |
+
|
| 34 |
+
# Scale the input features
|
| 35 |
+
scaled_features = scaler.transform(features)
|
| 36 |
+
|
| 37 |
+
# Make prediction
|
| 38 |
+
prediction = model.predict(scaled_features)
|
| 39 |
+
|
| 40 |
+
# Return raw model output
|
| 41 |
+
return jsonify({'prediction': prediction[0]})
|
| 42 |
+
except Exception as e:
|
| 43 |
+
return jsonify({'error': str(e)}), 400
|
| 44 |
+
|
| 45 |
+
if __name__ == '__main__':
|
| 46 |
+
app.run(debug=True)
|
crop_recommendation_model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:87c41241399ae3066cd2dcff2c59de98b9da702d407e691171e2e0781b80ca5f
|
| 3 |
+
size 3605778
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
echo -e "flask\nflask-cors\nscikit-learn\nnumpy" > requirements.txt
|
scaler.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:aade8e779aa81d1bc9ea25ed49f4392711081c6519198cf9a6d8868cef9ee51e
|
| 3 |
+
size 757
|