Service

class elg.service.Service(id: int, resource_name: str, resource_short_name: List[str], resource_type: str, entity_type: str, description: str, keywords: List[str], detail: str, licences: List[str], languages: List[str], country_of_registration: List[str], creation_date: str, last_date_updated: str, functional_service: bool, functions: List[str], intended_applications: List[str], views: int, downloads: int, size: int, service_execution_count: int, status: str, under_construction: bool, record: dict, auth_object: elg.authentication.Authentication, auth_file: str, scope: str, domain: str, use_cache: bool, cache_dir: str)

Class to use ELG service. Run an ELG service directly from python.

Examples:

from elg import Service

# You can initialize a service from its id. You will be asked to authenticate on the ELG website.
service = Service.from_id(474)

# You can then directly run the service.
result = service("Nikolas Tesla lives in Berlin.")
print(f"\nResult:\n{result}")

# You can also create a service from a catalog search result.

# First you need to search for a service using the catalog. Let's search an English to French Machine Translation service.
from elg import Catalog

catalog = Catalog()
results = catalog.search(
    resource = "Tool/Service",
    function = "Machine Translation",
    languages = ["en", "fr"],
    limit = 1,
)

# Now you can initialize the service using the first result. You will not be asked to authenticate because your token has been cached.
service = Service.from_entity(results[0])

# And run the service as before.
result = service("ELG is an amazing project.")
print(f"\nResult:\n{result}")

# It is possible to use a file as input when running the service.
with open("/tmp/example.txt", "w") as f:
    f.write("ELG is an amazing project.")

result = service("/tmp/example.txt")
print(f"\nResult:\n{result}")

# You can apply a method to the result to extract the information needed. To do so, you have to pass a
# callable object in the ouput_func parameter.
service = Service.from_id(5228)
pretty_result = service("Ich habe diesen Film geliebt. Die Schauspieler, das Drehbuch: alles von einem Meisterwerk.", output_func=lambda x: x.dict()["texts"][0]["content"])
print("Translation to Finnish: ", pretty_result)

# You can also set the output_func parameter to "auto" to extract the information needed automaticly.
# This is not working for all the services.
service = Service.from_id(5228)
pretty_result = service("Ich habe diesen Film geliebt. Die Schauspieler, das Drehbuch: alles von einem Meisterwerk.", output_func="auto")
print("Translation to Finnish: ", pretty_result)
classmethod from_id(id: int, auth_object: Optional[elg.authentication.Authentication] = None, auth_file: Optional[str] = None, scope: Optional[str] = None, domain: Optional[str] = None, use_cache: bool = True, cache_dir: str = '~/.cache/elg')

Class method to init a Service class from its id. You can provide authentication information through the auth_object or the auth_file attributes. If not authentication information is provided, the Authentication object will be initialized.

Parameters
  • id (int) – id of the service.

  • auth_object (elg.Authentication, optional) – elg.Authentication object to use. Defaults to None.

  • auth_file (str, optional) – json file that contains the authentication tokens. Defaults to None.

  • scope (str, optional) – scope to use when requesting tokens. Can be set to “openid” or “offline_access” to get offline tokens. Defaults to “openid”.

  • domain (str, optional) – ELG domain you want to use. “live” to use the public ELG, “dev” to use the development ELG and another value to use a local ELG. Defaults to “live”.

  • use_cache (bool, optional) – True if you want to use cached files. Defaults to True.

  • cache_dir (str, optional) – path to the cache_dir. Set it to None to not store any cached files. Defaults to “~/.cache/elg”.

Returns

Service object with authentication information.

Return type

elg.Service

classmethod from_entity(entity: elg.entity.Entity, auth_object: Optional[str] = None, auth_file: Optional[str] = None, scope: Optional[str] = None, use_cache: bool = True, cache_dir='~/.cache/elg')

Class method to init a Service class from an Entity object. You can provide authentication information through the auth_object or the auth_file attributes. If not authentication information is provided, the Authentication object will be initialized.

Parameters
  • entity (elg.Entity) – Entity object to init as a Service.

  • auth_object (elg.Authentication, optional) – elg.Authentication object to use. Defaults to None.

  • auth_file (str, optional) – json file that contains the authentication tokens. Defaults to None.

  • scope (str, optional) – scope to use when requesting tokens. Can be set to “openid” or “offline_access” to get offline tokens. Defaults to “openid”.

  • domain (str, optional) – ELG domain you want to use. “live” to use the public ELG, “dev” to use the development ELG and another value to use a local ELG. Defaults to “live”.

  • use_cache (bool, optional) – True if you want to use cached files. Defaults to True.

  • cache_dir (str, optional) – path to the cache_dir. Set it to None to not store any cached files. Defaults to “~/.cache/elg”.

Returns

Service object with authentication information.

Return type

elg.Service

classmethod from_docker_image(image: str, execution_location: str, port: int = 8000)

Class method to init a Service class from a Docker image ELG compatible.

Parameters
  • image (str) – name of the Docker image ELG compatible

  • execution_location (str) – url of the ELG compatible endpoint, e.g., http://localhost:8000/process.

  • port (int, optional) – port to use locally. Defaults to 8000.

Returns

Service object with authentication information.

Return type

elg.Service

__call__(request_input: Union[str, List[str], elg.model.base.Request.Request] = None, request_type: str = 'text', sync_mode: bool = False, timeout: int = None, check_file: bool = True, verbose: bool = True, output_func: Union[str, Callable] = <function Service.<lambda>>, **kwargs) Union[dict, str]

Method to call a service. You can enter a string input or the path to the file to process. The output is returned in JSON format.

Parameters
  • request_input (Union[str, List[str], Request]) – can be the text to process directly, the name of the file to process, a list of texts, or directly a Request object.

  • request_type (str, optional) – precise the type of the request. Can be “text”, “structuredText”, “audio”, or “audioStream”. It is only used if request_input is not a Request object. Defaults to “text”.

  • sync_mode (bool, optional) – True to use the sync_mode. Defaults to False.

  • timeout (int, optional) – number of seconds before timeout. Defaults to None.

  • check_file (bool, optional) – True to check if request_input can be a file or not. Defaults to True.

  • verbose (bool, optional) – False to avoid print messages. Defaults to True.

  • output_func (Union[str, Callable], optional) – function applied to the service response. It can be used to extract only the content from the response. If set to ‘auto’, a generic extractive function will be used. Defaults to lambda response: response.

  • kwargs – additional keyword arguments used to hide deprecated arguments

Raises
  • ValueError – if a parameter is not correct.

  • ElgException – can raise a specific Elg exception if the request to the service did not succeed.

Returns

service response in JSON format or as a string if output_func returns a string.

Return type

Union[dict, str]