Skip to content

Commit 15172a7

Browse files
author
Johannes Hötter
committed
implements sdk improvements of stresstest
1 parent 227db45 commit 15172a7

File tree

3 files changed

+31
-16
lines changed

3 files changed

+31
-16
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Once you installed the package, you can create a `Client` object from any Python
3232
```python
3333
from kern import Client
3434

35-
user_name = "your-username"
35+
user_name = "your-username" # this is the email you log in with
3636
password = "your-password"
3737
project_id = "your-project-id" # can be found in the URL of the web application
3838

@@ -111,6 +111,8 @@ file_path = "my/file/path/data.json"
111111
upload_was_successful = client.post_file_import(file_path)
112112
```
113113

114+
We use Pandas to process the data you upload, so you can also provide `import_file_options` for the file type you use. Currently, you need to provide them as a `\n`-separated string (e.g. `"quoting=1\nsep=';'"`). We'll adapt this in the future to work with dictionaries instead.
115+
114116
Alternatively, you can `kern push <path-to-your-file>` via CLI, given that you have provided the `secrets.json` file in the same directory.
115117

116118
**Make sure that you've selected the correct project beforehand, and fit the data schema of existing records in your project!**

kern/__init__.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Client:
1414
"""Client object which can be used to directly address the Kern AI API.
1515
1616
Args:
17-
user_name (str): Your username for the application.
17+
user_name (str): Your username (email) for the application.
1818
password (str): The respective password. Do not share this!
1919
project_id (str): The link to your project. This can be found in the URL in an active project.
2020
uri (str, optional): Link to the host of the application. Defaults to "https://app.kern.ai".
@@ -26,6 +26,7 @@ class Client:
2626
def __init__(
2727
self, user_name: str, password: str, project_id: str, uri=settings.DEFAULT_URI
2828
):
29+
msg.info(f"Connecting to {uri}")
2930
settings.set_base_uri(uri)
3031
self.session_token = authentication.create_session_token(
3132
user_name=user_name, password=password
@@ -37,25 +38,33 @@ def __init__(
3738
raise exceptions.get_api_exception_class(401)
3839
self.project_id = project_id
3940

41+
self.get_project_details()
42+
4043
@classmethod
41-
def from_secrets_file(cls, path_to_file: str):
44+
def from_secrets_file(cls, path_to_file: str, project_id: Optional[str] = None):
4245
"""Creates a Client object from a secrets file.
4346
4447
Args:
4548
path_to_file (str): Path to the secrets file.
49+
project_id (Optional[str], optional): The link to your project. This can be found in the URL in an active project. Defaults to None. In that case, it will read the project id from the file
4650
4751
Returns:
48-
Client: kern.Client object.
52+
kern.Client: Client object.
4953
"""
5054
with open(path_to_file, "r") as file:
5155
content = json.load(file)
56+
5257
uri = content.get("uri")
5358
if uri is None:
5459
uri = settings.DEFAULT_URI
60+
61+
if project_id is None:
62+
project_id = content["project_id"]
63+
5564
return cls(
5665
user_name=content["user_name"],
5766
password=content["password"],
58-
project_id=content["project_id"],
67+
project_id=project_id,
5968
uri=uri,
6069
)
6170

@@ -116,12 +125,9 @@ def get_record_export(
116125

117126
if tokenize:
118127
tokenize_attributes = []
119-
for column in df.columns:
120-
if "__confidence" in column:
121-
dtype = type(df[column].iloc[0])
122-
if dtype == list:
123-
attribute = column.split("__")[0]
124-
tokenize_attributes.append(attribute)
128+
for attribute in self.get_project_details()["attributes"]:
129+
if attribute["data_type"] == "TEXT":
130+
tokenize_attributes.append(attribute["name"])
125131

126132
if len(tokenize_attributes) > 0:
127133
tokenizer_package = self.get_project_details()["tokenizer"]
@@ -142,7 +148,7 @@ def get_record_export(
142148
)
143149

144150
else:
145-
msg.info("No tokenization necessary.")
151+
msg.warn("There are no attributes that can be tokenized in this project.")
146152

147153
if download_to is not None:
148154
df.to_json(download_to, orient="records")

kern/exceptions.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
class SDKError(Exception):
66
def __init__(self, message: Optional[str] = None):
77
if message is None:
8-
message = (
9-
"Please check the SDK documentation at https://docs.kern.ai/reference."
10-
)
8+
message = "Please check the SDK documentation at https://github.com/code-kern-ai/kern-python"
119
super().__init__(message)
1210

1311

@@ -20,6 +18,10 @@ class UnauthorizedError(SDKError):
2018
class NotFoundError(SDKError):
2119
pass
2220

21+
class UnknownProjectError(SDKError):
22+
def __init__(self, project_id: str):
23+
super().__init__(message=f"Could not find project '{project_id}'. Please check your input.")
24+
2325

2426
# 500 Server Error
2527
class InternalServerError(SDKError):
@@ -29,10 +31,15 @@ class InternalServerError(SDKError):
2931
class FileImportError(Exception):
3032
pass
3133

34+
# mirror this from the rest api class ErrorCodes
35+
class ErrorCodes:
36+
UNRECOGNIZED_USER = "UNRECOGNIZED_USER" # not actively used in SDK
37+
PROJECT_NOT_FOUND = "PROJECT_NOT_FOUND"
38+
3239

3340
RESPONSE_CODES_API_EXCEPTION_MAP = {
3441
401: {"*": UnauthorizedError},
35-
404: {"*": NotFoundError},
42+
404: {"*": NotFoundError, ErrorCodes.PROJECT_NOT_FOUND: UnknownProjectError},
3643
500: {"*": InternalServerError},
3744
}
3845

0 commit comments

Comments
 (0)