Update handler.py
Browse files- 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 |
-
|
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 |
-
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|