Spaces:
Runtime error
Runtime error
# import libraries | |
import streamlit as st | |
import pandas as pd | |
import numpy as np | |
import pickle | |
df = pd.read_csv("rtv.csv", low_memory=False) | |
# load model from pickle file | |
with open("rf.pkl", "rb") as f: | |
rf = pickle.load(f) | |
st.set_page_config( | |
page_title = 'Real-Time Data Science Dashboard', | |
page_icon = '✅', | |
layout = 'wide' | |
) | |
# create sidebar title | |
st.title("Raising the :green[Village] :desert:") | |
st.image("https://ngosjob.b-cdn.net/wp-content/uploads/2023/03/Raising-The-Village.png") | |
# creating a single-element container. | |
placeholder = st.empty() | |
duration = df['duration'].mean() | |
mean = pd.to_numeric(df['HH Income (UGX)'], errors='coerce').mean() | |
maxi = pd.to_numeric(df['HH Income (UGX)'], errors='coerce').max() | |
with placeholder.container(): | |
# create three columns | |
kpi1, kpi2, kpi3 = st.columns(3) | |
# fill in those three columns with respective metrics or KPIs | |
kpi1.metric(label="Average Income ⏳", value=round(mean)) | |
kpi2.metric(label="Mean Time(minutes) 💍", value= int(duration)) | |
kpi3.metric(label="Max income$", value= f"$ {round(maxi,2)} ") | |
# create title | |
st.title("Income Prediction") | |
# create input fields for each feature | |
inputs = [] | |
X_columns = ['Season 2 Agriculture Value (Ugx)', | |
'Casual Labour (Ugx)', | |
'Season 1 Agriculture Value (Ugx)', | |
'Perenial Crops Income (Ugx)', | |
'Personal Business & Self Employment (Ugx)', | |
'Agriculture Income (UGX) ', | |
'Perennial Agriculture Value (Ugx)', | |
'HH Income + Consumption + Residues (UGX)'] | |
for col in X_columns: | |
value = st.number_input(f"Enter value for {col}", value=0.0) | |
inputs.append(value) | |
# create button for prediction | |
predict = st.button("Predict Income") | |
if predict: | |
# convert inputs to numpy array and reshape to match model input shape | |
inputs = np.array(inputs).reshape(1, -1) | |
# make prediction using loaded model | |
y_pred = rf.predict(inputs) | |
# display prediction result | |
st.write(f"The predicted income value is {y_pred[0]:.2f}") | |
st.markdown("============================================================================================================================") | |
# make prediction and display result if button is clicked and data is uploaded | |
# display file upload option | |
st.title("Get predictions of the whole dataset") | |
st.write("Upload your csv or excel file here:") | |
st.write("The dashboard supports only csv files at the moment") | |
uploaded_file = st.file_uploader("Choose a file", type=["csv", "xlsx"]) | |
def preproc(df): | |
df = df[['Season 2 Agriculture Value (Ugx)', | |
'Casual Labour (Ugx)', | |
'Season 1 Agriculture Value (Ugx)', | |
'Perenial Crops Income (Ugx)', | |
'Personal Business & Self Employment (Ugx)', | |
'Agriculture Income (UGX) ', | |
'Perennial Agriculture Value (Ugx)', | |
'HH Income + Consumption + Residues (UGX)']] | |
df.fillna(df.mean(), inplace=True) | |
return df | |
# load and display data if uploaded | |
if uploaded_file is not None: | |
if uploaded_file.type == "text/csv": | |
df = pd.read_csv(uploaded_file) | |
elif uploaded_file.type == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": | |
df = pd.read_excel(uploaded_file) | |
X_new = preproc(df) | |
else: | |
st.write("No file uploaded yet.") | |
# create button for prediction | |
predict = st.button("Predict") | |
# make prediction and display result if button is clicked and data is uploaded | |
if uploaded_file is not None and predict: | |
# make prediction using loaded model | |
y_pred = rf.predict(X_new) | |
# display prediction result as dataframe | |
df["prediction"] = y_pred.round(2) | |
st.dataframe(df[["Surveyor_Name", "prediction"]]) | |
job_filter = st.selectbox("Select the Quartile", pd.unique(df['Quartile'])) | |
# dataframe filter | |
df1 = df[df['Quartile']==job_filter] | |
st.dataframe(df1) | |