Skip to content

Commit 15cb3f2

Browse files
authored
Ability to add a model to a kernel (#460)
http://b/270962570
1 parent 49057db commit 15cb3f2

File tree

6 files changed

+70
-50
lines changed

6 files changed

+70
-50
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
Changelog
22
====
33

4+
#### 1.5.13
5+
Release date: 2/27/22
6+
* Add ability to add a model to a kernel
7+
48
### 1.5.12
59
Release date: 03/12/21
610
* No changes

KaggleSwagger.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,6 +1150,11 @@ definitions:
11501150
description: A list of kernel data sources that the kernel should use. Each dataset is specified as `USERNAME/KERNEL-SLUG`
11511151
items:
11521152
type: string
1153+
modelDataSources:
1154+
type: array
1155+
description: A list of model data sources that the kernel should use. Each model is specified as `USERNAME/MODEL-SLUG/FRAMEWORK/VARIATION-SLUG/VERSION-NUMBER`
1156+
items:
1157+
type: string
11531158
categoryIds:
11541159
type: array
11551160
description: A list of tag IDs to associated with the kernel

kaggle/api/kaggle_api_extended.py

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,3 @@
1-
#!/usr/bin/python
2-
#
3-
# Copyright 2020 Kaggle Inc
4-
#
5-
# Licensed under the Apache License, Version 2.0 (the "License");
6-
# you may not use this file except in compliance with the License.
7-
# You may obtain a copy of the License at
8-
#
9-
# http://www.apache.org/licenses/LICENSE-2.0
10-
#
11-
# Unless required by applicable law or agreed to in writing, software
12-
# distributed under the License is distributed on an "AS IS" BASIS,
13-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14-
# See the License for the specific language governing permissions and
15-
# limitations under the License.
16-
171
#!/usr/bin/python
182
#
193
# Copyright 2019 Kaggle Inc
@@ -82,7 +66,7 @@
8266

8367

8468
class KaggleApi(KaggleApi):
85-
__version__ = '1.5.12'
69+
__version__ = '1.5.13'
8670

8771
CONFIG_NAME_PROXY = 'proxy'
8872
CONFIG_NAME_COMPETITION = 'competition'
@@ -1763,6 +1747,7 @@ def kernels_initialize(self, folder):
17631747
'dataset_sources': [],
17641748
'competition_sources': [],
17651749
'kernel_sources': [],
1750+
'model_sources': [],
17661751
}
17671752
meta_file = os.path.join(folder, self.KERNEL_METADATA_FILE)
17681753
with open(meta_file, 'w') as f:
@@ -1856,6 +1841,10 @@ def kernels_push(self, folder):
18561841
for source in kernel_sources:
18571842
self.validate_kernel_string(source)
18581843

1844+
model_sources = self.get_or_default(meta_data, 'model_sources', [])
1845+
for source in model_sources:
1846+
self.validate_model_string(source)
1847+
18591848
docker_pinning_type = self.get_or_default(meta_data,
18601849
'docker_image_pinning_type',
18611850
None)
@@ -1891,6 +1880,7 @@ def kernels_push(self, folder):
18911880
competition_data_sources=self.get_or_default(
18921881
meta_data, 'competition_sources', []),
18931882
kernel_data_sources=kernel_sources,
1883+
model_data_sources=model_sources,
18941884
category_ids=self.get_or_default(meta_data, 'keywords', []),
18951885
docker_image_pinning_type=docker_pinning_type)
18961886

@@ -2068,6 +2058,8 @@ def kernels_pull(self, kernel, path, metadata=False, quiet=True):
20682058
'kernel_sources')
20692059
self.set_if_present(server_metadata, 'competitionDataSources',
20702060
data, 'competition_sources')
2061+
self.set_if_present(server_metadata, 'modelDataSources', data,
2062+
'model_sources')
20712063
with open(metadata_path, 'w') as f:
20722064
json.dump(data, f, indent=2)
20732065

