Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to provide only predict or train endpoints #116

Merged
merged 3 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion deepaas/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@

async def get_app(
swagger=True,
enable_doc=True,
doc="/ui",
prefix="",
static_path="/static/swagger",
Expand Down Expand Up @@ -101,7 +102,7 @@ async def get_app(
basePath=base_path,
version=deepaas.__version__,
url="/swagger.json",
swagger_path=doc if doc else None,
swagger_path=doc if enable_doc else None,
prefix=prefix,
static_path=static_path,
in_place=True,
Expand Down
6 changes: 5 additions & 1 deletion deepaas/cmd/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ def main():

log.info("Starting DEEPaaS version %s", deepaas.__version__)

app = api.get_app(doc="/ui")
app = api.get_app(
enable_doc=CONF.doc_endpoint,
enable_train=CONF.train_endpoint,
enable_predict=CONF.predict_endpoint,
)
web.run_app(
app,
host=CONF.listen_ip,
Expand Down
24 changes: 23 additions & 1 deletion deepaas/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,36 @@
warnings.simplefilter("default", DeprecationWarning)

opts = [
cfg.BoolOpt(
"train-endpoint",
default=True,
help="""
Specify whether DEEPaaS should provide a train endpoint (default: True).
""",
),
cfg.BoolOpt(
"predict-endpoint",
default=True,
help="""
Specify whether DEEPaaS should provide a predict endpoint (default: True).
""",
),
cfg.BoolOpt(
"debug-endpoint",
default="false",
default=False,
help="""
Enable debug endpoint. If set we will provide all the information that you
print to the standard output and error (i.e. stdout and stderr) through the
"/debug" endpoint. Default is to not provide this information. This will not
provide logging information about the API itself.
""",
),
cfg.BoolOpt(
"doc-endpoint",
default=True,
help="""
Enable documentation endpoint. If set we will provide the documentation
through the "/ui" endpoint. Default is to provide this information.
""",
),
cfg.IntOpt(
Expand Down
122 changes: 122 additions & 0 deletions deepaas/tests/test_v2_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,128 @@ class Fake(object):
assert response is responses.Prediction


class TestApiV2NoTrain(base.TestCase):
async def get_application(self):
app = web.Application(debug=True)
app.middlewares.append(web.normalize_path_middleware())

deepaas.model.v2.register_models(app)

v2app = v2.get_app(enable_train=False)
app.add_subapp("/v2", v2app)

return app

def setUp(self):
super(TestApiV2NoTrain, self).setUp()

self.maxDiff = None

self.flags(debug=True)

def assert_ok(self, response):
self.assertIn(response.status, [200, 201])

async def test_not_found(self):
ret = await self.client.post("/v2/models/deepaas-test/train")
self.assertEqual(404, ret.status)

async def test_predict_data(self):
f = six.BytesIO(b"foo")
ret = await self.client.post(
"/v2/models/deepaas-test/predict/",
data={"data": (f, "foo.txt"), "parameter": 1},
)
json = await ret.json()
self.assertEqual(200, ret.status)
self.assertDictEqual(fake_responses.deepaas_test_predict, json)

async def test_get_metadata(self):
meta = fake_responses.models_meta

ret = await self.client.get("/v2/models/")
self.assert_ok(ret)
self.assertDictEqual(meta, await ret.json())

ret = await self.client.get("/v2/models/deepaas-test/")
self.assert_ok(ret)
self.assertDictEqual(meta["models"][0], await ret.json())


class TestApiV2NoPredict(base.TestCase):
async def get_application(self):
app = web.Application(debug=True)
app.middlewares.append(web.normalize_path_middleware())

deepaas.model.v2.register_models(app)

v2app = v2.get_app(enable_predict=False)
app.add_subapp("/v2", v2app)

return app

def setUp(self):
super(TestApiV2NoPredict, self).setUp()

self.maxDiff = None

self.flags(debug=True)

def assert_ok(self, response):
self.assertIn(response.status, [200, 201])

async def test_not_found(self):
ret = await self.client.post("/v2/models/deepaas-test/predict/")
self.assertEqual(404, ret.status)

async def test_train(self):
ret = await self.client.post(
"/v2/models/deepaas-test/train/", data={"sleep": 0}
)
self.assertEqual(200, ret.status)
json = await ret.json()
json.pop("date")
self.assertDictEqual(fake_responses.deepaas_test_train, json)

async def test_get_metadata(self):
meta = fake_responses.models_meta

ret = await self.client.get("/v2/models/")
self.assert_ok(ret)
self.assertDictEqual(meta, await ret.json())

ret = await self.client.get("/v2/models/deepaas-test/")
self.assert_ok(ret)
self.assertDictEqual(meta["models"][0], await ret.json())


class TestApiV2NoDoc(base.TestCase):
async def get_application(self):
app = web.Application(debug=True)
app.middlewares.append(web.normalize_path_middleware())

deepaas.model.v2.register_models(app)

v2app = v2.get_app(enable_doc=False)
app.add_subapp("/v2", v2app)

return app

def setUp(self):
super(TestApiV2NoDoc, self).setUp()

self.maxDiff = None

self.flags(debug=True)

def assert_ok(self, response):
self.assertIn(response.status, [200, 201])

async def test_not_found(self):
ret = await self.client.get("/ui")
self.assertEqual(404, ret.status)


class TestApiV2(base.TestCase):
async def get_application(self):
app = web.Application(debug=True)
Expand Down
8 changes: 8 additions & 0 deletions doc/source/cli/deepaas-run.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ Options
If set to true, the logging level will be set to DEBUG instead of the
default INFO level.

.. option:: --predict-endpoint

Specify whether DEEPaaS should provide a train endpoint (default: True).

.. option:: --train-endpoint

Specify whether DEEPaaS should provide a train endpoint (default: True).

.. option:: --debug-endpoint

Enable debug endpoint. If set we will provide all the information that you
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ deps =
pytest-aiohttp
pytest-cov>=4.0.0,<4.1
fixtures
reno
mock
testtools
-r{toxinidir}/requirements.txt
Expand Down