import streamlit as st import joblib import pickle import pandas as pd import numpy as np from model_interface.hf_model_store import get_artifact_path def expense_forecasting_farmerp(): st.title("Expense Forecasting") model = joblib.load( get_artifact_path("13_Expense_forecasting(FarmERP)/Expense_model2.pkl" )) encoders = joblib.load( get_artifact_path("13_Expense_forecasting(FarmERP)/labels_expense.pkl" )) reference_df = joblib.load( get_artifact_path("13_Expense_forecasting(FarmERP)/expense_reference.pkl" )) # PAGE CONFIG # st.set_page_config( # page_title="Expense Forecasting App", # layout="wide" # ) # st.title("🌾 Expense & Harvest Forecasting - VegPro") # SIDEBAR INPUTS (WITH 'All' OPTION) st.sidebar.header("Enter Crop & Field Details") # 1. Site Name site_options = ["All"] + sorted(reference_df["Site_Name"].unique()) site_name = st.sidebar.selectbox("Site Name", site_options) if site_name != "All": site_df = reference_df[reference_df["Site_Name"] == site_name] else: site_df = reference_df.copy() # 2. Plot Name plot_options = ["All"] + sorted(site_df["Plot_Name"].unique()) plot_name = st.sidebar.selectbox("Plot Name", plot_options) if plot_name != "All": plot_df = site_df[site_df["Plot_Name"] == plot_name] else: plot_df = site_df.copy() # 3. Crop Name crop_options = ["All"] + sorted(plot_df["Crop_Name"].unique()) crop_name = st.sidebar.selectbox("Crop Name", crop_options) if crop_name != "All": filtered_df = plot_df[plot_df["Crop_Name"] == crop_name] else: filtered_df = plot_df.copy() # PREDICTION if st.sidebar.button("🔮 Predict"): if filtered_df.empty: st.warning("No records found for selected inputs.") st.stop() results = [] # Detect area column automatically area_col = [c for c in filtered_df.columns if "area" in c.lower()][0] for _, row in filtered_df.iterrows(): area_acres = float(row[area_col]) input_df = pd.DataFrame({ "Site_Name": [row["Site_Name"]], "Plot_Name": [row["Plot_Name"]], "SubPlot_Name": [row["SubPlot_Name"]], "Crop_Name": [row["Crop_Name"]], "Crop_Type": [row["Crop_Type"]], "Variety_Name": [row["Variety_Name"]], "Area_acres": [area_acres] }) # Encode categorical columns for col, encoder in encoders.items(): if input_df[col].iloc[0] not in encoder.classes_: st.error(f"❌ Unknown value in '{col}': {input_df[col].iloc[0]}") st.stop() input_df[col] = encoder.transform(input_df[col]) # Model prediction predictions = model.predict(input_df) total_expense = float(predictions[0][0]) total_harvested_qty = float(predictions[0][1]) results.append({ "Site Name": row["Site_Name"], "Plot Name": row["Plot_Name"], "Sub Plot Name": row["SubPlot_Name"], "Crop Name": row["Crop_Name"], "Crop Type": row["Crop_Type"], "Variety Name": row["Variety_Name"], "Area (Acres)": area_acres, "Total Estimated Production Qty(Kg)": 0, "Total Harvested Qty (Kg)": round(total_harvested_qty, 2), "Total Expenses (KES)": round(total_expense, 2), #"Cost Per Unit": 0 }) result_df = pd.DataFrame(results) # DISPLAY RESULTS st.subheader("📊 Forecast Results") st.dataframe(result_df, use_container_width=True) # SUMMARY METRICS st.subheader("📈 Overall Summary") col1, col2, col3 = st.columns(3) total_expenses = result_df["Total Expenses (KES)"].sum() total_qty = result_df["Total Harvested Qty (Kg)"].sum() col1.metric("Total Expenses (KES)", round(total_expenses, 2)) col2.metric("Total Harvested Qty (Kg)", round(total_qty, 2)) #col3.metric("Avg Cost Per Unit", 0)