@@ -2537,6 +2529,24 @@ def validate_kernel_string(self, kernel):
25372529
raise ValueError(
25382530
'Kernel slug must be at least five characters')
25392531

2532+
def validate_model_string(self, model):
2533+
""" determine if a model string is valid, meaning it is in the format
2534+
of {username}/{model-slug}.
2535+
Parameters
2536+
==========
2537+
model: the model name to validate
2538+
"""
2539+
if model:
2540+
if '/' not in model:
2541+
raise ValueError(
2542+
'Model must be specified in the form of '
2543+
'\'{username}/{model-slug}/{framework}/{variation-slug}/{version-number}\''
2544+
)
2545+
2546+
split = model.split('/')
2547+
if not split[0] or not split[1]:
2548+
raise ValueError('Invalid model specification ' + model)
2549+
25402550
def validate_resources(self, folder, resources):
25412551
""" validate resources is a wrapper to validate the existence of files
25422552
and that there are no duplicates for a folder and set of resources.
@@ -2609,6 +2619,7 @@ def convert_to_dataset_file_metadata(self, file_data, path):
26092619

26102620

26112621
class TqdmBufferedReader(io.BufferedReader):
2622+
26122623
def __init__(self, raw, progress_bar):
26132624
""" helper class to implement an io.BufferedReader
26142625
Parameters

kaggle/models/kaggle_models_extended.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,3 @@
1-
#!/usr/bin/python
2-
#
3-
# Copyright 2020 Kaggle Inc
4-
#
5-
# Licensed under the Apache License, Version 2.0 (the "License");
6-
# you may not use this file except in compliance with the License.
7-
# You may obtain a copy of the License at
8-
#
9-
# http://www.apache.org/licenses/LICENSE-2.0
10-
#
11-
# Unless required by applicable law or agreed to in writing, software
12-
# distributed under the License is distributed on an "AS IS" BASIS,
13-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14-
# See the License for the specific language governing permissions and
15-
# limitations under the License.
16-
171
#!/usr/bin/python
182
#
193
# Copyright 2019 Kaggle Inc
@@ -36,6 +20,7 @@
3620

3721

3822
class Competition(object):
23+
3924
def __init__(self, init_dict):
4025
parsed_dict = {k: parse(v) for k, v in init_dict.items()}
4126
self.__dict__.update(parsed_dict)
@@ -46,6 +31,7 @@ def __repr__(self):
4631

4732

4833
class SubmitResult(object):
34+
4935
def __init__(self, init_dict):
5036
parsed_dict = {k: parse(v) for k, v in init_dict.items()}
5137
self.__dict__.update(parsed_dict)
@@ -55,6 +41,7 @@ def __repr__(self):
5541

5642

5743
class Submission(object):
44+
5845
def __init__(self, init_dict):
5946
parsed_dict = {k: parse(v) for k, v in init_dict.items()}
6047
self.__dict__.update(parsed_dict)
@@ -68,6 +55,7 @@ def __repr__(self):
6855

6956

7057
class LeaderboardEntry(object):
58+
7159
def __init__(self, init_dict):
7260
parsed_dict = {k: parse(v) for k, v in init_dict.items()}
7361
self.__dict__.update(parsed_dict)
@@ -77,6 +65,7 @@ def __repr__(self):
7765

7866

7967
class Dataset(object):
68+
8069
def __init__(self, init_dict):
8170
parsed_dict = {k: parse(v) for k, v in init_dict.items()}
8271
self.__dict__.update(parsed_dict)
@@ -90,6 +79,7 @@ def __repr__(self):
9079

9180

9281
class Metadata(object):
82+
9383
def __init__(self, init_info):
9484
parsed_info = {k: parse(v) for k, v in init_info.items()}
9585
# backwards compatibility
@@ -102,6 +92,7 @@ def __repr__(self):
10292

