aaravlovescodes commited on
Commit
b1833aa
·
verified ·
1 Parent(s): 550f689

Create handler.py

Browse files
Files changed (1) hide show
  1. handler.py +62 -0
handler.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)