Original article was published by Piyush Maheshwari on Deep Learning on Medium
5. Deploying the models through API
When training the models with Monk, it automatically creates a workspace directory. It contains all the training logs and all the intermediate models obtained during training. For developing the API, we just need the final models with the same directory structure that they were created in. If you have trained your models, you can use them. Otherwise, you can download the workspace with the final models from here.
If you just want to test the API, check this. The readme file has detailed instructions to set up the environment.
Create a sub-directory named ‘uploads’ in the project directory, wherein the files uploaded by the user would be saved before returning the predictions. Set up a virtual environment and install the required libraries. A virtual environment is not necessary, but is recommended. Download the requirements.txt file from here and run
pip install -r requirements.txt
Create a file named app.py in the project directory where the workspace is located. We’ll write the code for the API in this file.
Imports and utility functions:
Functions to respond to HTTP requests:
I have included a user interface for the API, but it is not necessary. If you wish use it as well, download this and this directory into the project directory, and then define the index() function given below to make the API load the webpage when accessed through a browser.
The upload() function responds to the POST requests at ‘/predict’, saves it to a subdirectory named ‘uploads’ and returns a string with the predictions if the file is a valid image.
It loads the models and starts the server.