I keep getting this attribute error with Voila running locally. I am running the notebook in Voila with a requirements.txt file, image file, and an export.pkl file as well. I’m using Python version 3.7.8 and have the packages installed in a Conda environment.
AttributeError Traceback (most recent call last)
in
18 # Code that is run and does something
19 defaults.device = torch.device(‘cpu’)
—> 20 learn = load_learner(".")
21
22 # Notes
~/anaconda3/envs/fastai37/lib/python3.7/site-packages/fastai/basic_train.py in load_learner(path, file, test, tfm_y, **db_kwargs)
619 “Load a Learner
object saved with export_state
in path/file
with empty data, optionally add test
and load on cpu
. file
can be file-like (file or buffer)”
620 source = Path(path)/file if is_pathlike(file) else file
–> 621 state = torch.load(source, map_location=‘cpu’) if defaults.device == torch.device(‘cpu’) else torch.load(source)
622 model = state.pop(‘model’)
623 src = LabelLists.load_state(path, state.pop(‘data’))
~/anaconda3/envs/fastai37/lib/python3.7/site-packages/torch/serialization.py in load(f, map_location, pickle_module, **pickle_load_args)
525 with _open_file_like(f, ‘rb’) as opened_file:
526 if _is_zipfile(opened_file):
–> 527 with _open_zipfile_reader(f) as opened_zipfile:
528 return _load(opened_zipfile, map_location, pickle_module, **pickle_load_args)
529 return _legacy_load(opened_file, map_location, pickle_module, **pickle_load_args)
~/anaconda3/envs/fastai37/lib/python3.7/site-packages/torch/serialization.py in init(self, name_or_buffer)
222 class _open_zipfile_reader(_opener):
223 def init(self, name_or_buffer):
–> 224 super(_open_zipfile_reader, self).init(torch._C.PyTorchFileReader(name_or_buffer))
225
226
AttributeError: ‘PosixPath’ object has no attribute ‘tell’
AttributeError: ‘PosixPath’ object has no attribute ‘tell’
Here’s my ipynb code:
%matplotlib inline
from IPython.display import clear_output, Image
from matplotlib.pyplot import imshow, axis, show
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from ipywidgets import interact, interactive, fixed, interact_manual, Layout
import ipywidgets as widgets
import re
from fastai.vision import *
import urllib.request
import warnings
# Code that is run and does something
defaults.device = torch.device('cpu')
learn = load_learner(".")
# Notes
# This webapp may be vulnerable to large files that are downloaded from the URLs
# or from direct file uploads. I'm not sure how to handle this.
# Widget for receiving an image link
link1 = widgets.Text(
description = "Image link: ",
placeholder = "Type image link here"
)
# Widget that downloads the image when clicked
download1 = widgets.Button(
description = 'Download!',
disabled = False,
button_style = '', # 'success', 'info', 'warning', 'danger' or ''
tooltip = 'Download the image supplied by the link',
)
def dl_img(event):
with d1_out:
d1_out.clear_output()
# Save the image to 'image.png'
bla = link1
# Check that input is a string
if(type(bla.value) is not str):
print("Error, link must be a string!")
# Check that input link leads to an image
elif(re.search('\.((png)|(jpg))', bla.value) is None):
print("URL must link to a png or jpg file!")
else:
try: # Check that there is an image at the link
urllib.request.urlretrieve(bla.value, 'image.png')
print('Image downloaded!')
# Show the image (assumes that there is a file)
try:
file = open("./image.png", "rb")
image1 = file.read()
except:
file = open("./default.png", "rb")
image1 = file.read()
finally:
# This isn't showing up for some reason
img_out = widgets.HBox([widgets.Image(value = image1,
format = 'url')])
except: # If the image in the link is not found
print("image not found :(")
download1.on_click(dl_img)
# Widget that classifies the image when clicked
classify1 = widgets.Button(
description = 'Classify!',
disabled = False,
button_style = '', # 'success', 'info', 'warning', 'danger' or ''
tooltip = 'Classify the image as "draft" or "not draft"',
)
def classify_img(event):
with c1_out:
c1_out.clear_output()
try:
img = open_image(Path('./image.png'))
except:
img = open_image(Path('./default.png'))
print("Image not found, default loaded instead")
pred_class,pred_idx,outputs = learn.predict(img)
print('Class: ' + pred_class.obj)
with warnings.catch_warnings():
# I'm hoping that this only ignores warnings from the classify
# button because I'm getting warnings about upsampling from pytorch.
# I tried, but couldn't find a way to test if warnings from other parts
# of the code are suppressed.
warnings.filterwarnings('ignore')
classify1.on_click(classify_img)
# Widget that shows the downloaded image
img1 = widgets.Button(
description = 'Show image',
disabled = False,
button_style = '', # 'success', 'info', 'warning', 'danger' or ''
tooltip = 'View the image',
)
def view_img(event):
with img_out:
img_out.clear_output(True)
try:
img = mpimg.imread('image.png')
except:
print("Image not found, default loaded instead")
img = mpimg.imread('default.png')
imgplot = plt.imshow(img)
plt.axis('off')
plt.show()
img1.on_click(view_img)
## Output widgets
# Show the link that's being received
def f1(link):
print(' Link received: {}'.format(link))
out = widgets.interactive_output(f1, {'link': link1})
# Print statement that confirms the download
d1_out = widgets.Output(layout = Layout(margin='auto'))
# Print statement that shows the classification
c1_out = widgets.Output(layout = Layout(margin='auto'))
# Print image
img_out = widgets.Output(layout = Layout(margin = 'auto'))
# Layout of all the widgets
widgets.VBox([
widgets.HBox([widgets.VBox([link1, download1, classify1]),
widgets.VBox([out, d1_out, c1_out])]),
widgets.HBox([img1, img_out])
])
Any tips?