From 22924f15cff5d313a72c9d46c093c5f3f1d4a3f7 Mon Sep 17 00:00:00 2001 From: Rohan Kapadia Date: Thu, 12 Dec 2024 15:26:28 +1100 Subject: [PATCH 1/7] Update presidio containers to use gunicorn --- presidio-analyzer/Dockerfile | 5 ++++- presidio-analyzer/app.py | 5 +++-- presidio-analyzer/pyproject.toml | 3 ++- presidio-anonymizer/Dockerfile | 6 +++++- presidio-anonymizer/app.py | 5 +++-- presidio-anonymizer/pyproject.toml | 3 ++- presidio-image-redactor/Dockerfile | 5 ++++- presidio-image-redactor/app.py | 5 +++-- presidio-image-redactor/pyproject.toml | 3 ++- 9 files changed, 28 insertions(+), 12 deletions(-) diff --git a/presidio-analyzer/Dockerfile b/presidio-analyzer/Dockerfile index 6d2f59539..12d8bff96 100644 --- a/presidio-analyzer/Dockerfile +++ b/presidio-analyzer/Dockerfile @@ -10,6 +10,9 @@ ENV ANALYZER_CONF_FILE=${ANALYZER_CONF_FILE} ENV RECOGNIZER_REGISTRY_CONF_FILE=${RECOGNIZER_REGISTRY_CONF_FILE} ENV NLP_CONF_FILE=${NLP_CONF_FILE} +ENV PORT=3000 +ENV WORKERS=1 + COPY ${ANALYZER_CONF_FILE} /usr/bin/${NAME}/${ANALYZER_CONF_FILE} COPY ${RECOGNIZER_REGISTRY_CONF_FILE} /usr/bin/${NAME}/${RECOGNIZER_REGISTRY_CONF_FILE} COPY ${NLP_CONF_FILE} /usr/bin/${NAME}/${NLP_CONF_FILE} @@ -31,4 +34,4 @@ RUN poetry run python install_nlp_models.py --conf_file ${NLP_CONF_FILE} COPY . /usr/bin/${NAME}/ EXPOSE ${PORT} -CMD poetry run python app.py --host 0.0.0.0 +CMD poetry run gunicorn -w $WORKERS -b 0.0.0.0:$PORT app:app diff --git a/presidio-analyzer/app.py b/presidio-analyzer/app.py index 900429eae..a2fed3d4b 100644 --- a/presidio-analyzer/app.py +++ b/presidio-analyzer/app.py @@ -135,8 +135,9 @@ def supported_entities() -> Tuple[str, int]: def http_exception(e): return jsonify(error=e.description), e.code +server = Server() +app = server.app if __name__ == "__main__": port = int(os.environ.get("PORT", DEFAULT_PORT)) - server = Server() - server.app.run(host="0.0.0.0", port=port) + app.run(host="0.0.0.0", port=port) diff --git a/presidio-analyzer/pyproject.toml b/presidio-analyzer/pyproject.toml index f6e266ddb..fa477e9fa 100644 --- a/presidio-analyzer/pyproject.toml +++ b/presidio-analyzer/pyproject.toml @@ -36,9 +36,10 @@ azure-ai-textanalytics = { version = "*", optional = true } azure-core = { version = "*", optional = true } transformers = { version = "*", optional = true } huggingface_hub = { version = "*", optional = true } +gunicorn = {version = "*", optional = true} [tool.poetry.extras] -server = ["flask"] +server = ["flask", "gunicorn"] transformers = [ "transformers", "huggingface_hub", diff --git a/presidio-anonymizer/Dockerfile b/presidio-anonymizer/Dockerfile index ade207023..8783c9279 100644 --- a/presidio-anonymizer/Dockerfile +++ b/presidio-anonymizer/Dockerfile @@ -2,6 +2,10 @@ FROM python:3.9-slim ARG NAME ENV PIP_NO_CACHE_DIR=1 + +ENV PORT=3000 +ENV WORKERS=1 + WORKDIR /usr/bin/${NAME} COPY ./pyproject.toml /usr/bin/${NAME}/ @@ -11,4 +15,4 @@ RUN pip install poetry && poetry install --no-root --only=main -E server COPY . /usr/bin/${NAME}/ EXPOSE ${PORT} -CMD poetry run python app.py \ No newline at end of file +CMD poetry run gunicorn -w $WORKERS -b 0.0.0.0:$PORT app:app \ No newline at end of file diff --git a/presidio-anonymizer/app.py b/presidio-anonymizer/app.py index 12ce67c02..88e530f9a 100644 --- a/presidio-anonymizer/app.py +++ b/presidio-anonymizer/app.py @@ -112,8 +112,9 @@ def server_error(e): self.logger.error(f"A fatal error occurred during execution: {e}") return jsonify(error="Internal server error"), 500 +server = Server() +app = server.app if __name__ == "__main__": port = int(os.environ.get("PORT", DEFAULT_PORT)) - server = Server() - server.app.run(host="0.0.0.0", port=port) + app.run(host="0.0.0.0", port=port) diff --git a/presidio-anonymizer/pyproject.toml b/presidio-anonymizer/pyproject.toml index e840ac7f5..b570a86fd 100644 --- a/presidio-anonymizer/pyproject.toml +++ b/presidio-anonymizer/pyproject.toml @@ -25,9 +25,10 @@ python = ">=3.9,<4.0" pycryptodome = ">=3.10.1" azure-core = { version = "*", optional = true } flask = { version = ">=1.1", optional = true } +gunicorn = {version = "*", optional = true} [tool.poetry.extras] -server = ["flask"] +server = ["flask", "gunicorn"] [tool.poetry.group.dev.dependencies] pip = "*" diff --git a/presidio-image-redactor/Dockerfile b/presidio-image-redactor/Dockerfile index 5c0244a11..9023de0b8 100644 --- a/presidio-image-redactor/Dockerfile +++ b/presidio-image-redactor/Dockerfile @@ -10,6 +10,9 @@ ENV ANALYZER_CONF_FILE=${ANALYZER_CONF_FILE} ENV RECOGNIZER_REGISTRY_CONF_FILE=${RECOGNIZER_REGISTRY_CONF_FILE} ENV NLP_CONF_FILE=${NLP_CONF_FILE} +ENV PORT=3000 +ENV WORKERS=1 + COPY ${ANALYZER_CONF_FILE} /usr/bin/${NAME}/${ANALYZER_CONF_FILE} COPY ${RECOGNIZER_REGISTRY_CONF_FILE} /usr/bin/${NAME}/${RECOGNIZER_REGISTRY_CONF_FILE} COPY ${NLP_CONF_FILE} /usr/bin/${NAME}/${NLP_CONF_FILE} @@ -35,4 +38,4 @@ RUN pip install poetry && poetry install --no-root --only=main -E server COPY . /usr/bin/${NAME}/ EXPOSE ${PORT} -CMD poetry run python app.py \ No newline at end of file +CMD poetry run gunicorn -w $WORKERS -b 0.0.0.0:$PORT app:app \ No newline at end of file diff --git a/presidio-image-redactor/app.py b/presidio-image-redactor/app.py index 780742eaf..ea77402bb 100644 --- a/presidio-image-redactor/app.py +++ b/presidio-image-redactor/app.py @@ -80,8 +80,9 @@ def server_error(e): self.logger.error(f"A fatal error occurred during execution: {e}") return jsonify(error="Internal server error"), 500 +server = Server() +app = server.app if __name__ == "__main__": port = int(os.environ.get("PORT", DEFAULT_PORT)) - server = Server() - server.app.run(host="0.0.0.0", port=port) + app.run(host="0.0.0.0", port=port) diff --git a/presidio-image-redactor/pyproject.toml b/presidio-image-redactor/pyproject.toml index ac39df6b6..f02f4b312 100644 --- a/presidio-image-redactor/pyproject.toml +++ b/presidio-image-redactor/pyproject.toml @@ -32,9 +32,10 @@ azure-ai-formrecognizer = ">=3.3.0,<4.0.0" opencv-python = ">=4.0.0,<5.0.0" python-gdcm = ">=3.0.24.1" flask = { version = ">=1.1", optional = true } +gunicorn = {version = "*", optional = true} [tool.poetry.extras] -server = ["flask"] +server = ["flask", "gunicorn"] [tool.poetry.group.dev.dependencies] pip = "*" From 0c062e9e9f86e51eac19362b14e3bedb1d06f858 Mon Sep 17 00:00:00 2001 From: Rohan Kapadia Date: Thu, 12 Dec 2024 21:08:57 +1100 Subject: [PATCH 2/7] Add gunicorn to notice file --- NOTICE | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/NOTICE b/NOTICE index 56dc1d2c6..098e4819e 100644 --- a/NOTICE +++ b/NOTICE @@ -1552,3 +1552,30 @@ distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. + +******* +gunicorn + +2009-2024 (c) BenoƮt Chesneau +2009-2015 (c) Paul J. Davis + +Permission is hereby granted, free of charge, to any person +obtaining a copy of this software and associated documentation +files (the "Software"), to deal in the Software without +restriction, including without limitation the rights to use, +copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file From d9c40780ee0b97ff376daeda1c457b405725921a Mon Sep 17 00:00:00 2001 From: Rohan Kapadia Date: Sat, 14 Dec 2024 01:29:49 +1100 Subject: [PATCH 3/7] Update app.py so it doesn't create a server object on import --- presidio-analyzer/Dockerfile | 2 +- presidio-analyzer/app.py | 6 ++++-- presidio-anonymizer/Dockerfile | 2 +- presidio-anonymizer/app.py | 6 ++++-- presidio-image-redactor/app.py | 6 ++++-- 5 files changed, 14 insertions(+), 8 deletions(-) diff --git a/presidio-analyzer/Dockerfile b/presidio-analyzer/Dockerfile index 12d8bff96..86ded5997 100644 --- a/presidio-analyzer/Dockerfile +++ b/presidio-analyzer/Dockerfile @@ -34,4 +34,4 @@ RUN poetry run python install_nlp_models.py --conf_file ${NLP_CONF_FILE} COPY . /usr/bin/${NAME}/ EXPOSE ${PORT} -CMD poetry run gunicorn -w $WORKERS -b 0.0.0.0:$PORT app:app +CMD poetry run gunicorn -w $WORKERS -b 0.0.0.0:$PORT 'app:create_app()' diff --git a/presidio-analyzer/app.py b/presidio-analyzer/app.py index a2fed3d4b..94666f2ee 100644 --- a/presidio-analyzer/app.py +++ b/presidio-analyzer/app.py @@ -135,9 +135,11 @@ def supported_entities() -> Tuple[str, int]: def http_exception(e): return jsonify(error=e.description), e.code -server = Server() -app = server.app +def create_app(): + server = Server() + return server.app if __name__ == "__main__": + app = create_app() port = int(os.environ.get("PORT", DEFAULT_PORT)) app.run(host="0.0.0.0", port=port) diff --git a/presidio-anonymizer/Dockerfile b/presidio-anonymizer/Dockerfile index 8783c9279..b7d1870ee 100644 --- a/presidio-anonymizer/Dockerfile +++ b/presidio-anonymizer/Dockerfile @@ -15,4 +15,4 @@ RUN pip install poetry && poetry install --no-root --only=main -E server COPY . /usr/bin/${NAME}/ EXPOSE ${PORT} -CMD poetry run gunicorn -w $WORKERS -b 0.0.0.0:$PORT app:app \ No newline at end of file +CMD poetry run gunicorn -w $WORKERS -b 0.0.0.0:$PORT 'app:create_app()' \ No newline at end of file diff --git a/presidio-anonymizer/app.py b/presidio-anonymizer/app.py index 88e530f9a..e808b705d 100644 --- a/presidio-anonymizer/app.py +++ b/presidio-anonymizer/app.py @@ -112,9 +112,11 @@ def server_error(e): self.logger.error(f"A fatal error occurred during execution: {e}") return jsonify(error="Internal server error"), 500 -server = Server() -app = server.app +def create_app(): + server = Server() + return server.app if __name__ == "__main__": + app = create_app() port = int(os.environ.get("PORT", DEFAULT_PORT)) app.run(host="0.0.0.0", port=port) diff --git a/presidio-image-redactor/app.py b/presidio-image-redactor/app.py index ea77402bb..64d258d47 100644 --- a/presidio-image-redactor/app.py +++ b/presidio-image-redactor/app.py @@ -80,9 +80,11 @@ def server_error(e): self.logger.error(f"A fatal error occurred during execution: {e}") return jsonify(error="Internal server error"), 500 -server = Server() -app = server.app +def create_app(): + server = Server() + return server.app if __name__ == "__main__": + app = create_app() port = int(os.environ.get("PORT", DEFAULT_PORT)) app.run(host="0.0.0.0", port=port) From f65004590f96042a8be66d0768e92d9aa0dd9a15 Mon Sep 17 00:00:00 2001 From: Rohan Kapadia Date: Sat, 14 Dec 2024 01:30:09 +1100 Subject: [PATCH 4/7] update presidio image redactor dockerfile --- presidio-image-redactor/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/presidio-image-redactor/Dockerfile b/presidio-image-redactor/Dockerfile index 9023de0b8..0f11dc905 100644 --- a/presidio-image-redactor/Dockerfile +++ b/presidio-image-redactor/Dockerfile @@ -38,4 +38,4 @@ RUN pip install poetry && poetry install --no-root --only=main -E server COPY . /usr/bin/${NAME}/ EXPOSE ${PORT} -CMD poetry run gunicorn -w $WORKERS -b 0.0.0.0:$PORT app:app \ No newline at end of file +CMD poetry run gunicorn -w $WORKERS -b 0.0.0.0:$PORT 'app:create_app()' \ No newline at end of file From a0454079e657c0dba8aed68c8c59277bf624acb9 Mon Sep 17 00:00:00 2001 From: Omri Mendels Date: Sat, 14 Dec 2024 14:14:18 +0200 Subject: [PATCH 5/7] noqa for new function --- presidio-analyzer/app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/presidio-analyzer/app.py b/presidio-analyzer/app.py index 94666f2ee..a85ce16de 100644 --- a/presidio-analyzer/app.py +++ b/presidio-analyzer/app.py @@ -135,7 +135,7 @@ def supported_entities() -> Tuple[str, int]: def http_exception(e): return jsonify(error=e.description), e.code -def create_app(): +def create_app(): # noqa server = Server() return server.app From 75d90bad5ac59bcdf72f051d74947d4108a7df5c Mon Sep 17 00:00:00 2001 From: Omri Mendels Date: Sat, 14 Dec 2024 14:14:43 +0200 Subject: [PATCH 6/7] noqa for new function --- presidio-anonymizer/app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/presidio-anonymizer/app.py b/presidio-anonymizer/app.py index e808b705d..52f75f41a 100644 --- a/presidio-anonymizer/app.py +++ b/presidio-anonymizer/app.py @@ -112,7 +112,7 @@ def server_error(e): self.logger.error(f"A fatal error occurred during execution: {e}") return jsonify(error="Internal server error"), 500 -def create_app(): +def create_app(): # noqa server = Server() return server.app From 96a60dafe2734f75e8fa3e47eb88235458284fac Mon Sep 17 00:00:00 2001 From: Omri Mendels Date: Sat, 14 Dec 2024 14:15:05 +0200 Subject: [PATCH 7/7] noqa for new function --- presidio-image-redactor/app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/presidio-image-redactor/app.py b/presidio-image-redactor/app.py index 64d258d47..fdcc31e95 100644 --- a/presidio-image-redactor/app.py +++ b/presidio-image-redactor/app.py @@ -80,7 +80,7 @@ def server_error(e): self.logger.error(f"A fatal error occurred during execution: {e}") return jsonify(error="Internal server error"), 500 -def create_app(): +def create_app(): # noqa server = Server() return server.app