Spaces:
Sleeping
Sleeping
Commit
·
e4fb332
1
Parent(s):
2803da4
Minor changes
Browse files
app.py
CHANGED
|
@@ -2,7 +2,6 @@ import streamlit as st
|
|
| 2 |
import pandas as pd
|
| 3 |
import joblib
|
| 4 |
|
| 5 |
-
|
| 6 |
models = ["Linear Regression", "XGBoost", "Random Forests Regressor"]
|
| 7 |
|
| 8 |
@st.cache_resource
|
|
@@ -27,7 +26,7 @@ def load_model(chosen_model: str) -> tuple:
|
|
| 27 |
chosen_model (str): The name of the model you wish to import for inference.
|
| 28 |
|
| 29 |
Returns:
|
| 30 |
-
The ML regressor model and the scaler
|
| 31 |
"""
|
| 32 |
model = None
|
| 33 |
if chosen_model == models[0]:
|
|
@@ -39,7 +38,7 @@ def load_model(chosen_model: str) -> tuple:
|
|
| 39 |
|
| 40 |
return joblib.load(model), joblib.load("./models/scaler.pkl")
|
| 41 |
|
| 42 |
-
def get_input_data(df: pd.core.frame.DataFrame)
|
| 43 |
"""
|
| 44 |
Takes the input of user that will be used to predict the crop yield.
|
| 45 |
|
|
@@ -47,7 +46,7 @@ def get_input_data(df: pd.core.frame.DataFrame) -> pd.core.frame.DataFrame:
|
|
| 47 |
df (pd.core.frame.DataFrame): The dataframe to be used as a basis as to what the user can choose.
|
| 48 |
|
| 49 |
Returns:
|
| 50 |
-
|
| 51 |
"""
|
| 52 |
|
| 53 |
item_mapping = {category: code for code, category in enumerate(df['Item'].unique())}
|
|
@@ -58,7 +57,7 @@ def get_input_data(df: pd.core.frame.DataFrame) -> pd.core.frame.DataFrame:
|
|
| 58 |
year = st.number_input("When do you wish to plant?", min_value=2025, max_value=2030, value=2025, placeholder="Choose a year")
|
| 59 |
avg_rainfall = st.slider("What is the estimated average rainfall for that year?", min_value=df['average_rain_fall_mm_per_year'].min(), max_value=df['average_rain_fall_mm_per_year'].max())
|
| 60 |
pesticides = st.slider("How much pesticides do you intend to use? (in tonnes)", max_value=int(df['pesticides_tonnes'].max()))
|
| 61 |
-
avg_temp = st.slider("How hot or cold is the area around you?", min_value=df['avg_temp'].min(), max_value=df['avg_temp'].max())
|
| 62 |
|
| 63 |
item_num = item_mapping[item]
|
| 64 |
area_num = area_mapping[area]
|
|
@@ -70,7 +69,7 @@ def get_input_data(df: pd.core.frame.DataFrame) -> pd.core.frame.DataFrame:
|
|
| 70 |
"average_rain_fall_mm_per_year": [avg_rainfall],
|
| 71 |
"pesticides_tonnes": [pesticides],
|
| 72 |
"avg_temp": [avg_temp]
|
| 73 |
-
})
|
| 74 |
|
| 75 |
def main():
|
| 76 |
st.title("Crop Yield Predictor")
|
|
@@ -81,8 +80,9 @@ def main():
|
|
| 81 |
|
| 82 |
st.dataframe(df, height=300, width=900)
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
|
|
|
| 86 |
|
| 87 |
selected_model = st.selectbox("Which model do you want to use?",
|
| 88 |
tuple(models),
|
|
@@ -98,14 +98,23 @@ def main():
|
|
| 98 |
st.image("./plots/rf_plot.png", caption="Random Forests Regressor Plot")
|
| 99 |
|
| 100 |
if st.button("Predict yield!"):
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
model, scaler = load_model(selected_model)
|
| 104 |
|
| 105 |
input_scaled = scaler.transform(input_data)
|
| 106 |
|
| 107 |
pred = model.predict(input_scaled)
|
| 108 |
|
| 109 |
-
st.
|
|
|
|
| 110 |
if __name__ == "__main__":
|
| 111 |
main()
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import joblib
|
| 4 |
|
|
|
|
| 5 |
models = ["Linear Regression", "XGBoost", "Random Forests Regressor"]
|
| 6 |
|
| 7 |
@st.cache_resource
|
|
|
|
| 26 |
chosen_model (str): The name of the model you wish to import for inference.
|
| 27 |
|
| 28 |
Returns:
|
| 29 |
+
tuple: The ML regressor model and the scaler
|
| 30 |
"""
|
| 31 |
model = None
|
| 32 |
if chosen_model == models[0]:
|
|
|
|
| 38 |
|
| 39 |
return joblib.load(model), joblib.load("./models/scaler.pkl")
|
| 40 |
|
| 41 |
+
def get_input_data(df: pd.core.frame.DataFrame):
|
| 42 |
"""
|
| 43 |
Takes the input of user that will be used to predict the crop yield.
|
| 44 |
|
|
|
|
| 46 |
df (pd.core.frame.DataFrame): The dataframe to be used as a basis as to what the user can choose.
|
| 47 |
|
| 48 |
Returns:
|
| 49 |
+
A dataframe that contains a single instance based on the user input, and a tuple of the item and area.
|
| 50 |
"""
|
| 51 |
|
| 52 |
item_mapping = {category: code for code, category in enumerate(df['Item'].unique())}
|
|
|
|
| 57 |
year = st.number_input("When do you wish to plant?", min_value=2025, max_value=2030, value=2025, placeholder="Choose a year")
|
| 58 |
avg_rainfall = st.slider("What is the estimated average rainfall for that year?", min_value=df['average_rain_fall_mm_per_year'].min(), max_value=df['average_rain_fall_mm_per_year'].max())
|
| 59 |
pesticides = st.slider("How much pesticides do you intend to use? (in tonnes)", max_value=int(df['pesticides_tonnes'].max()))
|
| 60 |
+
avg_temp = st.slider("How hot or cold is the area around you? (Average Temperature)", min_value=df['avg_temp'].min(), max_value=df['avg_temp'].max())
|
| 61 |
|
| 62 |
item_num = item_mapping[item]
|
| 63 |
area_num = area_mapping[area]
|
|
|
|
| 69 |
"average_rain_fall_mm_per_year": [avg_rainfall],
|
| 70 |
"pesticides_tonnes": [pesticides],
|
| 71 |
"avg_temp": [avg_temp]
|
| 72 |
+
}), (item, area)
|
| 73 |
|
| 74 |
def main():
|
| 75 |
st.title("Crop Yield Predictor")
|
|
|
|
| 80 |
|
| 81 |
st.dataframe(df, height=300, width=900)
|
| 82 |
|
| 83 |
+
st.divider()
|
| 84 |
+
|
| 85 |
+
input_data, (item, area) = get_input_data(df)
|
| 86 |
|
| 87 |
selected_model = st.selectbox("Which model do you want to use?",
|
| 88 |
tuple(models),
|
|
|
|
| 98 |
st.image("./plots/rf_plot.png", caption="Random Forests Regressor Plot")
|
| 99 |
|
| 100 |
if st.button("Predict yield!"):
|
| 101 |
+
|
| 102 |
+
col1, col2 = st.columns(2)
|
| 103 |
+
col1.metric("Area", area, border=True)
|
| 104 |
+
col2.metric("Item", item, border=True)
|
| 105 |
+
|
| 106 |
+
col3, col4, col5 = st.columns(3)
|
| 107 |
+
col3.metric("Average Rainfall", input_data['average_rain_fall_mm_per_year'], border=True)
|
| 108 |
+
col4.metric("Pestiscide Usage (Tonne)", input_data['pesticides_tonnes'], border=True)
|
| 109 |
+
col5.metric("Average Temperature (Celcius)", input_data['avg_temp'], border=True)
|
| 110 |
+
|
| 111 |
model, scaler = load_model(selected_model)
|
| 112 |
|
| 113 |
input_scaled = scaler.transform(input_data)
|
| 114 |
|
| 115 |
pred = model.predict(input_scaled)
|
| 116 |
|
| 117 |
+
st.header(f"Predicted Crop Yield: **{int(pred[0])}**")
|
| 118 |
+
|
| 119 |
if __name__ == "__main__":
|
| 120 |
main()
|