From 097cc8303d65daf9949ab93c2e97144f172f6ce5 Mon Sep 17 00:00:00 2001 From: Alvaro Lopez Garcia Date: Fri, 7 Jun 2024 23:28:15 +0200 Subject: [PATCH] feat!: remove deprecated deepaas-predict Fixes #154 --- deepaas/cmd/execute.py | 164 ------------------ deepaas/tests/test_cmd.py | 54 ------ doc/source/cli/deepaas-predict.rst | 63 ------- doc/source/cli/index.rst | 1 - doc/source/user/predict.rst | 36 ---- pyproject.toml | 1 - ...cate-deepaas-predict-f92939d443cb1904.yaml | 4 +- 7 files changed, 2 insertions(+), 321 deletions(-) delete mode 100644 deepaas/cmd/execute.py delete mode 100644 doc/source/cli/deepaas-predict.rst delete mode 100644 doc/source/user/predict.rst diff --git a/deepaas/cmd/execute.py b/deepaas/cmd/execute.py deleted file mode 100644 index c1cdc8b6..00000000 --- a/deepaas/cmd/execute.py +++ /dev/null @@ -1,164 +0,0 @@ -#!/usr/bin/env python -*- coding: utf-8 -*- - -# Copyright 2020 Spanish National Research Council (CSIC) -# -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# 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. - -import mimetypes -import os -import shutil -import sys -import warnings - -from oslo_config import cfg -from oslo_log import log - -from deepaas.model import loading -from deepaas.model.v2.wrapper import UploadedFile - -cli_opts = [ - cfg.StrOpt( - "input-file", - short="i", - help=""" -Set local input file to predict. -""", - ), - cfg.StrOpt( - "content-type", - default="application/json", - short="ct", - help=""" -Especify the content type of the output file. The selected -option must be available in the model used. -(by default application/json). -""", - ), - cfg.StrOpt( - "output", - short="o", - help=""" -Save the result to a local file. -""", - ), - cfg.BoolOpt( - "url", - short="u", - default=False, - help=""" -Run as input file an URL. -If this option is set to True, we can use the URL -of an image as an input file. -""", - ), -] - -CONF = cfg.CONF -CONF.register_cli_opts(cli_opts) - -LOG = log.getLogger(__name__) - -# Loading the model installed - - -def get_model_name(): - model_name = CONF.model_name - models = loading.get_available_models("v2") - if model_name: - model_obj = models.get(model_name) - if model_obj is None: - sys.stderr.write( - "ERROR: The model {} is not available.\n".format(model_name) - ) - sys.exit(1) - return model_name, model_obj - elif len(models) == 1: - return models.popitem() - else: - sys.stderr.write( - "ERROR: There are several models available ({}) " - "you have to choose one.\n".format(list(models.keys())) - ) - sys.exit(1) - - -def prediction(input_file, file_type, content_type): - model_name, model_obj = get_model_name() - predict_data = model_obj.predict_data - predict_url = model_obj.predict_url - - if file_type is True: - input_data = {"urls": [input_file], "accept": content_type} - output_pred = predict_url(input_data) - else: - content_type_in, file_encoding = mimetypes.guess_type(input_file) - file = UploadedFile( - name=input_file, filename=input_file, content_type=content_type_in - ) - input_data = {"files": [file], "accept": content_type} - output_pred = predict_data(input_data) - - return output_pred - - -def main(): - msg = ( - "\033[0;31;40mWARNING: This command is deprectated, please use " - "deepaas-cli instead. \n" - "WARNING: This command will be removed in the next major release. \033[0m" - ) - warnings.warn(msg, DeprecationWarning, stacklevel=2) - sys.stderr.write(msg + "\n") - - cfg.CONF(sys.argv[1:]) - input_file = CONF.input_file - content_type = CONF.content_type - file_type = CONF.url - output = CONF.output - - # Checking required argument - if input_file is None: - sys.stderr.write("ERROR: Option input_file is required.\n") - sys.exit(1) - - if output is None: - sys.stderr.write("ERROR: Option output is required.\n") - sys.exit(1) - - output_pred = prediction(input_file, file_type, content_type) - extension = mimetypes.guess_extension(content_type) - if extension is None or output_pred is None: - sys.stderr.write("ERROR: Content type {} not valid.\n".format(content_type)) - sys.exit(1) - if extension == ".json": - name_image = os.path.splitext(os.path.basename(input_file))[0] - out_file_name = "out_" + name_image - f = open(out_file_name + ".json", "w+") - f.write(repr(output_pred) + "\n") - f.close() - if not os.path.exists(output): # Create path if does not exist - os.makedirs(output) - dir_name = output + f.name - shutil.move(f.name, os.path.join(output, f.name)) - else: - output_path_image = output_pred.name - dir_name = output + os.path.basename(output_path_image) - if not os.path.exists(output): # Create path if does not exist - os.makedirs(output) - shutil.copy(output_path_image, output) - - print("Output saved at {}".format(dir_name)) - - -if __name__ == "__main__": - main() diff --git a/deepaas/tests/test_cmd.py b/deepaas/tests/test_cmd.py index 10029284..9f9dd369 100644 --- a/deepaas/tests/test_cmd.py +++ b/deepaas/tests/test_cmd.py @@ -14,14 +14,10 @@ # License for the specific language governing permissions and limitations # under the License. -import os -import shutil import sys -import urllib.request import mock -from deepaas.cmd import execute from deepaas.cmd import run from deepaas.tests import base @@ -62,53 +58,3 @@ def test_run_custom_ip_port(self, m_get_app, m_run_app, m_handle_signals): port=port, ) m_handle_signals.assert_called_once() - - -class TestExecute(base.TestCase): - @mock.patch("deepaas.cmd.execute.prediction") - def test_execute_data(self, m_out_pred): - in_file = "file" - out_file = "deepaas/tests/out_test/" - self.flags(input_file=in_file) - self.flags(output=out_file) - m_out_pred.return_value = [{"value1": {"pred": 1}, "value2": {"pred": 0.9}}] - with mock.patch.object(sys, "argv", ["deepaas-predict"]): - execute.main() - - @mock.patch("deepaas.cmd.execute.prediction") - def test_execute_url(self, m_out_pred): - in_file = "https://xxxxxxxxxx" - out_file = "deepaas/tests/out_test/" - self.flags(input_file=in_file) - self.flags(output=out_file) - self.flags(url=True) - m_out_pred.return_value = [{"value1": {"pred": 1}, "value2": {"pred": 0.9}}] - with mock.patch.object(sys, "argv", ["deepaas-predict"]): - execute.main() - - @mock.patch("deepaas.cmd.execute.prediction") - def test_execute_ct(self, m_out_pred): - in_file = "file" - out_file = "deepaas/tests/out_test/" - cont_type = "application/zip" - self.flags(input_file=in_file) - self.flags(output=out_file) - self.flags(content_type=cont_type) - output_dir = "deepaas/tests/tmp_dir" - if os.path.exists(output_dir): - shutil.rmtree(output_dir) - os.mkdir(output_dir) - out_json = [{"value1": {"pred": 1}, "value2": {"pred": 0.9}}] - f_json = open(output_dir + "output" + ".json", "w+") - f_json.write(repr(out_json) + "\n") - f_json.close - url = "https://storage.googleapis.com/" "tfjs-models/assets/posenet/frisbee.jpg" - urllib.request.urlretrieve(url, "example_image.jpg") - shutil.move(f_json.name, output_dir) - shutil.move("example_image.jpg", output_dir) - f = shutil.make_archive(base_name=output_dir, format="zip", root_dir=output_dir) - m_out_pred.return_value = open(f, "rb") - with mock.patch.object(sys, "argv", ["deepaas-predict"]): - execute.main() - shutil.rmtree(output_dir) - os.remove(output_dir + ".zip") diff --git a/doc/source/cli/deepaas-predict.rst b/doc/source/cli/deepaas-predict.rst deleted file mode 100644 index 469395d3..00000000 --- a/doc/source/cli/deepaas-predict.rst +++ /dev/null @@ -1,63 +0,0 @@ -================ -deepaas-predict -================ - -Synopsis -======== - -:program:`deepaas-predict` [options] - -Description -=========== - -:program:`deepaas-predict` It is a command that allows you to obtain, - through the command line, the prediction of a file or the url of - a file, of the models that are loaded through the ``deepaas.v2.models`` - entrypoint API. - -Options -======= - -.. option:: --input-file INPUT_FILE, -i INPUT_FILE - - Set local input file to predict. This option is required. - -.. option:: --content-type CONTENT_TYPE, -c CONTENT_TYPE - - Especify the content type of the output file. The selected - option must be available in the model used. - (by default application/json). - -.. option:: --model-name MODEL_NAME - - Specify the model to be used. If not specified, DEEPaaS will serve all the models - that are available. If specified, DEEPaaS will serve only the specified model. You - can also use the DEEPAAS_V2_MODEL environment variable. - - WARNING: Serving multiple models is deprecated and will be removed in the future, - therefore it is strongly suggested that you specify the model you want to or that - you ensure that only one model is available. - -.. option:: --output OUTPUT_DIR, -o OUTPUT_DIR - - Save the result to a local file. This option is required. - -.. option:: --url, -u - - If set to true, the input file is the url o a file to predict. - -Files -===== - -None - -See Also -======== - -Documentation: `DEEPaaS API `_ - -Reporting Bugs -============== - -Bugs are managed at `GitHub `_ - diff --git a/doc/source/cli/index.rst b/doc/source/cli/index.rst index 772d3c29..44130f8a 100644 --- a/doc/source/cli/index.rst +++ b/doc/source/cli/index.rst @@ -6,5 +6,4 @@ Available commands :maxdepth: 1 deepaas-cli - deepaas-predict deepaas-run diff --git a/doc/source/user/predict.rst b/doc/source/user/predict.rst deleted file mode 100644 index dc7c9db7..00000000 --- a/doc/source/user/predict.rst +++ /dev/null @@ -1,36 +0,0 @@ -.. _predict: - -DEEPaaS API as a command line action -==================================== - -Support for execution from the command line for DEEPaaS. In your Dockerfile, -you must ensure that you execute `` deepaas-predict`` in one of the following ways: - -Basic form of execution. - -.. code-block:: console - - (...) - CMD ["sh", "-c", "deepaas-predict -i INPUT_FILE -o OUTPUT_DIR"] - -Execution specifying a content type option. - -.. code-block:: console - - (...) - CMD ["sh", "-c", "deepaas-predict -i INPUT_FILE -c CONTENT_TYPE -o OUTPUT_DIR"] - -Execution specifying an url as an input file. - -.. code-block:: console - - (...) - CMD ["sh", "-c", "deepaas-predict -i INPUT_FILE --url -o OUTPUT_DIR"] - -Execution specifying the name of a model. - -.. code-block:: console - - (...) - CMD ["sh", "-c", "deepaas-predict -i INPUT_FILE --model-name MODEL_NAME -o OUTPUT_DIR"] - diff --git a/pyproject.toml b/pyproject.toml index fb6fbf07..55c5dea5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -40,7 +40,6 @@ classifiers = [ [tool.poetry.scripts] deepaas-run = "deepaas.cmd.run:main" -deepaas-predict = "deepaas.cmd.execute:main" deepaas-cli = "deepaas.cmd.cli:main" [tool.poetry.plugins] # Optional super table diff --git a/releasenotes/notes/deprecate-deepaas-predict-f92939d443cb1904.yaml b/releasenotes/notes/deprecate-deepaas-predict-f92939d443cb1904.yaml index 7560650e..512edf4e 100644 --- a/releasenotes/notes/deprecate-deepaas-predict-f92939d443cb1904.yaml +++ b/releasenotes/notes/deprecate-deepaas-predict-f92939d443cb1904.yaml @@ -1,4 +1,4 @@ --- -deprecations: +upgrade: - | - The command ``deepaas-predict`` is now deprecated, please use ``deepaas-cli`` instead. + The command ``deepaas-predict`` has been removed, please use ``deepaas-cli`` instead.