def info(self, job_id, sort_by, page_id): base_url = self.url name = self.workspace # GET /train/<name>/<job_id> r = requests.get(f"{base_url}/train/{name}/{job_id}", params={ 'sort_by': sort_by, 'page_id': page_id }, headers={"X-Token": self.token}) if r.status_code < 300: return r.json() elif 400 <= r.status_code < 500: err = Error.from_dict(r.json()) raise RequestError(err.message) else: err = Error.from_dict(r.json()) if err.error_code == 'UNFINISHED_COMMIT': raise RequestError( "`kaos train info` not available while job in state JOB_RUNNING" ) else: raise RequestError(r.text)
def upload_hyperparams(self, hyperparams_file=None, **kwargs): base_url = self.url name = self.workspace user = self.user kwargs['user'] = user if hyperparams_file: f = open(hyperparams_file, 'r').read() label = " Uploading (hyper)parameters bundle" else: f = json.dumps({}) label = None r = upload_with_progress_bar(f, f"{base_url}/data/{name}/params", kwargs, label=label) if r.status_code < 300: return r.json() elif 400 <= r.status_code < 500: err = Error.from_dict(r.json()) raise RequestError(err.message) else: raise RequestError(r.text)
def create(self, name): base_url = self.url user = self.user self.workspace_name_validation(name) name = name.lower() if self.exists_by_name(name): raise WorkspaceExistsError(name) # POST /workspace/<name> r = requests.post(f"{base_url}/workspace/{name}", params={"user": user}, headers={"X-Token": self.token}) if r.status_code < 300: # set workspace to state self.state_service.set(PACHYDERM, workspace=name) self.state_service.write() return r.json() elif 400 <= r.status_code < 500: err = Error.from_dict(r.json()) raise RequestError(err.message) else: raise RequestError(r.text)
def get_build_logs(self, job_id): base_url = self.url name = self.workspace # GET /train/<name>/<job_id>/logs r = requests.get(f"{base_url}/build/{name}/{job_id}/logs") if r.status_code >= 300: err = Error.from_json(r.json()) raise RequestError(err.message) return r.json()
def get_serve_logs(self, endpoint): base_url = self.url # GET /inference/<endpoint>/logs r = requests.get(f"{base_url}/inference/{endpoint}/logs") if r.status_code < 300: return r.json() elif 400 <= r.status_code < 500: err = Error.from_dict(r.json()) raise RequestError(err.message) else: raise RequestError(r.text)
def get_build_logs(self, job_id): base_url = self.url name = self.workspace # GET /notebook/<name>/build/<job_id>/logs r = requests.get(f"{base_url}/notebook/{name}/build/{job_id}/logs") if r.status_code < 300: return r.json() elif 400 <= r.status_code < 500: err = Error.from_dict(r.json()) raise RequestError(err.message) else: raise RequestError(r.text)
def list(self): base_url = self.url name = self.workspace # GET /train/<name> r = requests.get(f"{base_url}/train/{name}") if r.status_code < 300: return r.json() elif 400 <= r.status_code < 500: err = Error.from_dict(r.json()) raise RequestError(err.message) else: raise RequestError(r.text)
def kill_job(self, job_id): base_url = self.url workspace = self.workspace # GET /train/<workspace>/<job_id> r = requests.delete(f"{base_url}/train/{workspace}/{job_id}") if r.status_code < 300: return r.json() elif 400 <= r.status_code < 500: err = Error.from_dict(r.json()) raise RequestError(err.message) else: raise RequestError(r.text)
def delete(self, endpoint): base_url = self.url # DELETE /inference/<endpoint> r = requests.delete(f"{base_url}/inference/{endpoint}") # invalidate notebook cache invalidate_cache(SERVE_CACHE) if r.status_code < 300: return r.json() elif 400 <= r.status_code < 500: err = Error.from_dict(r.json()) raise RequestError(err.message) else: raise RequestError(r.text)
def get_build_logs(self, job_id): base_url = self.url name = self.workspace # GET /train/<name>/<job_id>/logs r = requests.get(f"{base_url}/inference/{name}/build/{job_id}/logs", headers={"X-Token": self.token}) if r.status_code < 300: return r.json() elif 400 <= r.status_code < 500: err = Error.from_dict(r.json()) raise RequestError(err.message) else: raise RequestError(r.text)
def info(self): base_url = self.url name = self.workspace # GET /workspace/<name> r = requests.get(f"{base_url}/workspace/{name}", headers={"X-Token": self.token}) if r.status_code < 300: return r.json() elif 400 <= r.status_code < 500: err = Error.from_dict(r.json()) raise RequestError(err.message) else: raise RequestError(r.text)
def delete(self, name): base_url = self.url # DELETE /notebook/<name>/<notebook> r = requests.delete(f"{base_url}/notebook/{name}", headers={"X-Token": self.token}) if 300 <= r.status_code < 400: raise RequestError(f"Error while deleting notebook: {r.text}") elif 400 <= r.status_code < 500: err = Error.from_dict(r.json()) raise RequestError(err.message) elif r.status_code == 500: raise RequestError(r.text) # invalidate workspace cache invalidate_cache(NOTEBOOK_CACHE, workspace=True)
def get_bundle(self, endpoint): base_url = self.url name = self.workspace # GET /inference/<name>/<endpoint>/bundle r = requests.get(f"{base_url}/inference/{name}/{endpoint}/bundle", headers={"X-Token": self.token}) if r.status_code < 300: return name, r.content elif 300 <= r.status_code < 400: raise NoServingJobsError() elif 400 <= r.status_code < 500: err = Error.from_dict(r.json()) raise RequestError(err.message) else: raise RequestError(r.text)
def upload_data_bundle(self, bundle_file, **kwargs): base_url = self.url name = self.workspace user = self.user kwargs['user'] = user with open(bundle_file, 'rb') as data: r = upload_with_progress_bar(data, f"{base_url}/data/{name}/features", kwargs, " Uploading data bundle") if r.status_code < 300: return r.json() elif 400 <= r.status_code < 500: err = Error.from_dict(r.json()) raise RequestError(err.message) else: raise RequestError(r.text)
def list(self, as_dict=True): base_url = self.url # GET /workspace r = requests.get(f"{base_url}/workspace", headers={"X-Token": self.token}) if 300 <= r.status_code < 500: err = Error.from_dict(r.json()) raise RequestError(err.message) elif r.status_code == 500: raise RequestError(r.text) data = r.json() if as_dict: data = [{"name": v} for v in data['names']] return data
def list(self): base_url = self.url name = self.workspace # GET /inference/<name> r = requests.get(f"{base_url}/inference/{name}", headers={"X-Token": self.token}) if r.status_code < 300: return Response.from_dict(r.json()).response elif 300 <= r.status_code < 400: raise NoServingJobsError() elif 400 <= r.status_code < 500: err = Error.from_dict(r.json()) raise RequestError(err.message) else: raise RequestError(r.text)
def upload_source_bundle(self, c, model_id, **kwargs): base_url = self.url name = self.workspace user = self.user kwargs['user'] = user with open(c, 'rb') as data: r = upload_with_progress_bar( data, f"{base_url}/inference/{name}/{model_id}", kwargs, " Uploading source bundle", self.token) if r.status_code < 300: return r.json() elif 400 <= r.status_code < 500: err = Error.from_dict(r.json()) raise RequestError(err.message) else: raise RequestError(r.text)
def provenance(self, out_dir, endpoint): base_url = self.url name = self.workspace # build output directory (default = workspace) prov_dir = build_dir(out_dir, name, 'provenance') # GET /inference/<name>/<endpoint>/provenance r = requests.get(f"{base_url}/inference/{name}/{endpoint}/provenance") if r.status_code < 300: out_fid = os.path.join(prov_dir, f"{endpoint}") return out_fid, r.json() elif 400 <= r.status_code < 500: err = Error.from_dict(r.json()) raise RequestError(err.message) else: raise RequestError(r.text)
def deploy(self, **kwargs): base_url = self.url name = self.workspace user = self.user kwargs['user'] = user r = requests.post(f"{base_url}/notebook/{name}", params=kwargs, headers={"X-Token": self.token}) if r.status_code < 300: return r.json() elif 300 <= r.status_code < 400: raise RequestError(f"Error while deploying notebook: {r.text}") elif 400 <= r.status_code < 500: err = Error.from_dict(r.json()) raise RequestError(err.message) else: raise RequestError(r.text)
def upload_manifest(self, manifest_file, **kwargs): base_url = self.url name = self.workspace user = self.user kwargs['user'] = user with open(manifest_file, 'rb') as manif_f: r = upload_with_progress_bar(manif_f, f"{base_url}/data/{name}/manifest", kwargs, " Uploading manifest bundle") if r.status_code < 300: return r.json() elif 400 <= r.status_code < 500: err = Error.from_dict(r.json()) raise RequestError(err.message) else: raise RequestError(r.text)
def provenance(self, out_dir, model_id): base_url = self.url name = self.workspace # build output directory (default = workspace) prov_dir = build_dir(out_dir, name, 'provenance') # GET /train/<name>/<model_id>/provenance r = requests.get(f"{base_url}/train/{name}/{model_id}/provenance", headers={"X-Token": self.token}) if r.status_code < 300: out_fid = os.path.join(prov_dir, f"model-{model_id}") return out_fid, r.json() elif 400 <= r.status_code < 500: err = Error.from_dict(r.json()) raise RequestError(err.message) else: raise RequestError(r.text)
def upload_source_bundle(self, c, **kwargs): base_url = self.url name = self.workspace user = self.user kwargs['user'] = user r = requests.post(f"{base_url}/notebook/{name}", data=open(c, 'rb').read(), params=kwargs, headers={"X-Token": self.token}) if r.status_code < 300: return r.json() elif 300 <= r.status_code < 400: raise RequestError(f"Error while uploading source bundle: {r.text}") elif 400 <= r.status_code < 500: err = Error.from_dict(r.json()) raise RequestError(err.message) else: raise RequestError(r.text)
def delete(self): base_url = self.url name = self.workspace # DELETE /workspace/<name> r = requests.delete(f"{base_url}/workspace/{name}", headers={"X-Token": self.token}) if 300 <= r.status_code < 500: err = Error.from_dict(r.json()) raise RequestError(err.message) elif r.status_code == 500: raise RequestError(r.text) # unset workspace (since killed) name = "" self.state_service.set(PACHYDERM, workspace=name) self.state_service.write() # invalidate workspace cache invalidate_cache(WORKSPACE_CACHE, workspace=True) return name
def get_bundle(self, job_id, include_code, include_data, include_model, model_id): base_url = self.url name = self.workspace # GET /train/<name>/<job_id>/bundle r = requests.get(f"{base_url}/train/{name}/{job_id}/bundle", params={ "include_code": include_code, "include_data": include_data, "include_model": include_model, "model_id": model_id }) if r.status_code < 300: return name, r.content elif 400 <= r.status_code < 500: err = Error.from_dict(r.json()) raise RequestError(err.message) else: raise RequestError(r.text)
def make_error_response(status_code, error_code, message): return Error(error_code=error_code, message=message).to_json(), status_code