Deploying a REST API¶
Anaconda Enterprise enables you to deploy your machine learning or predictive models as a REST API endpoint so others can query and consume results from them. REST APIs are web server endpoints, or callable URLs, which provide results based on a query, allowing developers to create applications that programmatically query and consume them via other user interfaces or applications.
Rather than sharing your model with other data scientists and having them run it, you can give them an endpoint to query the model, which you can continue to update, improve and redeploy as needed.
REST API endpoints deployed with Anaconda Enterprise are secure and only accessible to users that you’ve shared the deployment with or users that have generated a token that can be used to query the REST API endpoint outside of Anaconda Enterprise.
The process of deploying a REST API involves the following steps:
Create a project to encapsulate all of the components necessary to use or run your model.
Deploy the project with the
rest_api
command (shown in Step 4 below) to build the software dependencies—all of the libraries required for it to run—and encapsulate them so they are completely self-contained.Share the deployment, or the URL of the endpoint, and generate a unique token so that they can connect to the deployment and use it from within notebooks, APIs or other applications.
Using the API wrapper¶
As an alternative to using the REST API wrapper provided with Anaconda Enterprise, you can construct an API endpoint using any web framework and serve the endpoint on port 8086
within your deployment, to make it available as a secure REST API endpoint in Anaconda Enterprise.
Follow this process to wrap your code with an API:
Open the Jupyter Notebook and add this code to be able to handle HTTP requests. Define a global REQUEST JSON string that will be replaced on each invocation of the API.
Import the Anaconda Enterprise
publish
function.Add the deployment command and an appropriate environment variable to the
anaconda-project.yml
file:Ensure the
anaconda-enterprise
channel is listed underchannels:
and theanaconda-enterprise-web-publisher
package is listed underpackages:
. For example:Use the following command to test the API within your notebook session (without deploying it):
Now if you visit
http://localhost:8888/{function}
from within a notebook session you will see the results of your function.From within a notebook session, execute the following command:
Click the Deploy icon in the toolbar to deploy the project as an API.
This deploys the notebook as an API which you can then query.
To query externally, create a token and find the url to the running project.
Example using
curl
:The
-L
option tells curl to follow redirects. The-H
adds a header. In this case-H
adds the token required to authorize the client to visit that URL.If you deploy the project as described above you can add the
-X POST
option tocurl
to access that function.