10393

10494
class DatasetVersion(object):
95+
10596
def __init__(self, init_dict):
10697
parsed_dict = {k: parse(v) for k, v in init_dict.items()}
10798
self.__dict__.update(parsed_dict)
@@ -111,6 +102,7 @@ def __repr__(self):
111102

112103

113104
class File(object):
105+
114106
def __init__(self, init_dict):
115107
parsed_dict = {k: parse(v) for k, v in init_dict.items()}
116108
self.__dict__.update(parsed_dict)
@@ -130,6 +122,7 @@ def get_size(size, precision=0):
130122

131123

132124
class Tag(object):
125+
133126
def __init__(self, init_dict):
134127
parsed_dict = {k: parse(v) for k, v in init_dict.items()}
135128
self.__dict__.update(parsed_dict)
@@ -139,6 +132,7 @@ def __repr__(self):
139132

140133

141134
class FileUploadInfo(object):
135+
142136
def __init__(self, init_dict):
143137
parsed_dict = {k: parse(v) for k, v in init_dict.items()}
144138
self.__dict__.update(parsed_dict)
@@ -148,6 +142,7 @@ def __repr__(self):
148142

149143

150144
class DatasetNewVersionResponse(object):
145+
151146
def __init__(self, init_dict):
152147
parsed_dict = {k: parse(v) for k, v in init_dict.items()}
153148
self.__dict__.update(parsed_dict)
@@ -157,6 +152,7 @@ def __repr__(self):
157152

158153

159154
class DatasetNewResponse(object):
155+
160156
def __init__(self, init_dict):
161157
parsed_dict = {k: parse(v) for k, v in init_dict.items()}
162158
self.__dict__.update(parsed_dict)
@@ -166,6 +162,7 @@ def __repr__(self):
166162

167163

168164
class ListFilesResult(object):
165+
169166
def __init__(self, init_dict):
170167
self.error_message = init_dict['errorMessage']
171168
files = init_dict['datasetFiles']
@@ -179,6 +176,7 @@ def __repr__(self):
179176

180177

181178
class Kernel:
179+
182180
def __init__(self, init_dict):
183181
parsed_dict = {k: parse(v) for k, v in init_dict.items()}
184182
self.__dict__.update(parsed_dict)
@@ -188,6 +186,7 @@ def __repr__(self):
188186

189187

190188
class KernelPushResponse(object):
189+
191190
def __init__(self, init_dict):
192191
parsed_dict = {k: parse(v) for k, v in init_dict.items()}
193192
self.__dict__.update(parsed_dict)

kaggle/tests/test_authenticate.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
#!/usr/bin/python
2-
#
3-
# Copyright 2020 Kaggle Inc
4-
#
5-
# Licensed under the Apache License, Version 2.0 (the "License");
6-
# you may not use this file except in compliance with the License.
7-
# You may obtain a copy of the License at
8-
#
9-
# http://www.apache.org/licenses/LICENSE-2.0
10-
#
11-
# Unless required by applicable law or agreed to in writing, software
12-
# distributed under the License is distributed on an "AS IS" BASIS,
13-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14-
# See the License for the specific language governing permissions and
15-
# limitations under the License.
16-
1+
#!/usr/bin/python
2+
#
3+
# Copyright 2020 Kaggle Inc
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
1717
from kaggle.api.kaggle_api_extended import KaggleApi
1818

1919
# python -m unittest tests.test_authenticate
@@ -23,6 +23,7 @@
2323

2424

2525
class TestAuthenticate(unittest.TestCase):
26+
2627
def setUp(self):
2728
print("setup class:%s" % self)
2829

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
setup(
2121
name='kaggle',
22-
version='1.5.12',
22+
version='1.5.13',
2323
description='Kaggle API',
2424
long_description=
2525
('Official API for https://www.kaggle.com, accessible using a command line '

0 commit comments

Comments
 (0)