Skip to content

Commit

Permalink
complete
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeyk committed Apr 16, 2021
1 parent 1dc63fd commit 45326bb
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ logs
.mypy_cache
notebooks/lightning_logs
lightning_logs/
lab9/requirements.txt
2 changes: 1 addition & 1 deletion lab9/api_server/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ RUN set -ex && mkdir /repo
WORKDIR /repo

# Install Python dependencies
COPY requirements/prod.txt ./requirements.txt
COPY requirements.txt ./requirements.txt
RUN sed -i 's/cu101/cpu/' requirements.txt
RUN pip install --upgrade pip~=21.0.0
RUN pip install -r requirements.txt
Expand Down
2 changes: 1 addition & 1 deletion lab9/api_serverless/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
FROM amazon/aws-lambda-python:3.6

# Install Python dependencies
COPY requirements/prod.txt ./requirements.txt
COPY requirements.txt ./requirements.txt
RUN sed -i 's/cu101/cpu/' requirements.txt
RUN pip install --upgrade pip~=21.0.0
RUN pip install -r requirements.txt
Expand Down
23 changes: 21 additions & 2 deletions lab9/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
- First, we speed up our ParagraphTextRecognizer model with TorchScript
- Next, we wrap the model in a web app, and send it some requests
- We package up the web app and model as a Docker container, and run it that way
- Lastly, we deploy the web app to production using AWS Lambda
- Lastly, we prepare to deploy as a serverless function using AWS Lambda, getting it working locally

## Follow along

Expand Down Expand Up @@ -41,9 +41,23 @@ text_recognizer/
training/
```

## First, TorchScript

To speed up inference, we can compile our trained PyTorch model using TorchScript.

Check out the minimal changes in `text_recognizer/paragraph_text_recognizer.py`:

```python
self.scripted_model = self.lit_model.to_torchscript(method="script", file_path=None)
...
y_pred = self.scripted_model(image_tensor.unsqueeze(axis=0))[0]
```

The conversion takes a few seconds, but then all inference calls are significantly sped up.

## Serving predictions from a web server

First, we will get a Flask web server up and running and serving predictions.
Now, let's get a Flask web server up and running and serving predictions.

Flask is a standard Python library for web servers.
If we were actually deploying to production using this method, we would use FastAPI, which works almost exactly the same but is more modern and is faster at responding to requests.
Expand Down Expand Up @@ -101,6 +115,7 @@ First off, if you don't already have `docker` installed on your system, do so: h
Still in the `lab9` directory, run:

```sh
cp ../requirements/prod.txt requirements.txt
docker build -t text-recognizer/api-server -f api_server/Dockerfile .
```

Expand Down Expand Up @@ -136,6 +151,7 @@ Check out `api_serverless/app.py` and `api_serverless/Dockerfile`.
We can build the container with

```sh
cp ../requirements/prod.txt requirements.txt
docker build -t text-recognizer/api-serverless -f api_serverless/Dockerfile .
```

Expand All @@ -158,3 +174,6 @@ Or, we can put a lightweight REST gateway in front of it, and also set up some b
## Homework

Follow these instructions!
If you're able to, then you're able to get the basics of Docker and Python web servers.

For a deeper understanding, try using FastAPI instead of Flask.

0 comments on commit 45326bb

Please sign in to comment.