Use a CSV file as input to classifier, and output the results

Hi all. I built a web app using Flask and Flasgger but after finding out about streamlit, I want to use streamlit to make something that looks prettier.

Μy streamlit_app.py looks like this:

import numpy as np
import pickle
import pandas as pd
import streamlit as st 
import os

ada_boost_model_goldman = pickle.load(open("ada_boost_model.dat", "rb"))
xgb_model_goldman = pickle.load(open("xgb_model.dat", "rb"))
lgb_model_goldman = pickle.load(open("lgb_model.dat", "rb"))

The functions that make the prediction look like this:

def predict_property_osteometric_xgb_classifier(BIB,HML,HHD,RML,FML,FBL,FHD,TML):
   
    x = [[BIB,HML,HHD,RML,FML,FBL,FHD,TML]]

    x = np.asarray(x, dtype='float64')

    predict_proba = xgb_model_goldman.predict_proba(x)

    return predict_proba

My main function looks like this:

def main():
    st.title("Property estimation web application")
    html_temp = """
    <div style="background-color:tomato;padding:10px">
    <h2 style="color:white;text-align:center;">Streamlit Property Estimation Machine Learning App </h2>
    </div>
    """
    st.markdown(html_temp,unsafe_allow_html=True)

    BIB=st.text_input("BIB", "")
    HML=st.text_input("HML", "")
    HHD=st.text_input("HHD", "")
    RML=st.text_input("RML", "")
    FML=st.text_input("FML", "")
    FBL=st.text_input("FBL", "")
    FHD=st.text_input("FHD", "")
    TML=st.text_input("TML", "")
    result=""

    if st.button("Predict Property Using XGB boosting"):
      result=predict_property_osteometric_xgb_classifier(BIB, HML, HHD, RML, FML, FBL, FHD, TML)
      st.success('The Property is {}'.format(result))

    if st.button("Predict Property Using LGB boosting"):
      result=predict_property_osteometric_lgb_classifier(BIB, HML, HHD, RML, FML, FBL, FHD, TML)
      st.success('The Property is {}'.format(result))

    if st.button("Predict Property Using ADA boosting"):
      result=predict_property_osteometric_ada_boost_classifier(BIB, HML, HHD, RML, FML, FBL, FHD, TML)
      st.success('The Property is {}'.format(result))

if __name__=='__main__':
    main()

Everything worked like a charm up to this point.

But then, I wanted to write a function that will take a CSV file as an input, and output the appropriate result. Here is the function:

def predict_property_with_file_osteometric_xgb_classifier():

  uploaded_file = st.file_uploader("Choose a file", type = ['csv'])
  
  if uploaded_file is not None:
    df_test=pd.read_csv(uploaded_file, header=None, usecols=[0,1,2,3,4,5,6,7])

    df_test = df_test.dropna()
    
    prediction=xgb_model_goldman.predict_proba(df_test.values)

    return prediction

And here is the appropriate script inside the main function:

def main():

...

if st.button("Predict Property Using XGB boosting and a file entry"):
      result=predict_property_with_file_osteometric_xgb_classifier()
      st.success('The Property is {}'.format(result))

When I press the button, it appears that the file is indeed uploaded, but no output, or error is shown on the web app. What am I doing wrong?

Thank you in advance, and I’m glad I found this community :slight_smile: