Spaces:
Sleeping
Sleeping
Update pages/Prediction.py
Browse files- pages/Prediction.py +35 -32
pages/Prediction.py
CHANGED
|
@@ -1,50 +1,53 @@
|
|
| 1 |
import pickle
|
| 2 |
-
import
|
|
|
|
|
|
|
|
|
|
| 3 |
with open("model.pkl", "rb") as file:
|
| 4 |
model = pickle.load(file)
|
| 5 |
|
|
|
|
|
|
|
|
|
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
# Function to take user inputs for the features using Streamlit sliders
|
| 12 |
-
def get_user_input():
|
| 13 |
-
st.sidebar.header('User Input Parameters')
|
| 14 |
-
nitrogen = st.sidebar.slider('Nitrogen', min_value=0.0, max_value=200.0, value=10.0)
|
| 15 |
-
phosphorus = st.sidebar.slider('Phosphorus', min_value=0.0, max_value=200.0, value=10.0)
|
| 16 |
-
potassium = st.sidebar.slider('Potassium', min_value=0.0, max_value=200.0, value=10.0)
|
| 17 |
-
temperature = st.sidebar.slider('Temperature (°C)', min_value=0.0, max_value=50.0, value=25.0)
|
| 18 |
-
humidity = st.sidebar.slider('Humidity (%)', min_value=0.0, max_value=100.0, value=60.0)
|
| 19 |
-
ph_value = st.sidebar.slider('pH Value', min_value=0.0, max_value=14.0, value=7.0)
|
| 20 |
-
rainfall = st.sidebar.slider('Rainfall (mm)', min_value=0.0, max_value=500.0, value=100.0)
|
| 21 |
-
|
| 22 |
-
# Create a numpy array from the user input
|
| 23 |
-
user_input = np.array([nitrogen, phosphorus, potassium, temperature, humidity, ph_value, rainfall]).reshape(1, -1)
|
| 24 |
return user_input
|
| 25 |
|
| 26 |
# Get user input
|
| 27 |
user_input = get_user_input()
|
| 28 |
|
| 29 |
-
# Preprocessing the input data (Scaling using StandardScaler)
|
| 30 |
-
scaler = StandardScaler()
|
| 31 |
-
user_input_scaled = scaler.fit_transform(user_input)
|
| 32 |
-
|
| 33 |
# Predict the crop using the model
|
| 34 |
-
prediction = model.predict(
|
| 35 |
|
| 36 |
# Display the prediction
|
| 37 |
st.title('Crop Prediction Application')
|
| 38 |
st.write('### Crop Prediction Based on Input Parameters')
|
| 39 |
-
st.write(f'Predicted Crop: {prediction[0]}')
|
| 40 |
-
|
| 41 |
-
#
|
| 42 |
-
st.write('### Input Parameters:')
|
| 43 |
-
st.write(f'Nitrogen: {user_input[0][0]}')
|
| 44 |
-
st.write(f'Phosphorus: {user_input[0][1]}')
|
| 45 |
-
st.write(f'Potassium: {user_input[0][2]}')
|
| 46 |
-
st.write(f'Temperature: {user_input[0][3]}')
|
| 47 |
-
st.write(f'Humidity: {user_input[0][4]}')
|
| 48 |
-
st.write(f'
|
| 49 |
-
st.write(f'Rainfall: {user_input[0][6]}')
|
|
|
|
| 50 |
|
|
|
|
| 1 |
import pickle
|
| 2 |
+
import numpy as np
|
| 3 |
+
import streamlit as st
|
| 4 |
+
|
| 5 |
+
# Load the trained model
|
| 6 |
with open("model.pkl", "rb") as file:
|
| 7 |
model = pickle.load(file)
|
| 8 |
|
| 9 |
+
# Function to take user inputs from text input fields with explanations
|
| 10 |
+
def get_user_input():
|
| 11 |
+
st.sidebar.header('Enter Input Parameters')
|
| 12 |
|
| 13 |
+
# Soil Nutrients (Integer values)
|
| 14 |
+
nitrogen = st.sidebar.text_input('Nitrogen (N),'10')
|
| 15 |
+
phosphorus = st.sidebar.text_input('Phosphorus (P), '10')
|
| 16 |
+
potassium = st.sidebar.text_input('Potassium (K) , '10')
|
| 17 |
|
| 18 |
+
# Weather Conditions (Float values)
|
| 19 |
+
temperature = st.sidebar.text_input('Temperature (°C), '25.0')
|
| 20 |
+
humidity = st.sidebar.text_input('Humidity (%) , '60.0')
|
| 21 |
+
ph_value = st.sidebar.text_input('Soil pH , '7.0')
|
| 22 |
+
rainfall = st.sidebar.text_input('Rainfall (mm), '100.0')
|
| 23 |
+
|
| 24 |
+
# Convert input to correct data types
|
| 25 |
+
user_input = np.array([
|
| 26 |
+
int(nitrogen), int(phosphorus), int(potassium),
|
| 27 |
+
float(temperature), float(humidity), float(ph_value), float(rainfall)
|
| 28 |
+
]).reshape(1, -1)
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
return user_input
|
| 31 |
|
| 32 |
# Get user input
|
| 33 |
user_input = get_user_input()
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
# Predict the crop using the model
|
| 36 |
+
prediction = model.predict(user_input)
|
| 37 |
|
| 38 |
# Display the prediction
|
| 39 |
st.title('Crop Prediction Application')
|
| 40 |
st.write('### Crop Prediction Based on Input Parameters')
|
| 41 |
+
st.write(f'Predicted Crop: **{prediction[0]}**')
|
| 42 |
+
|
| 43 |
+
# Display input values
|
| 44 |
+
st.write('### Input Parameters Entered:')
|
| 45 |
+
st.write(f'**Nitrogen (N):** {user_input[0][0]}')
|
| 46 |
+
st.write(f'**Phosphorus (P):** {user_input[0][1]}')
|
| 47 |
+
st.write(f'**Potassium (K):** {user_input[0][2]} ')
|
| 48 |
+
st.write(f'**Temperature (°C):** {user_input[0][3]} ')
|
| 49 |
+
st.write(f'**Humidity (%):** {user_input[0][4]} ')
|
| 50 |
+
st.write(f'**Soil pH:** {user_input[0][5]} ')
|
| 51 |
+
st.write(f'**Rainfall (mm):** {user_input[0][6]}')
|
| 52 |
+
|
| 53 |
|