Unable to call a function inside streamlit app and print its output

The code:-

if st.button('Predict Churn'):
    # Prepare the data for the model
    data = pd.DataFrame({
        'Age': [10],  # Use a list with a single value
        'Gender': ['Male'],  # Use a list with a single value
        'Location': ['Los Angeles'],  # Use a list with a single value
        'Subscription_Length_Months': [5],  # Use a list with a single value
        'Monthly_Bill': [100],  # Use a list with a single value
        'Total_Usage_GB': [100]  # Use a list with a single value
    })

    # Calling prediction pipeline to predict the output
    predictor = ModelPrediction()
    x = predictor.prediction(data)


    # Return the prediction
    st.subheader('Churn Prediction:')
    if x < 0.5:
        st.write('The customer is likely to stay (Churn: No)')
    else:
        st.write('The customer is likely to churn (Churn: Yes)')

The function i am calling will load a model and then input the given data to get an output, but i am when i do so it gives many error, i tried running it outside streamlit app and it works fine please hel.

this is the error

CustomException: Error occurred python script name [C:\D_Drive\WORKS\Github_projects\customer_churn\src\pipelines\predict_pipeline.py] line number [21] error message [Error occurred python script name [C:\D_Drive\WORKS\Github_projects\customer_churn\src\utils\main_utils.py] line number [67] error message [[Errno 2] No such file or directory: โ€˜โ€ฆ\โ€ฆ\artifacts\model.pklโ€™]]

Traceback:

File "C:\Users\SAMAR\anaconda3\envs\CCP\Lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 552, in _run_script
    exec(code, module.__dict__)File "C:\D_Drive\WORKS\Github_projects\customer_churn\app.py", line 133, in <module>
    x = predictor.prediction(data)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^File "C:\D_Drive\WORKS\Github_projects\customer_churn\src\pipelines\predict_pipeline.py", line 54, in prediction
    raise CustomException(e, sys) #type:ignore
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Hey @Samarjeet,

Can you share a link to the GitHub repo for this app so we can run your code/try to reproduce the error?

Sure, here it is

https://github.com/Samarjeet-singh-chhabra/Customer_Churn_Prediction

Hey @Samarjeet,

It seems like the path youโ€™re storing in model_pkl_path isnโ€™t the correct path to the model.pkl file โ€“ I was able to resolve the error by setting model_pkl_path to be the full path to the model.pkl file (in my case, just my local path to where the app is stored on my computer + \Customer_Churn_Prediction/artifacts/model.pkl).

Iโ€™d recommend printing the value of model_pkl_path and playing around until the generated path matches what youโ€™d expect given the location of the file. You could still generate the filepath rather than hard-coding it, but the path generated when I ran the app was ../../artifacts/model.pkl, which I think would take you to the parent directory above where the Streamlit app is running, and then the parent directory above that, and artifacts wouldnโ€™t be in that folder.

Hope that makes sense! Let me know if you have any questions

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.