Skip to content

Commit 6ef02f0

Browse files
gustavocidornelaswhoseoyster
authored andcommitted
Make PUT request for reference dataset upload
1 parent 898b0ee commit 6ef02f0

File tree

3 files changed

+80
-16
lines changed

3 files changed

+80
-16
lines changed

openlayer/__init__.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1648,9 +1648,24 @@ def upload_reference_dataset(
16481648
{"task_type": task_type.value, **dataset_config}
16491649
)
16501650

1651-
# TODO: Make POST request to upload dataset
1652-
print("Uploading reference dataset...")
1653-
print(dataset_data)
1651+
with tempfile.TemporaryDirectory() as tmp_dir:
1652+
# Copy relevant files to tmp dir
1653+
utils.write_yaml(dataset_data, f"{tmp_dir}/dataset_config.yaml")
1654+
shutil.copy(file_path, tmp_dir)
1655+
1656+
tar_file_path = os.path.join(tmp_dir, "tarfile")
1657+
with tarfile.open(tar_file_path, mode="w:gz") as tar:
1658+
tar.add(tmp_dir, arcname=os.path.basename("reference_dataset"))
1659+
1660+
self.api.upload(
1661+
endpoint=f"inference-pipelines/{inference_pipeline_id}",
1662+
file_path=tar_file_path,
1663+
object_name="tarfile",
1664+
body={},
1665+
storage_uri_key="referenceDatasetUri",
1666+
method="PUT",
1667+
)
1668+
print("Referece dataset uploaded!")
16541669

16551670
def upload_reference_dataframe(
16561671
self,

openlayer/api.py

Lines changed: 61 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,15 @@ def put_request(self, endpoint: str, body=None, files=None, data=None):
181181
data=data,
182182
)
183183

184-
def upload(self, endpoint: str, file_path: str, object_name: str = None, body=None):
184+
def upload(
185+
self,
186+
endpoint: str,
187+
file_path: str,
188+
object_name: str = None,
189+
body=None,
190+
method: str = "POST",
191+
storage_uri_key: str = "storageUri",
192+
):
185193
"""Generic method to upload data to the default storage medium and create the
186194
appropriate resource in the backend.
187195
"""
@@ -198,10 +206,18 @@ def upload(self, endpoint: str, file_path: str, object_name: str = None, body=No
198206
file_path=file_path,
199207
object_name=object_name,
200208
body=body,
209+
method=method,
210+
storage_uri_key=storage_uri_key,
201211
)
202212

203213
def upload_blob_s3(
204-
self, endpoint: str, file_path: str, object_name: str = None, body=None
214+
self,
215+
endpoint: str,
216+
file_path: str,
217+
object_name: str = None,
218+
body=None,
219+
method: str = "POST",
220+
storage_uri_key: str = "storageUri",
205221
):
206222
"""Generic method to upload data to S3 storage and create the appropriate resource
207223
in the backend.
@@ -235,13 +251,22 @@ def upload_blob_s3(
235251
)
236252

237253
if res.ok:
238-
body["storageUri"] = presigned_json["storageUri"]
239-
return self.post_request(f"{endpoint}", body=body)
254+
body[storage_uri_key] = presigned_json["storageUri"]
255+
if method == "POST":
256+
return self.post_request(f"{endpoint}", body=body)
257+
elif method == "PUT":
258+
return self.put_request(f"{endpoint}", body=body)
240259
else:
241260
self._raise_on_respose(res)
242261

243262
def upload_blob_gcs(
244-
self, endpoint: str, file_path: str, object_name: str = None, body=None
263+
self,
264+
endpoint: str,
265+
file_path: str,
266+
object_name: str = None,
267+
body=None,
268+
method: str = "POST",
269+
storage_uri_key: str = "storageUri",
245270
):
246271
"""Generic method to upload data to Google Cloud Storage and create the
247272
appropriate resource in the backend.
@@ -265,13 +290,22 @@ def upload_blob_gcs(
265290
timeout=constants.REQUESTS_TIMEOUT,
266291
)
267292
if res.ok:
268-
body["storageUri"] = presigned_json["storageUri"]
269-
return self.post_request(f"{endpoint}", body=body)
293+
body[storage_uri_key] = presigned_json["storageUri"]
294+
if method == "POST":
295+
return self.post_request(f"{endpoint}", body=body)
296+
elif method == "PUT":
297+
return self.put_request(f"{endpoint}", body=body)
270298
else:
271299
self._raise_on_respose(res)
272300

273301
def upload_blob_azure(
274-
self, endpoint: str, file_path: str, object_name: str = None, body=None
302+
self,
303+
endpoint: str,
304+
file_path: str,
305+
object_name: str = None,
306+
body=None,
307+
method: str = "POST",
308+
storage_uri_key: str = "storageUri",
275309
):
276310
"""Generic method to upload data to Azure Blob Storage and create the
277311
appropriate resource in the backend.
@@ -298,12 +332,23 @@ def upload_blob_azure(
298332
timeout=constants.REQUESTS_TIMEOUT,
299333
)
300334
if res.ok:
301-
body["storageUri"] = presigned_json["storageUri"]
302-
return self.post_request(f"{endpoint}", body=body)
335+
body[storage_uri_key] = presigned_json["storageUri"]
336+
if method == "POST":
337+
return self.post_request(f"{endpoint}", body=body)
338+
elif method == "PUT":
339+
return self.put_request(f"{endpoint}", body=body)
303340
else:
304341
self._raise_on_respose(res)
305342

306-
def transfer_blob(self, endpoint: str, file_path: str, object_name: str, body=None):
343+
def transfer_blob(
344+
self,
345+
endpoint: str,
346+
file_path: str,
347+
object_name: str,
348+
body=None,
349+
method: str = "POST",
350+
storage_uri_key: str = "storageUri",
351+
):
307352
"""Generic method to transfer data to the openlayer folder and create the appropriate
308353
resource in the backend when using a local deployment.
309354
"""
@@ -317,5 +362,8 @@ def transfer_blob(self, endpoint: str, file_path: str, object_name: str, body=No
317362
except OSError as exc:
318363
raise OpenlayerException(f"Directory {dir_path} cannot be created") from exc
319364
shutil.copyfile(file_path, blob_path)
320-
body["storageUri"] = presigned_json["storageUri"]
321-
return self.post_request(f"{endpoint}", body=body)
365+
body[storage_uri_key] = presigned_json["storageUri"]
366+
if method == "POST":
367+
return self.post_request(f"{endpoint}", body=body)
368+
elif method == "PUT":
369+
return self.put_request(f"{endpoint}", body=body)

openlayer/inference_pipelines.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class InferencePipeline:
1010
def __init__(self, json, upload, client, task_type):
1111
self._json = json
1212
self.id = json["id"]
13+
self.project_id = json["projectId"]
1314
self.upload = upload
1415
self.client = client
1516
self.taskType = task_type

0 commit comments

Comments
 (0)