Sometimes you want to serve a simple webservice from a GPU container. Maybe you want to test out a new model you have trained (not hodog?!) or want to serve a simple visualization of your machine learning model. This repo shows you how to make a simple flask app using Paperspace's Gradient°.
Things to note when running flask:
- You need to have the flask server listen on all interfaces by specifying
app.run(host="0.0.0.0")
for the interface to listen on. - You need to install the
flask
andflask_restful
packages as part of running the job if it is not already part of the container. (Theflask_restful
package optional if you are not using its functionality in your flask server.) The example here shows both these actions.
Also note, flask by default does not provide SSL/TLS security. See the flask package documentation for options on how to secure flask. Here is an article on running your flask-application over https.
paperspace jobs create --machineType P5000 --container Test-Container --ports 5000:5000 --command 'pip install flask flask_restful && python myapp.py' --workspace https://github.com/Paperspace/gradient-flask-example.git
or alternatively you can pull down this repo and run from your local machine:
> git clone https://github.com/Paperspace/gradient-flask-example.git && cd gradient-flask-example
> paperspace project init
> paperspace jobs create --machineType P5000 --container Test-Container --ports 5000:5000 --command 'pip install flask flask_restful && python myapp.py'
Your flask app is now running on the Gradient job container. Grab the public IP address of the container from the Gradient jobs console or from the CLI.
Once you have found your public IP you can hit the web service from a command line or from a web browser. Try it out!
curl http://35.230.20.216:5000/api?key=hellothere
And the output: {"key": "hellothere"}