def query_lit_server(url: Text, endpoint: Text, params: Optional[Dict[Text, Text]] = None, inputs: Optional[Any] = None, config: Optional[Any] = None) -> Any: """Query a LIT server from Python.""" # Pack data for LIT request data = {'inputs': inputs, 'config': config} # TODO(lit-dev): for open source, require HTTPS. if not url.startswith('http://'): url = 'http://' + url full_url = urllib.parse.urljoin(url, endpoint) # Use requests to handle URL params. rq = requests.Request('POST', full_url, params=params, data=serialize.to_json(data), headers={'Content-Type': 'application/json'}) rq = rq.prepare() # Convert to urllib request request = urllib.request.Request( url=rq.url, data=six.ensure_binary(rq.body) if rq.body else None, headers=rq.headers, method=rq.method) response = urlopen(request) if response.code != 200: raise IOError( f'Failed to query {rq.url}; response code {response.code}') # TODO(iftenney): handle bad server response, e.g. if corplogin is required # and the server sends a login page instead of a JSON response. response_bytes = response.read() return serialize.from_json(six.ensure_text(response_bytes))
def load(self, path: str): """Load and return additional previously-saved datapoints for this dataset. Args: path: The path to the persisted datapoint file. Returns: (IndexedDataset) A dataset containing the loaded data. """ if not path.endswith(LIT_FILE_EXTENSION): # Try to load data using the base load method. If any data is # returned, then use that. Otherwise try loading the lit json extension # data format. new_dataset = self._base.load(path) if new_dataset is not None: description = (f'{len(new_dataset)} examples from ' f'{path}\n{self._base.description()}') return IndexedDataset(base=new_dataset, id_fn=self.id_fn, description=description) path += LIT_FILE_EXTENSION with open(path, 'r') as fd: examples = [ cast(IndexedInput, serialize.from_json(line)) for line in fd.readlines() ] # Load the side-by-side spec if it exists on disk. spec_path = path + LIT_SPEC_EXTENSION if os.path.exists(spec_path): with open(spec_path, 'r') as fd: spec = serialize.from_json(fd.read()) description = (f'{len(examples)} examples from ' f'{path}\n{self._base.description()}') return IndexedDataset(base=self._base, indexed_examples=examples, spec=spec, description=description, id_fn=self.id_fn)
def _handler(handler, request): logging.info('Request received: %s', request.full_path) kw = request.args.to_dict() # The frontend needs "simple" data (e.g. NumPy arrays converted to lists), # but for requests from Python we may want to use the invertible encoding # so that datatypes from remote models are the same as local ones. response_simple_json = utils.coerce_bool( kw.pop('response_simple_json', True)) data = serialize.from_json(request.data) if len(request.data) else None outputs = fn(data, **kw) response_body = serialize.to_json(outputs, simple=response_simple_json) return handler.respond(request, response_body, 'application/json', 200)
def _handler(app: wsgi_app.App, request, environ): kw = request.args.to_dict() # The frontend needs "simple" data (e.g. NumPy arrays converted to lists), # but for requests from Python we may want to use the invertible encoding # so that datatypes from remote models are the same as local ones. response_simple_json = utils.coerce_bool( kw.pop('response_simple_json', True)) data = serialize.from_json(request.data) if len( request.data) else None # Special handling to dereference IDs. if data and 'inputs' in data.keys() and 'dataset_name' in kw: data['inputs'] = self._reconstitute_inputs( data['inputs'], kw['dataset_name']) outputs = fn(data, **kw) response_body = serialize.to_json(outputs, simple=response_simple_json) return app.respond(request, response_body, 'application/json', 200)
def bytes_to_lit_example(input_bytes: bytes) -> Optional[JsonDict]: """Convert bytes representation to LIT example.""" return serialize.from_json(input_bytes.decode('utf-8'))