From 3a355ed5853427d192641546a341ab1a830082df Mon Sep 17 00:00:00 2001 From: aianko Date: Mon, 12 Dec 2022 12:43:20 +0200 Subject: [PATCH 01/13] init model before requests --- embedding-calculator/src/_endpoints.py | 13 ++++++++++++- embedding-calculator/src/app.py | 3 ++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/embedding-calculator/src/_endpoints.py b/embedding-calculator/src/_endpoints.py index 53685293d1..a75beecd15 100644 --- a/embedding-calculator/src/_endpoints.py +++ b/embedding-calculator/src/_endpoints.py @@ -19,15 +19,26 @@ from src.constants import ENV from src.exceptions import NoFaceFoundError -from src.services.facescan.plugins import base, managers +from src.services.facescan.plugins import managers from src.services.facescan.scanner.facescanners import scanner from src.services.flask_.constants import ARG from src.services.flask_.needs_attached_file import needs_attached_file from src.services.imgtools.read_img import read_img from src.services.utils.pyutils import Constants +from src.services.imgtools.test.files import IMG_DIR import base64 +def init_model() -> None: + detector = managers.plugin_manager.detector + face_plugins = managers.plugin_manager.face_plugins + detector( + img=read_img(str(IMG_DIR / 'einstein.jpeg')), + det_prob_threshold=_get_det_prob_threshold(), + face_plugins=face_plugins + ) + return None + def endpoints(app): @app.route('/status') def status_get(): diff --git a/embedding-calculator/src/app.py b/embedding-calculator/src/app.py index 5242f84bee..f2355db3f3 100644 --- a/embedding-calculator/src/app.py +++ b/embedding-calculator/src/app.py @@ -19,7 +19,7 @@ from src import constants from src._docs import add_docs -from src._endpoints import endpoints +from src._endpoints import endpoints, init_model from src.constants import ENV from src.docs import DOCS_DIR from src.init_runtime import init_runtime @@ -38,6 +38,7 @@ def init_app_runtime(): def create_app(add_endpoints_fun: Union[Callable, None] = None, do_add_docs: bool = False): app = Flask('embedding-calculator') + app.before_first_request_funcs.append(init_model) app.url_map.strict_slashes = False add_error_handling(app) app.after_request(log_http_response) From 6075341303f55f42037a07f94717a323ea814fe4 Mon Sep 17 00:00:00 2001 From: Anatolii-R <78787418+Anatolii-R@users.noreply.github.com> Date: Wed, 12 Jul 2023 15:59:52 +0300 Subject: [PATCH 02/13] Update app.py --- embedding-calculator/src/app.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/embedding-calculator/src/app.py b/embedding-calculator/src/app.py index f2355db3f3..5242f84bee 100644 --- a/embedding-calculator/src/app.py +++ b/embedding-calculator/src/app.py @@ -19,7 +19,7 @@ from src import constants from src._docs import add_docs -from src._endpoints import endpoints, init_model +from src._endpoints import endpoints from src.constants import ENV from src.docs import DOCS_DIR from src.init_runtime import init_runtime @@ -38,7 +38,6 @@ def init_app_runtime(): def create_app(add_endpoints_fun: Union[Callable, None] = None, do_add_docs: bool = False): app = Flask('embedding-calculator') - app.before_first_request_funcs.append(init_model) app.url_map.strict_slashes = False add_error_handling(app) app.after_request(log_http_response) From 19866e49a976756ce5f26d27375118267a7a9de9 Mon Sep 17 00:00:00 2001 From: Anatolii-R <78787418+Anatolii-R@users.noreply.github.com> Date: Wed, 12 Jul 2023 16:02:21 +0300 Subject: [PATCH 03/13] Update _endpoints.py --- embedding-calculator/src/_endpoints.py | 45 ++++++++++++++++++++------ 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/embedding-calculator/src/_endpoints.py b/embedding-calculator/src/_endpoints.py index a75beecd15..869f019a6d 100644 --- a/embedding-calculator/src/_endpoints.py +++ b/embedding-calculator/src/_endpoints.py @@ -11,6 +11,7 @@ # 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. + from typing import List, Optional from flask import request @@ -27,19 +28,40 @@ from src.services.utils.pyutils import Constants from src.services.imgtools.test.files import IMG_DIR import base64 +from src.constants import SKIPPED_PLUGINS + +class FaceDetection(object): + SKIPPING_FACE_DETECTION = False -def init_model() -> None: - detector = managers.plugin_manager.detector - face_plugins = managers.plugin_manager.face_plugins - detector( - img=read_img(str(IMG_DIR / 'einstein.jpeg')), - det_prob_threshold=_get_det_prob_threshold(), - face_plugins=face_plugins - ) - return None + +def face_detection_skip_check(face_plugins): + if request.values.get("detect_faces") == "false": + FaceDetection.SKIPPING_FACE_DETECTION = True + restricted_plugins = [plugin for plugin in face_plugins if plugin.name not in SKIPPED_PLUGINS] + return restricted_plugins + else: + return face_plugins def endpoints(app): + @app.before_first_request + def init_model() -> None: + detector = managers.plugin_manager.detector + face_plugins = managers.plugin_manager.face_plugins + face_plugins = face_detection_skip_check(face_plugins) + detector( + img=read_img(str(IMG_DIR / 'einstein.jpeg')), + det_prob_threshold=_get_det_prob_threshold(), + face_plugins=face_plugins + ) + return None + + @app.route('/healthcheck') + def healthcheck(): + return jsonify( + status='OK' + ) + @app.route('/status') def status_get(): available_plugins = {p.slug: str(p) @@ -58,7 +80,7 @@ def find_faces_base64_post(): face_plugins = managers.plugin_manager.filter_face_plugins( _get_face_plugin_names() ) - + face_plugins = face_detection_skip_check(face_plugins) rawfile = base64.b64decode(request.get_json()["file"]) faces = detector( @@ -68,6 +90,7 @@ def find_faces_base64_post(): ) plugins_versions = {p.slug: str(p) for p in [detector] + face_plugins} faces = _limit(faces, request.values.get(ARG.LIMIT)) + FaceDetection.SKIPPING_FACE_DETECTION = False return jsonify(plugins_versions=plugins_versions, result=faces) @app.route('/find_faces', methods=['POST']) @@ -77,6 +100,7 @@ def find_faces_post(): face_plugins = managers.plugin_manager.filter_face_plugins( _get_face_plugin_names() ) + face_plugins = face_detection_skip_check(face_plugins) faces = detector( img=read_img(request.files['file']), det_prob_threshold=_get_det_prob_threshold(), @@ -84,6 +108,7 @@ def find_faces_post(): ) plugins_versions = {p.slug: str(p) for p in [detector] + face_plugins} faces = _limit(faces, request.values.get(ARG.LIMIT)) + FaceDetection.SKIPPING_FACE_DETECTION = False return jsonify(plugins_versions=plugins_versions, result=faces) @app.route('/scan_faces', methods=['POST']) From 6ea864dba855a86c69a3ed2765da361099f69ff7 Mon Sep 17 00:00:00 2001 From: Anatolii-R <78787418+Anatolii-R@users.noreply.github.com> Date: Wed, 12 Jul 2023 16:04:20 +0300 Subject: [PATCH 04/13] Update docker-compose.yml --- dev/docker-compose.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/dev/docker-compose.yml b/dev/docker-compose.yml index 42a99e58e0..aba8b62238 100644 --- a/dev/docker-compose.yml +++ b/dev/docker-compose.yml @@ -102,3 +102,9 @@ services: - ML_PORT=3000 - UWSGI_PROCESSES=${uwsgi_processes:-2} - UWSGI_THREADS=${uwsgi_threads:-1} + healthcheck: + test: curl --fail http://localhost:3000/healthcheck || exit 1 + interval: 60s + retries: 2 + start_period: 60s + timeout: 120s From 65885c9da770f5772a4ea427e9862bf95bd42ad2 Mon Sep 17 00:00:00 2001 From: Anatolii-R <78787418+Anatolii-R@users.noreply.github.com> Date: Wed, 12 Jul 2023 16:05:15 +0300 Subject: [PATCH 05/13] Update docker-compose-gpu.yml --- dev/docker-compose-gpu.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/dev/docker-compose-gpu.yml b/dev/docker-compose-gpu.yml index 8f5ff40934..a432adce32 100644 --- a/dev/docker-compose-gpu.yml +++ b/dev/docker-compose-gpu.yml @@ -104,3 +104,9 @@ services: - ML_PORT=3000 - UWSGI_PROCESSES=${uwsgi_processes:-1} - UWSGI_THREADS=${uwsgi_threads:-1} + healthcheck: + test: curl --fail http://localhost:3000/healthcheck || exit 1 + interval: 60s + retries: 2 + start_period: 60s + timeout: 120s From ce7db7a3204e240a4364b6b3a2b4206d2c1b0b5f Mon Sep 17 00:00:00 2001 From: Anatolii-R <78787418+Anatolii-R@users.noreply.github.com> Date: Wed, 12 Jul 2023 16:06:12 +0300 Subject: [PATCH 06/13] Update docker-compose.yml --- docker-compose.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docker-compose.yml b/docker-compose.yml index 566a780e4e..8fadfe7bff 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -77,3 +77,9 @@ services: - IMG_LENGTH_LIMIT=${max_detect_size} - UWSGI_PROCESSES=${uwsgi_processes:-2} - UWSGI_THREADS=${uwsgi_threads:-1} + healthcheck: + test: curl --fail http://localhost:3000/healthcheck || exit 1 + interval: 60s + retries: 2 + start_period: 60s + timeout: 120s From 9bee66a208e141ca87082bf2176d018cbf9b8e6a Mon Sep 17 00:00:00 2001 From: Anatolii-R <78787418+Anatolii-R@users.noreply.github.com> Date: Wed, 12 Jul 2023 18:14:51 +0300 Subject: [PATCH 07/13] Update docker-compose.yml --- dev/docker-compose.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dev/docker-compose.yml b/dev/docker-compose.yml index 881adf44d4..250d9a6918 100644 --- a/dev/docker-compose.yml +++ b/dev/docker-compose.yml @@ -105,7 +105,7 @@ services: - UWSGI_THREADS=${uwsgi_threads:-1} healthcheck: test: curl --fail http://localhost:3000/healthcheck || exit 1 - interval: 60s - retries: 2 - start_period: 60s - timeout: 120s + interval: 10s + retries: 12 + start_period: 10s + timeout: 1s From c786c7670b580dff661f30986cb4e84e417cd5f5 Mon Sep 17 00:00:00 2001 From: Anatolii-R <78787418+Anatolii-R@users.noreply.github.com> Date: Wed, 12 Jul 2023 18:15:16 +0300 Subject: [PATCH 08/13] Update docker-compose-gpu.yml --- dev/docker-compose-gpu.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dev/docker-compose-gpu.yml b/dev/docker-compose-gpu.yml index a432adce32..74b740e780 100644 --- a/dev/docker-compose-gpu.yml +++ b/dev/docker-compose-gpu.yml @@ -106,7 +106,7 @@ services: - UWSGI_THREADS=${uwsgi_threads:-1} healthcheck: test: curl --fail http://localhost:3000/healthcheck || exit 1 - interval: 60s - retries: 2 - start_period: 60s - timeout: 120s + interval: 10s + retries: 12 + start_period: 10s + timeout: 1s From e07e7c1beca321c87a0e44ce4b4ca65b7523242b Mon Sep 17 00:00:00 2001 From: Anatolii-R <78787418+Anatolii-R@users.noreply.github.com> Date: Wed, 12 Jul 2023 18:15:39 +0300 Subject: [PATCH 09/13] Update docker-compose.yml --- docker-compose.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 8fadfe7bff..7bd2f83356 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -79,7 +79,7 @@ services: - UWSGI_THREADS=${uwsgi_threads:-1} healthcheck: test: curl --fail http://localhost:3000/healthcheck || exit 1 - interval: 60s - retries: 2 - start_period: 60s - timeout: 120s + interval: 10s + retries: 12 + start_period: 10s + timeout: 1s From 227c2f2e765b477bf1473c93aaa10df5c2fced0d Mon Sep 17 00:00:00 2001 From: Anatolii-R <78787418+Anatolii-R@users.noreply.github.com> Date: Thu, 13 Jul 2023 14:36:20 +0300 Subject: [PATCH 10/13] Update docker-compose.yml --- docker-compose.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 7bd2f83356..ac07b0bc7f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -80,6 +80,6 @@ services: healthcheck: test: curl --fail http://localhost:3000/healthcheck || exit 1 interval: 10s - retries: 12 - start_period: 10s + retries: 0 + start_period: 0s timeout: 1s From 130ce493f0ff93765ffa22dc3404b2cbf3996441 Mon Sep 17 00:00:00 2001 From: Anatolii-R <78787418+Anatolii-R@users.noreply.github.com> Date: Thu, 13 Jul 2023 14:36:52 +0300 Subject: [PATCH 11/13] Update docker-compose.yml --- dev/docker-compose.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dev/docker-compose.yml b/dev/docker-compose.yml index 250d9a6918..926613e11e 100644 --- a/dev/docker-compose.yml +++ b/dev/docker-compose.yml @@ -106,6 +106,6 @@ services: healthcheck: test: curl --fail http://localhost:3000/healthcheck || exit 1 interval: 10s - retries: 12 - start_period: 10s + retries: 0 + start_period: 0s timeout: 1s From 859a0ee682ef32a61394f0a1e487cc045d46f80a Mon Sep 17 00:00:00 2001 From: Anatolii-R <78787418+Anatolii-R@users.noreply.github.com> Date: Thu, 13 Jul 2023 14:37:25 +0300 Subject: [PATCH 12/13] Update docker-compose-gpu.yml --- dev/docker-compose-gpu.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dev/docker-compose-gpu.yml b/dev/docker-compose-gpu.yml index 74b740e780..f0c302d389 100644 --- a/dev/docker-compose-gpu.yml +++ b/dev/docker-compose-gpu.yml @@ -107,6 +107,6 @@ services: healthcheck: test: curl --fail http://localhost:3000/healthcheck || exit 1 interval: 10s - retries: 12 - start_period: 10s + retries: 0 + start_period: 0s timeout: 1s From 31b1722e4edd91c38f07e3c04f32dcdbaae20952 Mon Sep 17 00:00:00 2001 From: Anatolii-R <78787418+Anatolii-R@users.noreply.github.com> Date: Thu, 13 Jul 2023 14:39:21 +0300 Subject: [PATCH 13/13] Update _endpoints.py --- embedding-calculator/src/_endpoints.py | 1 + 1 file changed, 1 insertion(+) diff --git a/embedding-calculator/src/_endpoints.py b/embedding-calculator/src/_endpoints.py index 869f019a6d..a18080ae42 100644 --- a/embedding-calculator/src/_endpoints.py +++ b/embedding-calculator/src/_endpoints.py @@ -54,6 +54,7 @@ def init_model() -> None: det_prob_threshold=_get_det_prob_threshold(), face_plugins=face_plugins ) + print("Starting to load ML models") return None @app.route('/healthcheck')