def _serve_config(self, request):
        run = request.args.get('run')
        if run is None:
            return Respond(request, 'query parameter "run" 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]
        return Respond(request, json_format.MessageToJson(config),
                       'application/json')
    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)

        reader = self._get_reader_for_run(run)
        config = self.configs[run]

        if reader is None:
            # See if there is a tensor file in the config.
            embedding = self._get_embedding(name, config)
            if not embedding or not embedding.tensor_path:
                return Respond(
                    request,
                    'Tensor %s has no tensor_path in the config' % name,
                    'text/plain', 400)
            if not file_io.file_exists(embedding.tensor_path):
                return Respond(
                    request,
                    'Tensor file %s does not exist' % embedding.tensor_path,
                    'text/plain', 400)
            tensor = _read_tensor_file(embedding.tensor_path)
        else:
            if 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 errors.InvalidArgumentError as e:
                return Respond(request, str(e), 'text/plain', 400)

        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_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)
        if not file_io.file_exists(fpath) or file_io.is_directory(fpath):
            return Respond(request,
                           '%s does not exist or is directory' % fpath,
                           'text/plain', 400)
        f = file_io.FileIO(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)
        if not file_io.file_exists(fpath) or file_io.is_directory(fpath):
            return Respond(request, '%s is not a file' % fpath, 'text/plain',
                           400)

        bookmarks_json = None
        with file_io.FileIO(fpath, 'rb') as f:
            bookmarks_json = f.read()
        return Respond(request, bookmarks_json, 'application/json')
    def _serve_metadata(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]
        fpath = self._get_metadata_file_for_tensor(name, config)
        if not fpath:
            return Respond(
                request,
                'No metadata file found for tensor %s in the config file %s' %
                (name, self.config_fpaths[run]), 'text/plain', 400)
        if not file_io.file_exists(fpath) or file_io.is_directory(fpath):
            return Respond(request, '%s is not a file' % fpath, 'text/plain',
                           400)

        num_header_rows = 0
        with file_io.FileIO(fpath, 'r') as f:
            lines = []
            # Stream reading the file with early break in case the file doesn't fit in
            # memory.
            for line in f:
                lines.append(line)
                if len(lines) == 1 and '\t' in lines[0]:
                    num_header_rows = 1
                if num_rows and len(lines) >= num_rows + num_header_rows:
                    break
        return Respond(request, ''.join(lines), 'text/plain')
 def _serve_runs(self, request):
     """Returns a list of runs that have embeddings."""
     return Respond(request, list(self.configs.keys()), 'application/json')