Aryan Tarafdar commited on
Commit
c606217
·
unverified ·
1 Parent(s): bb9e4d7

Add files via upload

Browse files
Files changed (3) hide show
  1. crop(test).ipynb +0 -0
  2. scaler.pkl +0 -0
  3. streamlitapp.py +156 -0
crop(test).ipynb ADDED
The diff for this file is too large to render. See raw diff
 
scaler.pkl ADDED
Binary file (925 Bytes). View file
 
streamlitapp.py ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import numpy as np
4
+ import pickle
5
+ import sklearn
6
+ from sklearn.preprocessing import MinMaxScaler
7
+
8
+ st.set_page_config(page_title="Crop Recommendation App", layout="wide")
9
+
10
+ # Load the scaler from the pickle file
11
+ with open('scaler.pkl', 'rb') as file:
12
+ scaler = pickle.load(file)
13
+
14
+ # Define the transformation function
15
+
16
+
17
+ def transform_input(data):
18
+ d = data[0]
19
+ for i in range(len(d)):
20
+ if idx_to_skew[i] == 0:
21
+ d[i] = np.square(d[i])
22
+ else:
23
+ d[i] = np.sqrt(d[i])
24
+ x = np.array([d])
25
+ x = scaler.transform(x)
26
+ return x
27
+
28
+
29
+ # Add a title and description
30
+ st.title("🌾 Crop Recommendation App")
31
+ st.write("""
32
+ This app predicts the **crop type** based on soil and weather conditions! 🌱
33
+ Upload a CSV file with your data or use the sliders and text inputs below to enter your data manually.
34
+ """)
35
+
36
+ # Sidebar for user input
37
+ st.sidebar.header('📝 User Input Features')
38
+ st.sidebar.markdown("""
39
+ Upload a CSV file with the features or use the sliders and text inputs below for manual input.
40
+
41
+ [Example CSV input file](https://example.com/example.csv)
42
+ """)
43
+
44
+ # Define the skew transformation
45
+ idx_to_skew = [1, 0, 1, 0, 0, 0, 1]
46
+
47
+ # File uploader for CSV input
48
+ uploaded_file = st.sidebar.file_uploader(
49
+ "Upload your input CSV file", type=["csv"])
50
+
51
+ if uploaded_file is not None:
52
+ input_df = pd.read_csv(uploaded_file)
53
+ st.sidebar.write("Uploaded CSV File:")
54
+ st.sidebar.write(input_df)
55
+ else:
56
+ st.sidebar.subheader('🔄 Manual Input (or use sliders)')
57
+
58
+ st.sidebar.write("**Use the sliders to adjust values:**")
59
+
60
+ nitrogen = st.sidebar.slider('Nitrogen', 0, 100, 50)
61
+ phosphorus = st.sidebar.slider('Phosphorus', 0, 100, 50)
62
+ potassium = st.sidebar.slider('Potassium', 0, 100, 50)
63
+ temperature = st.sidebar.slider('Temperature (°C)', 0.0, 50.0, 25.0)
64
+ humidity = st.sidebar.slider('Humidity (%)', 0.0, 100.0, 50.0)
65
+ ph = st.sidebar.slider('pH', 0.0, 14.0, 7.0)
66
+ rainfall = st.sidebar.slider('Rainfall (mm)', 0.0, 500.0, 100.0)
67
+
68
+ # Define text inputs for manual input
69
+ nitrogen_text = st.sidebar.number_input(
70
+ 'Nitrogen (0-100)', min_value=0, max_value=100, value=nitrogen)
71
+ phosphorus_text = st.sidebar.number_input(
72
+ 'Phosphorus (0-100)', min_value=0, max_value=100, value=phosphorus)
73
+ potassium_text = st.sidebar.number_input(
74
+ 'Potassium (0-100)', min_value=0, max_value=100, value=potassium)
75
+ temperature_text = st.sidebar.number_input(
76
+ 'Temperature (°C) (0.0-50.0)', min_value=0.0, max_value=50.0, step=0.1, value=temperature)
77
+ humidity_text = st.sidebar.number_input(
78
+ 'Humidity (%) (0.0-100.0)', min_value=0.0, max_value=100.0, step=0.1, value=humidity)
79
+ ph_text = st.sidebar.number_input(
80
+ 'pH (0.0-14.0)', min_value=0.0, max_value=14.0, step=0.1, value=ph)
81
+ rainfall_text = st.sidebar.number_input(
82
+ 'Rainfall (mm) (0.0-500.0)', min_value=0.0, max_value=500.0, step=0.1, value=rainfall)
83
+
84
+ data_slider = {
85
+ 'NITROGEN': nitrogen,
86
+ 'PHOSPHORUS': phosphorus,
87
+ 'POTASSIUM': potassium,
88
+ 'TEMPERATURE': temperature,
89
+ 'HUMIDITY': humidity,
90
+ 'PH': ph,
91
+ 'RAINFALL': rainfall
92
+ }
93
+ input_df_slider = pd.DataFrame(data_slider, index=[0])
94
+
95
+ data_text = {
96
+ 'NITROGEN': nitrogen_text,
97
+ 'PHOSPHORUS': phosphorus_text,
98
+ 'POTASSIUM': potassium_text,
99
+ 'TEMPERATURE': temperature_text,
100
+ 'HUMIDITY': humidity_text,
101
+ 'PH': ph_text,
102
+ 'RAINFALL': rainfall_text
103
+ }
104
+ input_df_text = pd.DataFrame(data_text, index=[0])
105
+
106
+ use_sliders = st.sidebar.radio(
107
+ "Choose input method:", ("Use Sliders", "Use Text Inputs"))
108
+
109
+ if use_sliders == "Use Sliders":
110
+ st.sidebar.write("**Slider Values:**")
111
+ st.sidebar.write(input_df_slider)
112
+ input_df = input_df_slider
113
+ else:
114
+ st.sidebar.write("**Text Input Values:**")
115
+ st.sidebar.write(input_df_text)
116
+ input_df = input_df_text
117
+
118
+ # Display user input features
119
+ st.subheader('🌟 User Input Features')
120
+ st.write(input_df)
121
+
122
+ # Transform the input data
123
+ input_data = transform_input(input_df.values)
124
+
125
+ # Load the trained model
126
+ load_clf = pickle.load(open('best_RF.pkl', 'rb'))
127
+
128
+ # Make predictions
129
+ prediction = load_clf.predict(input_data)
130
+ prediction_proba = load_clf.predict_proba(input_data)
131
+
132
+ # Display the prediction
133
+ st.subheader('🔍 Prediction')
134
+ crop_types = ['banana', 'blackgram', 'chickpea', 'coconut', 'coffee', 'cotton', 'jute',
135
+ 'kidneybeans', 'lentil', 'maize', 'mango', 'mothbeans', 'mungbean', 'muskmelon',
136
+ 'orange', 'papaya', 'pigeonpeas', 'pomegranate', 'rice', 'watermelon']
137
+
138
+ predicted_crop = prediction[0]
139
+ st.write(f"**Predicted Crop Type:** :green[{predicted_crop}]")
140
+
141
+ # Display prediction probability
142
+ st.subheader('📊 Prediction Probability')
143
+ prob_df = pd.DataFrame(prediction_proba, columns=crop_types)
144
+ st.bar_chart(prob_df.T, height=300, use_container_width=True)
145
+
146
+ # Display the class with the highest probability
147
+ highest_prob_class_index = np.argmax(prediction_proba, axis=1)
148
+ highest_prob_class = crop_types[highest_prob_class_index[0]]
149
+ highest_prob_value = prediction_proba[0, highest_prob_class_index[0]]
150
+
151
+ st.subheader('🏆 Class with Highest Probability')
152
+ st.write(f"**Class:** :blue[{highest_prob_class}]")
153
+ st.write(f"**Probability:** :blue[{highest_prob_value:.4f}]")
154
+
155
+ st.markdown("---")
156
+ st.markdown("Thank you for using the Crop Recommendation App! 🌾")