AttributeError: 'NoneType' object has no attribute 'update'

Trying to create a web app using streamlit and fastai, I get the following error when I click on “classify.”

***AttributeError: 'NoneType' object has no attribute 'update'***

Weirdly enough, I tried the exact same method last year with no trouble. Could anyone suggest a solution.

Here is the traceback below:

File "/usr/local/lib/python3.7/dist-packages/streamlit/script_runner.py", line 430, in _run_script
    exec(code, module.__dict__)
File "/content/app.py", line 45, in <module>
    predictor = Predict(file_name)
File "/content/app.py", line 21, in __init__
    self.get_prediction()
File "/content/app.py", line 36, in get_prediction
    pred, pred_idx, probs = self.learn_inference.predict(self.img)
File "/usr/local/lib/python3.7/dist-packages/fastai/learner.py", line 266, in predict
    inp,preds,_,dec_preds = self.get_preds(dl=dl, with_input=True, with_decoded=True)
File "/usr/local/lib/python3.7/dist-packages/fastai/learner.py", line 253, in get_preds
    self._do_epoch_validate(dl=dl)
File "/usr/local/lib/python3.7/dist-packages/fastai/learner.py", line 203, in _do_epoch_validate
    with torch.no_grad(): self._with_events(self.all_batches, 'validate', CancelValidException)
File "/usr/local/lib/python3.7/dist-packages/fastai/learner.py", line 163, in _with_events
    try: self(f'before_{event_type}');  f()
File "/usr/local/lib/python3.7/dist-packages/fastai/learner.py", line 141, in __call__
    def __call__(self, event_name): L(event_name).map(self._call_one)
File "/usr/local/lib/python3.7/dist-packages/fastcore/foundation.py", line 155, in map
    def map(self, f, *args, gen=False, **kwargs): return self._new(map_ex(self, f, *args, gen=gen, **kwargs))
File "/usr/local/lib/python3.7/dist-packages/fastcore/basics.py", line 698, in map_ex
    return list(res)
File "/usr/local/lib/python3.7/dist-packages/fastcore/basics.py", line 683, in __call__
    return self.func(*fargs, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/fastai/learner.py", line 145, in _call_one
    for cb in self.cbs.sorted('order'): cb(event_name)
File "/usr/local/lib/python3.7/dist-packages/fastai/callback/core.py", line 45, in __call__
    if self.run and _run: res = getattr(self, event_name, noop)()
File "/usr/local/lib/python3.7/dist-packages/fastai/callback/progress.py", line 26, in before_validate
    def before_validate(self): self._launch_pbar()
File "/usr/local/lib/python3.7/dist-packages/fastai/callback/progress.py", line 35, in _launch_pbar
    self.pbar.update(0)
File "/usr/local/lib/python3.7/dist-packages/fastprogress/fastprogress.py", line 56, in update
    self.update_bar(0)
File "/usr/local/lib/python3.7/dist-packages/fastprogress/fastprogress.py", line 76, in update_bar
    else: self.on_update(val, f'{100 * val/self.total:.2f}% [{val}/{self.total} {elapsed_t}<{remaining_t}{end}]')
File "/usr/local/lib/python3.7/dist-packages/fastprogress/fastprogress.py", line 127, in on_update
    if self.display: self.out.update(HTML(self.progress))

Hi @Boukman -

Posting the code that generated this error would be helpful, as the traceback appears to be referring to one of the packages you are using.

Best,
Randy

Hi Randy, Thank you for the follow-up.

Here it is below:

%%writefile app.py
from fastai.vision.widgets import *
from fastai.vision.all import *

from pathlib import Path
import streamlit as st
import numpy as np
from skimage.io import imread
from skimage.transform import resize
import pickle
import PIL.Image
st.title('Image Classifier')
st.text('upload the image')
path = Path()
learn_inf = load_learner(path/'sclassifier.pkl')
class Predict:
    def __init__(self, filename):
        self.learn_inference = load_learner(Path()/filename)
        self.img = self.get_image_from_upload()
        if self.img is not None:
            self.display_output()
            self.get_prediction()
    
    @staticmethod
    def get_image_from_upload():
        uploaded_file = st.file_uploader("Upload Files",type=['png','jpeg', 'jpg'])
        if uploaded_file is not None:
            return PILImage.create((uploaded_file))
        return None

    def display_output(self):
        st.image(self.img.to_thumb(500,500), caption='Uploaded Image')

    def get_prediction(self):

        if st.button('Classify'):
            pred, pred_idx, probs = self.learn_inference.predict(self.img)
            st.write(f'Prediction: {pred}; Probability: {probs[pred_idx]:.04f}')
        else: 
            st.write(f'Click the button to classify') 

if __name__=='__main__':

    file_name='sclassifier.pkl'

    predictor = Predict(file_name)

@randyzwitch Is that enough information?

Here is a bit more information @randyzwitch

When I run the classifier within Google Colab it works fine and makes the correct predictions.

The problem starts when I try to run the classifier on a browser using streamlit and ngrok. As soon as I click ‘classify’ I get the long error message posted above.

Below in the picture files is all of the code I use in Google Colab (in order) after successfully testing the classifier and before I get the error when I try using it in a web browser. In fact here, I’ll include the link to the classifier so you can test it out for yourself and see the error in realtime, although Colab may shut it down at any point so might not be running when you read this.

http://6f6a-35-247-61-15.ngrok.io/





Unfortunately, I don’t have any experience trying to run Streamlit from a notebook and using ngrok.

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