Model not saving

Yes, i was thinking the same. So this is my new code:

@anvil.server.callable
def download():
    if m is not None:
        saved = m.save('my_model.h5', save_format='h5')  
        time.sleep(0.25)
        import anvil.media
        media_obj = anvil.media.from_file('my_model.h5') 
        return media_obj # construct and return a media object
    else:
        raise Exception('model is not ready to download')

Getting this error:

Exception: model is not ready to download
at <ipython-input-14-b02b372b89f5>, line 55
  called from Form1, line 29

Is it possible that maybe ‘my_model.h5’ is not getting saved as a media file? should i convert it to bytes and then try converting into media file?

By the way, the model still got saved in my PC.

Print the value of m before the IF statement. It appears to be None still. Your new code does nothing to prevent this.

Sure, but it seems like you are not even getting inside the IF statement yet. Once you do you could certainly try to debug further if you have to and report back what you have learned?

1 Like

Yes, it looks like the global value of the variable m is still None.

hirasharif1998, you posted above some of your code, it looks like you haven’t generated a value for m in the control flow of the script being run for the first time, so maybe create a button on your anvil project to run one of the functions that will generate a new model for the variable m to save. You want the client on the anvil page to generate a new model, correct?

If you have a ‘my_model.h5’ on your system, it might have been generated if you tested it by running it yourself, and not through anvil?

More things to look out for, if you model takes longer than a minute or two to generate, the anvil system might time-out on the free plan. If it is fast, you could just put that server call to the model generating function in the button_2_click button code.

The value of M is still None because you are setting it globally to None in your code, and like campopianoa said, the if statement of

if m is not None:

is False, so it is going to the else: statement and raising your custom ‘model is not ready to download’ exception.

1 Like

@alcampopiano @ianbuywise Thank you for your response.

I’m sure that the model is being saved in my PC because i recheck every time i press the “download” button. Then i delete the model to see if it saves again on the next click. And yes it gets saved again every time.

“m” is not None. I assured this by printing m. You can see it here:

Are you still hitting the exception “model is not ready to download”?

Yes i am still getting the same error message.

Then m must still be None, right?

No, i mentioned in my previous post that m is not None. I also printed m to see if it is none. But "tensorflow model… " got printed i.e. m is not None.

Well something strange is definitely happening.

I would add a bunch more print statements to help you debug this. The only way that code path can be taken is if m is None. So when it gets called from the client it is None otherwise the else statements would never execute.

Are you checking the value of m by running that cell from jupyter or from the client code button click? If it’s the former then check the print statements when running the code from the anvil button click.

Is it possible you have multiple functions registered called download? Maybe you have multiple jupyter notebooks open. Or perhaps when you call that function from anvil you’ve reset the jupyter cells and m has changed.

When you have uplink server code running like this, it’s worth setting it up, then interacting with the server code from your app (exclusively) and check the logs in your jupyter notebook. Since that’s how your users will be working with the server code.

You might also add another server function for sanity, with an extra button in your app to call the function from client code.

### server
@anvil.server.callable
def check_m():
   print('checking m', m) # is m None?


### client code for another button 
def button_2_click(self, **event_args):
    anvil.server.call('check_m')

You may already be doing much of the above. But there is probably something that’s missing between our explanations and how you’re interacting with anvil and the jupyter notebook.


In summary - more print statements and try to exclusively interact with your notebook from the anvil app. (Write the server code, then run server calls from button clicks and check the jupyter notebook for the debug print statements)

Good luck!

You may also want to watch the jupyter notebook tutorial if you haven’t already.

2 Likes

Where is m passed to the download function (or declared as Global)? Has this been missed as the function declaration shows no instance of m being passed or created? In this case the if statement always evalutate m is None so carry out else ? I don’t know how it would print m in that case?? I am not familiar with Jupyter so may be misunderstanding something here…

@anvil.server.callable
def download():
    if m is not None:
        saved = m.save('my_model.h5', save_format='h5')  
        time.sleep(0.25)
        import anvil.media
        media_obj = anvil.media.from_file('my_model.h5') 
        return media_obj # construct and return a media object
    else:
        raise Exception('model is not ready to download')

@rickhurlbatt has entered the game :video_game:

@hirasharif1998 defined a global variable m in server code
See this post for more context.

1 Like

Hmmm thanks @stucork

@hirasharif1998 stupid question but is the function work (which modifies m from None) definitely being run before the function download? And is there a check to ensure that work is completing before download runs?

…Curiouser and curiouser

1 Like

Yes, I also don’t use Jupyter notebook and really don’t know how it completely works, and totally missed that part at the beginning of the question, sorry. I barely use IDLE. :see_no_evil:
My commodore 64 did not have such things when I was 8, I guess I’m still not used to anything that isn’t started from scratch every time from a file or memory.

From the client code button click. On click of the “download” button, this function gets called from the jupyter notebook. My notebook itself is not printing anything as i have connected EVERY function call with ANVIL buttons.

I do not run multiple notebooks in one time. But yes i do have other notebooks which are very similar to this one, with a very little difference in the code. Maybe this could be the reason, so i will definitely try changing the names of my functions in this notebook and then check it.

This is the first tutorial i saw and got to know about anvil. I found it really interesting how a simple python code could be turned into a website. After this, i decided to build this project :smile: This project is exactly same as the tutorial with a slight difference, which is, i’m trying to let user TRAIN, TEST and DOWNLOAD the whole machine learning model.

I changed my function names and tried the code you provided:


Training was successfull. But on the click of “download” button, it was just loading and loading for really long time. After 15 minutes FINALLY i got the option to choose where i want to save the model :smiley: Thank youu!!

1 Like

@rickhurlbatt @ianbuywise Thank you for your response!

Yes, i have ensured all of this already. work is running completely before download :blush:

It works EXACTLY SAME as the anvil server. The only difference is that jupyter notebook has “cells” (text areas) where we write our code while anvil server has the one big area where we write the code. The code is written in the SAME manner in both of them :blush:

From various podcasts, I understand that

  1. the cells in a notebook can be executed in any order.
  2. executing a def defines a function.

This makes me wonder: if you change the source code for a function, do you have to re-run the cell it’s in, before the change takes effect?

Yes indeed.

1 Like

Yes, exactly. You have to re-run the cells.

That makes sense.

:thinking: It also means one more manual step after editing, before that change is visible (at run time) to @anvil.server.callable. :frowning_face: