FlaskService

class elg.FlaskService(name: str)

Class to help the creation of an ELG compatible service from a python model. Extra dependencies need to be install to use the FlaskService class. Please run: pip install elg[flask].

to_json(obj)

Hook that can be overridden by subclasses to customise JSON encoding.

FlaskService can convert the following types to JSON by default, in addition to the types handled natively by json.dump:

  • date, time, datetime (via .isoformat())

  • uuid.UUID (converted to str)

  • any pydantic.BaseModel including the ELG message types (via .dict(by_alias=True, exclude_none=True))

  • anything Iterable (as a list)

  • any dataclass (converted to a dict via dataclasses.asdict)

  • anything with a __json__ or for_json method (which is expected to return a serializable type)

To handle other types, or to change the standard behaviour for any of the above types, subclasses can override this method, which will be called whenever an object other than a string, number, bool, list or dict must be serialized and is expected to return a JSON-serializable object to be used in place of the original, or None to fall back to the default behaviour.

The default implementation of this method always returns None.

Parameters

obj – the object to convert

Returns

a substitute object suitable for JSON serialization, or None to use the default behaviour.

run()

Method to start the flask app.

process()

Main request processing logic - accepts a JSON request and returns a JSON response.

process_request(request)

Method to process the request object. This method only calls the right process method regarding the type of the request.

process_text(request: elg.model.request.TextRequest.TextRequest)

Method to implement if the service takes text as input.

Parameters

request (TextRequest) – TextRequest object.

process_structured_text(request: elg.model.request.StructuredTextRequest.StructuredTextRequest)

Method to implement if the service takes structured text as input.

Parameters

request (StructuredTextRequest) – StructuredTextRequest object.

process_audio(request: elg.model.request.AudioRequest.AudioRequest)

Method to implement if the service takes audio as input.

Parameters

request (AudioRequest) – AudioRequest object.

classmethod create_requirements(requirements: List = [], path: Optional[str] = None)

Class method to create the correct requirements.txt file.

Parameters
  • requirements (List, optional) – List of required pip packages. Defaults to [].

  • path (str, optional) – Path where to generate the file. Defaults to None.

classmethod create_docker_files(required_files: List[str] = [], required_folders: List[str] = [], commands: List[str] = [], base_image: str = 'python:slim', path: Optional[str] = None, log_level: str = 'INFO', workers: int = 1, timeout: int = 30, worker_class: str = 'sync')

Class method to create the correct Dockerfile.

Parameters
  • required_files (List[str], optional) – List of files needed for the service. Defaults to [].

  • required_folders (List[str], optional) – List of folders needed for the service. Defaults to [].

  • commands (List[str], optional) – List off additional commands to run in the Dockerfile. Defaults to [].

  • base_image (str, optional) – Name of the base Docker image used in the Dockerfile. Defaults to ‘python:slim’.

  • path (str, optional) – Path where to generate the file. Defaults to None.

  • log_level (str, optional) – The minimum severity level from which logged messages should be displayed. Defaults to ‘INFO’.

  • workers (int, optional) – Number of Gunicorn workers. Defaults to 1.

  • timeout (int, optional) – Timeout value for the Gunicorn worker. Defaults to 30.

  • worker_class (str, optional) – Worker class value for the Gunicorn worker. Defaults to ‘sync’.

classmethod docker_build_image(tag: str, pull: bool = True, path: Optional[str] = None, **kwargs)

Class method to do docker build … in python.

classmethod docker_push_image(repository: str, tag: str, username: Optional[str] = None, password: Optional[str] = None, **kwargs)

Class method to do docker push … in python.