aaravlovescodes commited on
Commit
44acb8f
·
verified ·
1 Parent(s): b1833aa

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +33 -61
handler.py CHANGED
@@ -1,62 +1,34 @@
1
- import streamlit as st
2
- import numpy as np
3
  import pickle
4
-
5
- # Load your model and scalers
6
- model = pickle.load(open('model.pkl', 'rb'))
7
- sc = pickle.load(open('standscaler.pkl', 'rb'))
8
- ms = pickle.load(open('minmaxscaler.pkl', 'rb'))
9
-
10
- # Title of the web app
11
- st.title("Crop Recommendation System 🌱")
12
-
13
- # Create a sidebar for input fields
14
- with st.sidebar:
15
- st.header("Input Parameters")
16
- N = st.number_input("Nitrogen content", min_value=0.0, value=0.0, step=0.1, format="%.1f", help="Enter Nitrogen content")
17
- P = st.number_input("Phosphorus content", min_value=0.0, value=0.0, step=0.1, format="%.1f", help="Enter Phosphorus content")
18
- K = st.number_input("Potassium content", min_value=0.0, value=0.0, step=0.1, format="%.1f", help="Enter Potassium content")
19
- temp = st.number_input("Temperature in °C", min_value=0.0, value=0.0, step=0.1, format="%.1f", help="Enter Temperature in °C")
20
- humidity = st.number_input("Humidity in %", min_value=0.0, value=0.0, step=0.1, format="%.1f", help="Enter Humidity in %")
21
- ph = st.number_input("pH value", min_value=0.0, value=0.0, step=0.1, format="%.1f", help="Enter pH value")
22
- rainfall = st.number_input("Rainfall in mm", min_value=0.0, value=0.0, step=0.1, format="%.1f", help="Enter Rainfall in mm")
23
-
24
- if st.button("Get Recommendation"):
25
- feature_list = [N, P, K, temp, humidity, ph, rainfall]
26
- single_pred = np.array(feature_list).reshape(1, -1)
27
-
28
- scaled_features = ms.transform(single_pred)
29
- final_features = sc.transform(scaled_features)
30
- prediction = model.predict(final_features)
31
-
32
- crop_dict = {1: "Rice", 2: "Maize", 3: "Jute", 4: "Cotton", 5: "Coconut", 6: "Papaya", 7: "Orange",
33
- 8: "Apple", 9: "Muskmelon", 10: "Watermelon", 11: "Grapes", 12: "Mango", 13: "Banana",
34
- 14: "Pomegranate", 15: "Lentil", 16: "Blackgram", 17: "Mungbean", 18: "Mothbeans",
35
- 19: "Pigeonpeas", 20: "Kidneybeans", 21: "Chickpea", 22: "Coffee"}
36
-
37
- if prediction[0] in crop_dict:
38
- crop = crop_dict[prediction[0]]
39
- result = f"{crop} is the best crop to be cultivated right there."
40
- else:
41
- result = "Sorry, we could not determine the best crop to be cultivated with the provided data."
42
-
43
- st.success(result)
44
-
45
- # Footer
46
- st.markdown("""
47
- <style>
48
- .footer {
49
- position: fixed;
50
- left: 0;
51
- bottom: 0;
52
- width: 100%;
53
- background-color: #f1f1f1;
54
- color: #555;
55
- text-align: center;
56
- padding: 10px;
57
- }
58
- </style>
59
- <div class="footer">
60
- Powered by Streamlit
61
- </div>
62
- """, unsafe_allow_html=True)
 
 
 
1
  import pickle
2
+ import numpy as np
3
+ from flask import Flask, request, jsonify
4
+
5
+ # Load the pickle model
6
+ MODEL_PATH = "/mnt/data/model.pkl"
7
+
8
+ app = Flask(__name__)
9
+
10
+ # Load the model
11
+ with open(MODEL_PATH, 'rb') as file:
12
+ model = pickle.load(file)
13
+
14
+ @app.route('/predict', methods=['POST'])
15
+ def predict():
16
+ try:
17
+ # Parse input JSON data
18
+ input_data = request.get_json()
19
+ if not input_data:
20
+ return jsonify({"error": "Invalid input data"}), 400
21
+
22
+ # Assuming the input data is a list of features
23
+ features = np.array(input_data['features']).reshape(1, -1) # Adjust for single input
24
+
25
+ # Make predictions
26
+ prediction = model.predict(features)
27
+
28
+ # Return the prediction as JSON
29
+ return jsonify({"prediction": prediction.tolist()}), 200
30
+ except Exception as e:
31
+ return jsonify({"error": str(e)}), 500
32
+
33
+ if __name__ == '__main__':
34
+ app.run(host='0.0.0.0', port=5000)