Skip to content
This repository was archived by the owner on Apr 15, 2024. It is now read-only.

Commit ad59739

Browse files
committed
Added poetry
Tweaked tests, linting, pypint, and build scripts for poetry Remove old ModelClient Added thin Model client for new Model registry
1 parent dcb2894 commit ad59739

File tree

15 files changed

+3485
-161
lines changed

15 files changed

+3485
-161
lines changed

.devcontainer/devcontainer.json

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{
2+
"name": "cortex-python",
3+
"image": "mcr.microsoft.com/devcontainers/python:0-3.11",
4+
// "build": {
5+
// "dockerfile": "Dockerfile",
6+
// "context": "..",
7+
// "args": {
8+
// // Update 'VARIANT' to pick a Python version: 3, 3.10, 3.9, 3.8, 3.7, 3.6
9+
// // Append -bullseye or -buster to pin to an OS version.
10+
// // Use -bullseye variants on local on arm64/Apple Silicon.
11+
// "VARIANT": "3.10-bullseye",
12+
// // Options
13+
// "NODE_VERSION": "lts/*"
14+
// }
15+
// },
16+
17+
// Configure tool-specific properties.
18+
"customizations": {
19+
// Configure properties specific to VS Code.
20+
"vscode": {
21+
// Set *default* container specific settings.json values on container create.
22+
"settings": {
23+
"python.defaultInterpreterPath": "/usr/local/bin/python",
24+
"python.linting.enabled": true,
25+
"python.linting.pylintEnabled": true,
26+
"python.formatting.autopep8Path": "/usr/local/py-utils/bin/autopep8",
27+
"python.formatting.blackPath": "/usr/local/py-utils/bin/black",
28+
"python.formatting.yapfPath": "/usr/local/py-utils/bin/yapf",
29+
"python.linting.banditPath": "/usr/local/py-utils/bin/bandit",
30+
"python.linting.flake8Path": "/usr/local/py-utils/bin/flake8",
31+
"python.linting.mypyPath": "/usr/local/py-utils/bin/mypy",
32+
"python.linting.pycodestylePath": "/usr/local/py-utils/bin/pycodestyle",
33+
"python.linting.pydocstylePath": "/usr/local/py-utils/bin/pydocstyle",
34+
"python.linting.pylintPath": "/usr/local/py-utils/bin/pylint"
35+
},
36+
37+
// Add the IDs of extensions you want installed when the container is created.
38+
"extensions": [
39+
"ms-python.python",
40+
"ms-python.vscode-pylance"
41+
]
42+
}
43+
},
44+
45+
// Use 'forwardPorts' to make a list of ports inside the container available locally.
46+
// "forwardPorts": [],
47+
48+
// Use 'postCreateCommand' to run commands after the container is created.
49+
"postCreateCommand": "pip3 install --user -r requirements-dev.txt",
50+
51+
// Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
52+
"remoteUser": "vscode"
53+
}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ dist
1414
.python-version
1515
.pytest_cache
1616
coverage/
17+
coverage.xml
1718
.coverage
1819
test/report.html
1920
.idea

pylintrc renamed to .pylintrc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
[MAIN]
2-
32
# Analyse import fallback blocks. This can be used to support both Python 2 and
43
# 3 compatible code, which means that the block might have code that exists
54
# only in one or another interpreter, leading to false positives when analysed.
@@ -281,7 +280,7 @@ ignored-parents=
281280
max-args=10
282281

283282
# Maximum number of attributes for a class (see R0902).
284-
max-attributes=7
283+
max-attributes=10
285284

286285
# Maximum number of boolean expressions in an if statement (see R0916).
287286
max-bool-expr=5
@@ -330,7 +329,7 @@ indent-after-paren=4
330329
indent-string=' '
331330

332331
# Maximum number of characters on a single line.
333-
max-line-length=100
332+
max-line-length=200
334333

335334
# Maximum number of lines in a module.
336335
max-module-lines=1000

Dockerfile.c12e-ci.cortex-python-lib

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ COPY ./cortex /tmp/src/cortex
66
COPY ./scripts/entrypoint.sh /entrypoint.sh
77
COPY setup.py README.md /tmp/src/
88
RUN cd /tmp/src\
9-
&& pip install . flask fastapi uvicorn typing\
9+
&& pip install . flask fastapi uvicorn typing poetry\
1010
&& echo "default:x:1001:0:Default Application User:/app:/sbin/nologin" >> /etc/passwd\
1111
&& rm -r /tmp/src
1212
USER default

Makefile

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ clean:
1111
rm -rf ./cortex-python.docs.tgz
1212

1313
dev.install:
14-
pip install -r requirements-dev.txt
15-
pip install -r docs/requirements-docs.txt
16-
pip install -e .
14+
poetry install
1715

1816
ifdef ALPHA_BUILD
1917
build: build.alpha
@@ -41,11 +39,11 @@ build.release: clean
4139
python setup.py sdist bdist_wheel
4240

4341
dev.test:
44-
pylint --recursive=y cortex
45-
pytest --cache-clear --html=coverage/test-report.html --self-contained-html --cov=cortex/ --cov-report=html:coverage --cov-report=xml:coverage.xml --cov-report=term test/unit
42+
poetry run pylint --recursive=y cortex
43+
poetry run pytest --cache-clear --html=coverage/test-report.html --self-contained-html --cov=cortex/ --cov-report=html:coverage --cov-report=xml:coverage.xml --cov-report=term test/unit
4644

4745
test:
48-
tox -r # tox runs make dev.test internally
46+
poetry run tox -r # tox runs make dev.test internally
4947

