Otabek Najimov commited on
Commit
4474009
·
1 Parent(s): abc651d
Files changed (5) hide show
  1. Dockerfile +12 -0
  2. app.py +57 -0
  3. crop_model.pkl +3 -0
  4. label_encoder.pkl +3 -0
  5. requirements.txt +7 -0
Dockerfile ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9
2
+
3
+ WORKDIR /code
4
+
5
+ COPY ./requirements.txt /code/requirements.txt
6
+ COPY ./app.py /code/app.py
7
+ COPY ./crop_model.pkl /code/crop_model.pkl
8
+ COPY ./label_encoder.pkl /code/label_encoder.pkl
9
+
10
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
11
+
12
+ CMD ["gunicorn", "--bind", "0.0.0.0:7860", "app:app"]
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ from flask import Flask, request, jsonify
3
+ from flask_cors import CORS
4
+ from sklearn.preprocessing import LabelEncoder
5
+ from xgboost import XGBClassifier
6
+ import joblib
7
+
8
+ app = Flask(__name__)
9
+
10
+ CORS(app)
11
+
12
+ # Load the model and label encoder
13
+ model = joblib.load('crop_model.pkl')
14
+ label_encoder = joblib.load('label_encoder.pkl')
15
+
16
+
17
+ # Define preprocessing function
18
+ def preprocess_input(form_data):
19
+ """Ensure input data matches the model's requirements."""
20
+ required_columns = ['N', 'P', 'K', 'temperature', 'humidity', 'ph', 'rainfall']
21
+ input_data = pd.DataFrame([form_data], columns=required_columns)
22
+ return input_data
23
+
24
+
25
+ # Define prediction function
26
+ def predict(input_data):
27
+ """Predict the crop label for given input data."""
28
+ predictions = model.predict(input_data)
29
+ decoded_predictions = label_encoder.inverse_transform(predictions)
30
+ return decoded_predictions[0]
31
+
32
+
33
+ @app.route('/predict', methods=['POST'])
34
+ def get_prediction():
35
+ """Receive input data, preprocess, and return crop prediction."""
36
+ try:
37
+ print("here")
38
+ # Get the input JSON data from the request
39
+ form_data = request.json
40
+ print(form_data)
41
+
42
+ # Preprocess the input data
43
+ preprocessed_data = preprocess_input(form_data)
44
+
45
+ # Get the prediction from the model
46
+ predicted_label = predict(preprocessed_data)
47
+
48
+ print(predicted_label)
49
+
50
+ # Return the prediction as a JSON response
51
+ return jsonify({'crop': predicted_label})
52
+ except Exception as e:
53
+ return jsonify({'error': str(e)}), 400
54
+
55
+
56
+ if __name__ == '__main__':
57
+ app.run(debug=True)
crop_model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:72260ee7c0335c6c12f6db67b84bfff26816245a4a4da582024c76a0271cfd30
3
+ size 1680738
label_encoder.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:005f7e96ea3e09f640fba78c0494953dea9f949ae9b66de13b8e965c3a653e24
3
+ size 829
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ flask
2
+ flask-cors
3
+ pandas
4
+ scikit-learn
5
+ xgboost
6
+ joblib
7
+ gunicorn