Crop-Yield / app.py
kanneboinakumar's picture
Update app.py
15b38db verified
import streamlit as st
from joblib import load
# Load model and encoder
model = load('RandomForestRegressor.plk')
encoder = load('label_encoders.pkl')
# Styled Title
st.markdown("""
<h1 style='text-align: center; color: #ff6600;background-color: lightblue;'>Crop Yield Prediction</h1>
""", unsafe_allow_html=True)
# Input Layout
col1, col2, col3 = st.columns(3)
# Crop selection
dropdown_style = """
<style>
.stSelectbox label { font-weight: bold; color: #ff6600; }
</style>
"""
st.markdown(dropdown_style, unsafe_allow_html=True)
crop_options = ['Arecanut', 'Arhar/Tur', 'Bajra', 'Banana', 'Barley',
'Black pepper', 'Cardamom', 'Cashewnut', 'Castor seed', 'Coconut',
'Coriander', 'Cotton(lint)', 'Cowpea(Lobia)', 'Dry chillies',
'Garlic', 'Ginger', 'Gram', 'Groundnut', 'Guar seed', 'Horse-gram',
'Jowar', 'Jute', 'Khesari', 'Linseed', 'Maize', 'Masoor', 'Mesta',
'Moong(Green Gram)', 'Moth', 'Niger seed', 'Oilseeds total',
'Onion', 'Other Rabi pulses', 'Other Cereals',
'Other Kharif pulses', 'Other Summer Pulses',
'Peas & beans (Pulses)', 'Potato', 'Ragi', 'Rapeseed &Mustard',
'Rice', 'Safflower', 'Sannhamp', 'Sesamum', 'Small millets',
'Soyabean', 'Sugarcane', 'Sunflower', 'Sweet potato', 'Tapioca',
'Tobacco', 'Turmeric', 'Urad', 'Wheat', 'other oilseeds']
with col1:
Crop = st.selectbox('Crop', options=crop_options)
Crop = encoder['Crop'].transform([Crop])[0]
Area = st.number_input("Area (m²)")
Fertilizer = st.number_input("Fertilizer", min_value=50)
with col2:
Crop_Year = st.number_input("Crop Year", step=1, min_value=1997)
Production = st.number_input("Production", min_value=0)
Pesticide = st.number_input("Pesticide", min_value=1000)
with col3:
Season_options = ['Autumn', 'Kharif', 'Rabi', 'Summer', 'Whole Year', 'Winter']
Season = st.selectbox("Season", options=Season_options)
Season = encoder['Season'].transform([Season])[0]
Annual_Rainfall = st.number_input("Annual Rainfall", min_value=300, max_value=6600)
# State selection
State_options = ['Andhra Pradesh', 'Arunachal Pradesh', 'Assam', 'Bihar',
'Chhattisgarh', 'Delhi', 'Goa', 'Gujarat', 'Haryana',
'Himachal Pradesh', 'Jammu and Kashmir', 'Jharkhand', 'Karnataka',
'Kerala', 'Madhya Pradesh', 'Maharashtra', 'Manipur', 'Meghalaya',
'Mizoram', 'Nagaland', 'Odisha', 'Puducherry', 'Punjab', 'Sikkim',
'Tamil Nadu', 'Telangana', 'Tripura', 'Uttar Pradesh',
'Uttarakhand', 'West Bengal']
State = st.selectbox("State", options=State_options)
State = encoder["State"].transform([State])[0]
values = [Crop, Crop_Year, Season, State, Area, Production, Annual_Rainfall, Fertilizer, Pesticide]
# Styled Predict Button
button_style = """
<style>
div.stButton > button:first-child {
background-color: green;
color: white;
width: 200px;
display: block;
margin: auto;
}
</style>
"""
st.markdown(button_style, unsafe_allow_html=True)
if st.button('Predict'):
prediction = model.predict([values])
st.markdown(f"""
<h3 style='text-align: center; color: blue;'>Predicted Crop Yield: {prediction[0]:.2f}</h3>
""", unsafe_allow_html=True)