Skip to content

Commit 9e794dd

Browse files
committed
Add part 3 and some other updates
1 parent 7acdc21 commit 9e794dd

File tree

19 files changed

+143
-2
lines changed

19 files changed

+143
-2
lines changed

2-docker-nginx-gunicorn/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Flask ML API with Docker, Gunicorn WSGI and Nginx
22
Building upon the first phase, we dockerize the flask app, and use Gunicorn WSGI
33
to achieve a much more stable and production-ready Flask API. We also use Nginx
4-
to interface with
4+
to interface with Gunicorn.
55

66
The model is developed in [this tutorial](https://medium.com/technonerds/using-fastais-ulmfit-to-make-a-state-of-the-art-multi-label-text-classifier-bf54e2943e83).
77

2-docker-nginx-gunicorn/docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ services:
1515
- "5000:5000"
1616

1717
nginx:
18-
container_name: nginx_1
18+
container_name: nginx
1919
restart: always
2020
build: ./nginx
2121
networks:
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Managing multiple endpoints on Flask ML API
2+
Adding multiple endpoints to the project in part 2 would make the `app.py` file very large and unorganized.
3+
So, we can use Blueprints - a built in Flask tool to organize endpoints into separate files.
4+
5+
In part 2, we dockerized the flask API and we use Gunicorn WSGI
6+
to achieve a much more stable and production-ready Flask API.
7+
We also use Nginx to interface with Gunicorn.
8+
9+
The model is developed in [this tutorial](https://medium.com/technonerds/using-fastais-ulmfit-to-make-a-state-of-the-art-multi-label-text-classifier-bf54e2943e83).
10+
11+
## Anatomy
12+
* Gunicorn:
13+
* Gunicorn is a popular WSGI that works seamlessly with Flask.
14+
* Flask needs a Web Server Gateway Interface (WSGI) to talk to a web server.
15+
* Flask's built-in WSGI is not capable of handling production APIs, because it lacks security features and can only run one worker.
16+
* In this project, Gunicorn will start automatically in the api Docker container with the following config (see Dockerfile):
17+
```
18+
["gunicorn", "-w", "3", "-b", ":5000", "-t", "360", "--reload", "api.wsgi:app"]
19+
```
20+
* Nginx:
21+
* Nginx is a popular webserver platform. It is used to interface with gunicorn (on port 5000) and relay it to port 80.
22+
* Docker:
23+
* Docker and docker-compose allow apps to be easily deployed.
24+
* Using docker, we containerize our API to work independently of the environment
25+
26+
## How to run
27+
* Install `docker` and `docker-compose` for your specific platform (you can easily find instructions on Google)
28+
* Start the API using:
29+
```
30+
docker-compose build
31+
docker-compose up
32+
```
33+
This will build and spin up a virtual container with the API.
34+
* Building the continer for the first time will take a while, as it needs to install all requirements
35+
* You can use `0.0.0.0:80` to interface with it.
36+
* You can now query `GET 0.0.0.0/classification` using [Postman](https://www.getpostman.com/) with a json body:
37+
```
38+
{
39+
"text": "Your classification text goes here"
40+
}
41+
```
42+
It will return the associated category.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FROM python:3.6
2+
3+
#update
4+
RUN apt-get update
5+
6+
#install requirements
7+
COPY ./requirements.txt /tmp/requirements.txt
8+
WORKDIR /tmp
9+
RUN pip3 install -r requirements.txt
10+
11+
#copy app
12+
COPY . /api
13+
WORKDIR /
14+
15+
CMD ["gunicorn", "-w", "3", "-b", ":5000", "-t", "360", "--reload", "api.wsgi:app"]

3-multiple-endpoints-blueprints/api/__init__.py

Whitespace-only changes.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from flask import Flask
2+
from .endpoints.classification import classification_api
3+
4+
app = Flask(__name__)
5+
app.register_blueprint(classification_api)
6+
7+
8+
if __name__ == '__main__':
9+
app.run(host='0.0.0.0')

3-multiple-endpoints-blueprints/api/endpoints/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)