def _serve_sprite_image(self, request): run = request.args.get("run") if not run: return Respond( request, 'query parameter "run" is required', "text/plain", 400 ) name = request.args.get("name") if name is None: return Respond( request, 'query parameter "name" is required', "text/plain", 400 ) if run not in self.configs: return Respond( request, 'Unknown run: "%s"' % run, "text/plain", 400 ) config = self.configs[run] embedding_info = self._get_embedding(name, config) if not embedding_info or not embedding_info.sprite.image_path: return Respond( request, 'No sprite image file found for tensor "%s" in the config file "%s"' % (name, self.config_fpaths[run]), "text/plain", 400, ) fpath = os.path.expanduser(embedding_info.sprite.image_path) fpath = _rel_to_abs_asset_path(fpath, self.config_fpaths[run]) if not tf.io.gfile.exists(fpath) or tf.io.gfile.isdir(fpath): return Respond( request, '"%s" does not exist or is directory' % fpath, "text/plain", 400, ) f = tf.io.gfile.GFile(fpath, "rb") encoded_image_string = f.read() f.close() image_type = imghdr.what(None, encoded_image_string) mime_type = _IMGHDR_TO_MIMETYPE.get(image_type, _DEFAULT_IMAGE_MIMETYPE) return Respond(request, encoded_image_string, mime_type)
def _serve_bookmarks(self, request): run = request.args.get("run") if not run: return Respond( request, 'query parameter "run" is required', "text/plain", 400 ) name = request.args.get("name") if name is None: return Respond( request, 'query parameter "name" is required', "text/plain", 400 ) if run not in self.configs: return Respond( request, 'Unknown run: "%s"' % run, "text/plain", 400 ) config = self.configs[run] fpath = self._get_bookmarks_file_for_tensor(name, config) if not fpath: return Respond( request, 'No bookmarks file found for tensor "%s" in the config file "%s"' % (name, self.config_fpaths[run]), "text/plain", 400, ) fpath = _rel_to_abs_asset_path(fpath, self.config_fpaths[run]) if not tf.io.gfile.exists(fpath) or tf.io.gfile.isdir(fpath): return Respond( request, '"%s" not found, or is not a file' % fpath, "text/plain", 400, ) bookmarks_json = None with tf.io.gfile.GFile(fpath, "rb") as f: bookmarks_json = f.read() return Respond(request, bookmarks_json, "application/json")
def _serve_runs(self, request): """Returns a list of runs that have embeddings.""" return Respond(request, list(self.configs.keys()), 'application/json')
def _serve_file(self, file_path, request): """Returns a resource file.""" res_path = os.path.join(os.path.dirname(__file__), file_path) with open(res_path, 'rb') as read_file: mimetype = mimetypes.guess_type(file_path)[0] return Respond(request, read_file.read(), content_type=mimetype)
def _serve_tensor(self, request): run = request.args.get("run") if run is None: return Respond(request, 'query parameter "run" is required', "text/plain", 400) name = request.args.get("name") if name is None: return Respond(request, 'query parameter "name" is required', "text/plain", 400) num_rows = _parse_positive_int_param(request, "num_rows") if num_rows == -1: return Respond( request, "query parameter num_rows must be integer > 0", "text/plain", 400, ) if run not in self.configs: return Respond(request, 'Unknown run: "%s"' % run, "text/plain", 400) config = self.configs[run] tensor = self.tensor_cache.get((run, name)) if tensor is None: # See if there is a tensor file in the config. embedding = self._get_embedding(name, config) if embedding and embedding.tensor_path: fpath = _rel_to_abs_asset_path(embedding.tensor_path, self.config_fpaths[run]) if not tf.io.gfile.exists(fpath): return Respond( request, 'Tensor file "%s" does not exist' % fpath, "text/plain", 400, ) try: tensor = _read_tensor_tsv_file(fpath) except UnicodeDecodeError: tensor = _read_tensor_binary_file(fpath, embedding.tensor_shape) else: reader = self._get_reader_for_run(run) if not reader or not reader.has_tensor(name): return Respond( request, 'Tensor "%s" not found in checkpoint dir "%s"' % (name, config.model_checkpoint_path), "text/plain", 400, ) try: tensor = reader.get_tensor(name) except tf.errors.InvalidArgumentError as e: return Respond(request, str(e), "text/plain", 400) self.tensor_cache.set((run, name), tensor) if num_rows: tensor = tensor[:num_rows] if tensor.dtype != "float32": tensor = tensor.astype(dtype="float32", copy=False) data_bytes = tensor.tobytes() return Respond(request, data_bytes, "application/octet-stream")
def _serve_runs(self, request): """Returns a list of runs that have embeddings.""" self._update_configs() return Respond(request, list(self._configs.keys()), "application/json")