5048
stage:
5149
git fetch --all
@@ -58,7 +56,7 @@ stage:
5856
git checkout develop
5957

6058
docs.dev:
61-
sphinx-build -b html -v docs docs/_build/
59+
poetry run sphinx-build -b html -v docs docs/_build/
6260

6361
# To be removed we don't really support older versions ATM anyway..
6462
docs.multi:
@@ -69,4 +67,4 @@ docs.package:
6967
tar -cvzf ${DISTRIBUTION_NAME}.docs.tgz -C docs/_build .
7068

7169
dev.push: build.alpha
72-
twine upload --repository-url https://upload.pypi.org/legacy/ dist/*
70+
poetry run twine upload --repository-url https://upload.pypi.org/legacy/ dist/*

cortex/__version__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
Metadata for cortex-python
33
"""
44
__title__ = "cortex-python"
5-
__description__ = "Python module for the CognitiveScale Cortex Cognitive Platform"
5+
__description__ = "Python module for the Tecnotree Sensa Platform"
66
__url__ = "https://github.com/CognitiveScale/cortex-python"
77
__version__ = "6.4.0"
8-
__author__ = "CognitiveScale"
9-
__author_email__ = "support@cognitivescale.com"
8+
__author__ = "Tecnotree"
9+
__author_email__ = "support@tecnotree.com"
1010
__license__ = "Apache 2.0"
11-
__copyright__ = "Copyright 2023 Cognitive Scale, Inc. All Rights Reserved."
11+
__copyright__ = "Copyright 2024 Tecnotree, Inc. All Rights Reserved."

cortex/client.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from .experiment.local import LocalExperiment
2121
from .connection import ConnectionClient
2222
from .content import ManagedContentClient
23-
from .model import ModelClient
23+
from .models import ModelClient
2424
from .secrets import SecretsClient
2525
from .session import SessionClient
2626
from .skill import SkillClient
@@ -125,6 +125,7 @@ def __init__(
125125
version: int = VERSION,
126126
verify_ssl_cert: bool = False,
127127
):
128+
self._serviceconnector = None
128129
self._token = token
129130
self._config = config
130131
self._project = project
@@ -151,7 +152,8 @@ def message(self, payload: dict, properties: dict = None) -> Message:
151152
:return: A Message object.
152153
"""
153154
if not self._token.token:
154-
self._token = _Token(generate_token(self._config))
155+
self._token = _Token(generate_token(self._serviceconnector._config)) # pylint: disable=protected-access
156+
155157
params = {"payload": payload}
156158
if properties:
157159
params["properties"] = properties

cortex/model.py

Lines changed: 0 additions & 124 deletions
This file was deleted.

cortex/models.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
"""
2+
Copyright 2024 Tecnotree, Inc. All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
https://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
16+
----
17+
Module to interact with the Sensa model registry
18+
"""
19+
20+
import os
21+
from typing import Optional, Dict, Any
22+
from cortex.serviceconnector import _Client
23+
try:
24+
import mlflow
25+
except ImportError:
26+
mlflow = None
27+
28+
29+
def check_installed():
30+
if mlflow is None:
31+
raise NotImplementedError(
32+
'Models SDK extra not installed, please run `pip install cortex-python[models_sdk]` to install')
33+
34+
class ModelClient(_Client):
35+
"""
36+
Client for model registry, this class requires the `models_sdk` extras to be installed
37+
"""
38+
def __init__(self, *args, **kwargs):
39+
super().__init__(*args, **kwargs)
40+
# Setup model registry by default
41+
self._setup_model_client()
42+
43+
def _setup_model_client(self):
44+
# Generate a JWT, this call stores the JWT in `_serviceconnector.jwt` ( meh )
45+
self._serviceconnector._construct_headers({}) # pylint: disable=protected-access
46+
47+
mlflow.set_tracking_uri(self._serviceconnector.url)
48+
os.environ['MLFLOW_TRACKING_URI'] = self._serviceconnector.url
49+
os.environ['MLFLOW_TRACKING_TOKEN'] = self._serviceconnector.token
50+
# detect cortex client setting to avoid invalid SSL cert errors
51+
# os.environ['MLFLOW_TRACKING_TOKEN']='true'
52+
# os.environ['MLFLOW_TRACKING_CLIENT_CERT_PATH']=
53+
# Need api to fetch serverside userid..
54+
# os.environ['MLFLOW_TRACKING_USERNAME']=_Client.???
55+
56+
def login(self):
57+
"""
58+
Configure connection settings for model registry.
59+
This also setup by `Cortex.client()`
60+
"""
61+
check_installed()
62+
self._setup_model_client()
63+
print("Configuring connection for model registry")
64+
65+
def create_experiment(self,
66+
name: str,
67+
tags: Optional[Dict[str, Any]] = None,
68+
) -> str:
69+
"""
70+
Create an MLFlow experiment with default tags
71+
:param name: experiment name, must be unique
72+
:param tags: optional experiment tags
73+
"""
74+
check_installed()
75+
if tags is None:
76+
tags = {}
77+
# default to client project if project tag isn't specified
78+
if tags.get('project') is None:
79+
tags['project'] = self._project()
80+
return mlflow.create_experiment(name, tags=tags)

docs/requirements-docs.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Sphinx==5.1.1
22
sphinx-multiversion==0.2.4
3-
GitPython>=3.1.40
3+
GitPython >=3.1.40
44
sphinx-rtd-theme==1.0.0
55
sphinxcontrib-restbuilder==0.3
66
nbconvert==6.5.3

0 commit comments

Comments
 (0)