-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
83 lines (66 loc) · 2.79 KB
/
app.py
File metadata and controls
83 lines (66 loc) · 2.79 KB
1
2
3
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import streamlit as st
import joblib
import pandas as pd
# 1. Page Config
st.set_page_config(page_title="Crop Yield Predictor", page_icon="🌾")
# 2. Loading the Pipeline
@st.cache_resource
def load_model():
return joblib.load("crop_yield_model.pkl")
model = load_model()
# 3. Sidebar Navigation
st.sidebar.title("Navigation")
page = st.sidebar.radio("Go to", ["Prediction", "History", "Dataset Info", "Settings"])
if page == "Prediction":
st.title("🌾 Crop Yield Prediction")
with st.form("prediction_form"):
col1, col2 = st.columns(2)
with col1:
crop = st.selectbox("Crop Type", options=["Maize", "Rice", "Cassava", "Wheat", "Sorghum"])
area = st.number_input("Area (Hectares)", min_value=0.1, value=1.0, step=0.1)
rainfall = st.number_input("Rainfall (mm)", min_value=0.0, value=1200.0)
temp = st.number_input("Temperature (°C)", value=28.0)
with col2:
humidity = st.slider("Humidity (%)", 0, 100, 75)
sunlight = st.number_input("Sunlight Hours", min_value=0.0, max_value=24.0, value=7.5)
soil_ph = st.slider("Soil pH", 0.0, 14.0, 6.5)
st.write("**Soil Nutrients**")
n_col, p_col, k_col = st.columns(3)
n = n_col.number_input("N", value=80.0)
p = p_col.number_input("P", value=45.0)
k = k_col.number_input("K", value=60.0)
submit = st.form_submit_button("Predict Yield")
if submit:
# Create input DataFrame
input_df = pd.DataFrame([{
"rainfall_mm": rainfall,
"temperature_c": temp,
"humidity_percent": humidity,
"soil_ph": soil_ph,
"nitrogen": n,
"phosphorus": p,
"potassium": k,
"sunlight_hours": sunlight,
"area_hectares": area,
"crop_type": crop
}])
prediction = model.predict(input_df)[0]
st.success(f"### Estimated Yield: **{prediction:.2f} tonnes**")
# Save to session state for the 'History' page
if 'history' not in st.session_state:
st.session_state['history'] = []
st.session_state['history'].append({"Crop": crop, "Yield": round(prediction, 2)})
elif page == "History":
st.title("📜 Prediction History")
if 'history' in st.session_state and st.session_state['history']:
st.table(pd.DataFrame(st.session_state['history']))
else:
st.info("No predictions made yet.")
elif page == "Dataset Info":
st.title("📊 Dataset & Model Metrics")
st.write("The model was trained on `crop_yield_data.csv`.")
st.metric("Model Accuracy ($R^2$)", "0.79")
st.metric("RMSE", "0.53")
elif page == "Settings":
st.title("⚙️ Settings")
st.write("App version: 1.0.0")