Forums

TensorFlow ends up taking more than 5 minutes causing request timeout

I have a Flask API that serves a TensorFlow model on a POST request ("/predict"). However, when I call this POST request from the frontend, the request ends up taking more than 5 minutes and times out, resulting in a 504 error.

I understand completely why there is a limit on requests of 5 minutes, but what I don't understand is why my code ends up taking 5 minutes on the PythonAnywhere server wheras on my local machine, it takes at most 1 minute.

After deubbing, I have identified that the last line of code to be executed is the TensorFlow line in my code:

tf.io.read_file(path_to_img)

This apparently ends up taking 5 minutes. However, on my system, this takes no more than 30 seconds.

This line basically reads in a PNG image by downloading it.

What could be wrong with this?

You're on a free account, so that may be timing out because the image is not available to download because of our proxy and whitelist for free accounts: https://help.pythonanywhere.com/pages/403ForbiddenError/

Also take note of our warning about using tensorflow in a web app: https://help.pythonanywhere.com/pages/MachineLearningInWebsiteCode/

Thanks! So even if I'm using my Flask app as a backend with TensorFlow code in it, it will cause errors? Thus, I should just use my frontend part of the app on PythonAnywhere?

The answer to both of your questions is "yes". It doesn't matter what web framework you use, tensorflow will not work well in a web app (if it works at all).

Great, thanks!