Not Showing the main() function (going blank without error)

Hi,

My streamlit running successfully with a blank page except the title.
Please check if there is anything wrong with the code below:

# -*- coding: utf-8 -*-
"""
Created on Thu Jan 27 22:38:42 2022

@author: alishayzadag
"""

import numpy as np
import pickle
import streamlit as st

st.title('Pandas Real Estates')

# Loading the model
loaded_model = pickle.load(open('C:/Users/alishayzadag/Documents/mcs_fyp_2021/streamlit/trained_model2.sav', 'rb'))

# creating the funtion for webapp

def price_pred(input_data):
  #  input_data = np.array([[-0.43942006,  3.12628155, -1.12165014, -0.27288841, -1.42262747,
      # -0.24141041, -1.31238772,  2.61111401, -1.0016859 , -0.5778192 ,
   #    -0.97491834,  0.41164221, -0.86091034]])
   
   input_data_as = np.array([input_data])
   result = loaded_model.predict(input_data_as)
   print(result)
   
   return result

def main():
    
    #fetching the data from the user
    CRIM = st.text_input('Enter your value')
    ZN = st.text_input('Enter your value')
    INDUS = st.text_input('Enter your value')
    CHAS = st.text_input('Enter your value')
    NOX = st.text_input('Enter your value')
    RM = st.text_input('Enter your value')
    AGE = st.text_input('Enter your value')
    DIS = st.text_input('Enter your value')
    RAD	 = st.text_input('Enter your value')
    TAX	 = st.text_input('Enter your value')
    PTRATIO = st.text_input('Enter your value')
    B = st.text_input('Enter your value')
    LSTAT = st.text_input('Enter your value')
    
    # fethcing the result
    
    final_result = ''
    
    if st.button('Calculate'):
        final_result = price_pred(CRIM, ZN, INDUS, CHAS, NOX, RM, AGE, DIS, RAD, TAX, PTRATIO, B, LSTAT)
        
        st.success(final_result)
        
        if __name__ == "__main__":
          main()
    

Do you get an Error/Exception?
You seem to be missing the class definition of whatever you are unpickling.
(import…)

:black_cat:

Hi, I don’t get any error messages while running the whole code.

Can you elaborate please? I am a newbie.

Hi,

When you load an object that was pickled, the class definition needs to be available.

Try to see if loading the pickled file and executing the prediction works without using streamlit.

Hi,

yes it does. Below is the code without streamlit:

# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""
import numpy as np
import pickle

# Loading the model
loaded_model = pickle.load(open('C:/Users/alishayzadag/Documents/mcs_fyp_2021/streamlit/trained_model2.sav', 'rb'))

# # Input data

# input_data = (-0.43942006,  3.12628155, -1.12165014, -0.27288841, -1.42262747,
#        -0.24141041, -1.31238772,  2.61111401, -1.0016859 , -0.5778192 ,
#        -0.97491834,  0.41164221, -0.86091034)


def price_pred(input_data):
  #  input_data = np.array([[-0.43942006,  3.12628155, -1.12165014, -0.27288841, -1.42262747,
      # -0.24141041, -1.31238772,  2.61111401, -1.0016859 , -0.5778192 ,
   #    -0.97491834,  0.41164221, -0.86091034]])
   
   input_data_as = np.array([input_data])
    #reshaping the data
   input_data_reshaped = input_data_as.reshape(1,-1)
   result = loaded_model.predict(input_data_reshaped)
   
   return result

def main():
    
    #fetching the data from the user
    CRIM = input('Enter your value')
    ZN = input('Enter your value')
    INDUS = input('Enter your value')
    CHAS = input('Enter your value')
    NOX = input('Enter your value')
    RM = input('Enter your value')
    AGE = input('Enter your value')
    DIS = input('Enter your value')
    RAD	 = input('Enter your value')
    TAX	 = input('Enter your value')
    PTRATIO = input('Enter your value')
    B = input('Enter your value')
    LSTAT = input('Enter your value')
    
    # fethcing the result
    
    final_result = ''
    
    final_result = price_pred([CRIM, ZN, INDUS, CHAS, NOX, RM, AGE, DIS, RAD, TAX, PTRATIO, B, LSTAT])
        
    print(final_result)

And this code may seem different from my first code, but I tried the exact same code with streamlit (function) and the issue persists.

Hi,

Thank you(jkneer) for your time. The issue has been solved. There was a problem with the indentation of the code which I was unaware of and I think it is a ‘Python thingy’.

Thank you again, my friend! :slight_smile:``