Monday, June 6, 2022
HomeData ScienceThe best way to Construct REST API in Easy Phrases | by...

The best way to Construct REST API in Easy Phrases | by Pedram Ataee, PhD | Jun, 2022


A playbook for making a REST API as a knowledge scientist utilizing Flask and Docker

Photograph by Huper by Joshua Earle on Unsplash

I not too long ago developed a contextualized most-similar phrase API service named OWL. The OWL API is a complicated NLP service that’s developed utilizing Flask and Docker. This service is shared with the group by a collection of REST APIs hosted on the RapidAPI market. It was a fantastic journey for me so I made a decision to share varied phases of its growth with you.

This text describes a step-by-step guideline on the right way to construct a REST API utilizing Flask and Docker. You need to use this guideline in your information science initiatives in addition to every other net functions that you simply work on. Plus, this text is a part of the “… in Easy Phrases” collection the place I share my expertise with you. This collection helped many information science lovers earlier than. I hope it helps you as properly.

If you wish to learn extra in regards to the OWL API, take a look at the article under.

Step 1 — Create an app.py

First, you could write a Python script that maps a RESTful endpoint to a operate. Let’s identify the script app.py. On this instance, the RESTful endpoint is /common/<N>/<WORD> the place common is a continuing and N and WORD are two variables. By way of these variables, customers can ship information to the corresponding operate of the RESTful endpoint, i.e., similar_words(N, WORD). The RESTful endpoint that you simply outline within the Python script might be added to the URL of the host after deploying on a server.

import os
import sys
from flask import Flask, jsonify, request
from flask_httpauth import HTTPBasicAuth
import gensim.downloader as api
sys.path += [os.getcwd()]
from src.core.word_clusterizer import WordClusterizer
from src.backend.wrapper import GensimWrapper
auth = HTTPBasicAuth()
app = Flask(__name__)
MODEL = api.load('glove-wiki-gigaword-300')@app.route('/common/<N>/<WORD>', strategies=['GET'])
def similar_words(N, WORD):
mannequin = GensimWrapper(MODEL)
strive:
wc = WordClusterizer(mannequin=mannequin, phrase=WORD, top_n=int(N)
wc.match()
outcomes = wc.outcome
besides KeyError:
outcomes = 'Nothing is discovered!'
return jsonify(outcomes)
if __name__ == '__main__':
port = os.environ.get("PORT", 8080)
app.run(host='0.0.0.0', port=port, debug=True)

Evidently, the corresponding operate of the RESTful endpoint is the core of your information science mission. On this situation, it’s a phrase similarity service that extracts probably the most comparable phrases with extra granularity in comparison with the present options.

Step 2 — Create a Dockerfile

You want a Docker picture that runs app.py while you spin up a server. To construct such a Docker picture, you want a Dockerfile equivalent to under.

# MAINTAINER YOUR_NAMEFROM pdrm83/nlp-essentials:newestRUN pip set up --upgrade pip
COPY . /mission/
RUN pip set up .
WORKDIR /mission/src/.
ENV PYTHONPATH /mission
ENV PORT 8080
EXPOSE 8080
EXPOSE 80
CMD ["python3", "app/app.py"]

Within the Dockerfile, you could open/expose the port the place you set app.py working. As you’ll be able to see in Step 1, I set the app.pyscript to run on a port outlined as port = os.environ.get("PORT", 8080) . This line means, the port variable is ready to the worth of PORT from the surroundings, whether it is set, in any other case, it’s set to 8080.

On this case, PORT was not beforehand set. So, I had to verify to open port 8080 within the Dockerfile. I did that by EXPOSE 8080 within the Dockerfile. If PORT was beforehand set within the surroundings to something besides 8080, you could open that port within the Dockerfile by including EXPOSE XXXX.

Word that, I run the CMD ["python3", "app/app.py"] on an surroundings created by the Docker picture named nlp-essentials . This can be a Docker picture that I constructed beforehand by putting in all of the required libraries. This Docker picture is saved in my Dockerhub account pdrm83 and used as wanted. The nlp-essentials picture is pulled at any time when I need to replace the API service. This helps to not set up all of the required libraries each time that I need to replace the API. Final, however not least, since I saved the app.py in ROOT/src/app/. , I added WORKDIR /mission/src/. to the Dockerfile. You could not replicate this line. You simply have to configure it correctly within the Dockerfile primarily based on the file construction of your codebase.

Step 3 — Spin up a server

In nutshell, you could construct a docker picture utilizing docker construct -f <<DOCKERFILE_NAME>> -t <<IMAGE_NAME>> , after which run it utilizing docker run -i -p 80:8080 <<IMAGE_ID>>. The IMAGE_ID is the id of the Docker picture that you simply created within the earlier step. Word that, I map port 8080 to 80 because the app.py runs on port 8080 and I need to provide the API service on port 80 to the customers. Ultimately, you could be sure that port 80 is open to the web on any machine that you simply work with.

There are a lot of methods to let customers have entry to your companies. Nevertheless, I need to introduce you to a cool service named ngrok that permits you to make your laptop grow to be on-line in 3 easy steps 😮. The three steps are as simple as follows:

  1. Obtain ngrok.zip and unzip /path/to/ngrok.zip .
  2. Run ngrok authtoken YOUR_OWN_TOKEN
  3. Run ngrok http 80

After working ngrok http 80 , the ngrok service creates a devoted URL on ngrok.io area to your mission that appears just like the hyperlink under.

http://YOUR_DEDICATED_SUBDOMAIN.ngrok.io/

Your service is now accessible on the web for everybody. You don’t want any fancy cloud service to introduce your API service to the market. Isn’t that tremendous? You’ll be able to learn within the article under why I feel ngrok could be very helpful so that you can provide an API service to the group.

Final Phrases

One of the vital really useful architectures for software program merchandise is API Structure. This structure helps you design modular and reusable companies which can be additionally simple to keep up. For instance, there are efficient options to check and doc a collection of REST APIs that allow you to to keep up them simply. On this article, I described the right way to construct a RESTful API to your information science mission. There are different varieties of API protocols fairly than RESTful which was not the main focus of this text.

Thanks for Studying!

In the event you like this submit and need to help me…

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments