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?
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?
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.
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:
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.
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.
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')
@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?
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.
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 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.
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 Thank youu!!
Yes, i have ensured all of this already. work is running completely before download
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