From 2540bb1ff49ad9df0e9e5175e962285fd7227d43 Mon Sep 17 00:00:00 2001 From: Antunes Date: Fri, 1 Mar 2024 11:38:18 -0300 Subject: [PATCH 1/3] example of face recognition using docker-compose --- examples/docker-compose-example/Dockerfile | 15 +++++++++++++++ examples/docker-compose-example/api.py | 12 ++++++++++++ .../docker-compose-example/docker-compose.yml | 11 +++++++++++ examples/docker-compose-example/requirements.txt | 3 +++ 4 files changed, 41 insertions(+) create mode 100644 examples/docker-compose-example/Dockerfile create mode 100644 examples/docker-compose-example/api.py create mode 100644 examples/docker-compose-example/docker-compose.yml create mode 100644 examples/docker-compose-example/requirements.txt diff --git a/examples/docker-compose-example/Dockerfile b/examples/docker-compose-example/Dockerfile new file mode 100644 index 0000000..52e2ffb --- /dev/null +++ b/examples/docker-compose-example/Dockerfile @@ -0,0 +1,15 @@ +FROM python:3.11 + +WORKDIR /api + +# Copying requirements.txt to the container's working directory +COPY requirements.txt . + +# Copying all other files and directories to the container's working directory +COPY . /api + +# Installing dependencies +RUN pip install --no-cache-dir -r requirements.txt + +# Running the api +CMD ["python", "api.py"] diff --git a/examples/docker-compose-example/api.py b/examples/docker-compose-example/api.py new file mode 100644 index 0000000..d1835f1 --- /dev/null +++ b/examples/docker-compose-example/api.py @@ -0,0 +1,12 @@ +import os +from fastserve.models import FaceDetection + +#example of getting environment variables setted in docker-compose.yml +batch_size = os.environ.get('BATCH_SIZE', 2) +timeout = os.environ.get('TIMEOUT', 1) + + +app = FaceDetection(batch_size=batch_size, timeout=timeout) +app.run_server(host='0.0.0.0') + + diff --git a/examples/docker-compose-example/docker-compose.yml b/examples/docker-compose-example/docker-compose.yml new file mode 100644 index 0000000..431eb0d --- /dev/null +++ b/examples/docker-compose-example/docker-compose.yml @@ -0,0 +1,11 @@ +version: '3' +services: + api: + build: . + environment: + - BATCH_SIZE=2 + - TIMEOUT = 1 + volumes: + - .:/api + ports: + - "8000:8000" diff --git a/examples/docker-compose-example/requirements.txt b/examples/docker-compose-example/requirements.txt new file mode 100644 index 0000000..f77844f --- /dev/null +++ b/examples/docker-compose-example/requirements.txt @@ -0,0 +1,3 @@ +git+https://github.com/aniketmaurya/fastserve-ai.git +torch +face-recognition From 0ef96217f002b144959504d993f0d4384d66f0e0 Mon Sep 17 00:00:00 2001 From: Antunes Date: Fri, 1 Mar 2024 12:05:05 -0300 Subject: [PATCH 2/3] updating README: adding docker section and run_server explanation --- README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/README.md b/README.md index 1acc252..7c617b2 100644 --- a/README.md +++ b/README.md @@ -139,6 +139,33 @@ python fastserve.deploy.lightning --filename main.py \ --machine "CPU" # T4, A10G or A10G_X_4 ``` +## Containerization + +To containerize your FastServe application, a Docker example is provided in the `examples/docker-compose-example` directory. The example is about face recognition and includes a `Dockerfile` for creating a Docker image and a `docker-compose.yml` for easy deployment. Here's a quick overview: + +- **Dockerfile**: Defines the environment, installs dependencies from `requirements.txt`, and specifies the command to run your FastServe application. +- **docker-compose.yml**: Simplifies the deployment of your FastServe application by defining services, networks, and volumes. + +To use the example, navigate to the `examples/docker-compose-example` directory and run: + +```shell +docker-compose up --build +``` + +This will build the Docker image and start your FastServe application in a container, making it accessible on the specified port. + +Note: We provide an example using face recognition. If you need to use other models, you will likely need to change the requirements.txt or the Dockerfile. Don't worry; this example is intended to serve as a quick start. Feel free to modify it as needed. + +## Passing Arguments to Uvicorn in `run_server()` +FastServe leverages Uvicorn, a lightning-fast ASGI server, to serve machine learning models, making FastServe highly efficient and scalable. +The `run_server()` method supports passing additional arguments to uvicorn by utilizing `*args` and `**kwargs`. This feature allows you to customize the server's behavior without modifying the source code. For example: + +```shell +app.run_server(host='0.0.0.0', port=8000, log_level='info') +``` + +In this example, host, port, and log_level are passed directly to uvicorn.run() to specify the server's IP address, port, and logging level. You can pass any argument supported by `uvicorn.run()` to `run_server()` in this manner. + ## Contribute **Install in editable mode:** From bef2b5562b32c6f57031db40332051d5ba8e2597 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 1 Mar 2024 17:06:27 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- examples/docker-compose-example/api.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/examples/docker-compose-example/api.py b/examples/docker-compose-example/api.py index d1835f1..2a13fd1 100644 --- a/examples/docker-compose-example/api.py +++ b/examples/docker-compose-example/api.py @@ -1,12 +1,11 @@ import os + from fastserve.models import FaceDetection -#example of getting environment variables setted in docker-compose.yml -batch_size = os.environ.get('BATCH_SIZE', 2) -timeout = os.environ.get('TIMEOUT', 1) +# example of getting environment variables setted in docker-compose.yml +batch_size = os.environ.get("BATCH_SIZE", 2) +timeout = os.environ.get("TIMEOUT", 1) app = FaceDetection(batch_size=batch_size, timeout=timeout) -app.run_server(host='0.0.0.0') - - +app.run_server(host="0.0.0.0")