Skip to content

Teanga service Module

RESTService

Bases: Service

An external service implemented in REST

Source code in teanga/service.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class RESTService(Service):
    """An external service implemented in REST"""

    def __init__(self, endpoint):
        """Creates a new REST service reading from the given endpoint."""
        super().__init__()
        self.endpoint = endpoint
        self._requires = None
        self._produces = None

    def requires(self) -> dict:
        """Returns a dictionary of the layers required by this service."""
        if self._requires is None:
            r = requests.get(self.endpoint + "?requires")
            self._requires = r.json()
        return self._requires

    def produces(self) -> dict:
        """Returns a dictionary of the layers produced by this service."""
        if self._produces is None:
            r = requests.get(self.endpoint + "?produces")
            self._produces = r.json()
        return self._produces

    def execute(self, input:Document):
        """Executes this service on a document."""
        r = requests.post(self.endpoint, json=input.to_json())
        return input.add_layers(r.json())

__init__(endpoint)

Creates a new REST service reading from the given endpoint.

Source code in teanga/service.py
33
34
35
36
37
38
def __init__(self, endpoint):
    """Creates a new REST service reading from the given endpoint."""
    super().__init__()
    self.endpoint = endpoint
    self._requires = None
    self._produces = None

execute(input)

Executes this service on a document.

Source code in teanga/service.py
54
55
56
57
def execute(self, input:Document):
    """Executes this service on a document."""
    r = requests.post(self.endpoint, json=input.to_json())
    return input.add_layers(r.json())

produces()

Returns a dictionary of the layers produced by this service.

Source code in teanga/service.py
47
48
49
50
51
52
def produces(self) -> dict:
    """Returns a dictionary of the layers produced by this service."""
    if self._produces is None:
        r = requests.get(self.endpoint + "?produces")
        self._produces = r.json()
    return self._produces

requires()

Returns a dictionary of the layers required by this service.

Source code in teanga/service.py
40
41
42
43
44
45
def requires(self) -> dict:
    """Returns a dictionary of the layers required by this service."""
    if self._requires is None:
        r = requests.get(self.endpoint + "?requires")
        self._requires = r.json()
    return self._requires

Service

Bases: ABC

A service that can do some work on documents in a Teanga corpus.

Source code in teanga/service.py
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Service(ABC):
    """A service that can do some work on documents in a Teanga corpus."""

    def __init__(self):
        pass

    def setup(self):
        """Sets up this service, including downloading any models."""
        pass

    @abstractmethod
    def requires(self) -> dict:
        """Returns a dictionary of the layers required by this service."""
        pass

    @abstractmethod
    def produces(self) -> dict:
        """Returns a dictionary of the layers produced by this service."""
        pass

    @abstractmethod
    def execute(self, input:Document):
        """Executes this service on a document."""
        pass

execute(input) abstractmethod

Executes this service on a document.

Source code in teanga/service.py
25
26
27
28
@abstractmethod
def execute(self, input:Document):
    """Executes this service on a document."""
    pass

produces() abstractmethod

Returns a dictionary of the layers produced by this service.

Source code in teanga/service.py
20
21
22
23
@abstractmethod
def produces(self) -> dict:
    """Returns a dictionary of the layers produced by this service."""
    pass

requires() abstractmethod

Returns a dictionary of the layers required by this service.

Source code in teanga/service.py
15
16
17
18
@abstractmethod
def requires(self) -> dict:
    """Returns a dictionary of the layers required by this service."""
    pass

setup()

Sets up this service, including downloading any models.

Source code in teanga/service.py
11
12
13
def setup(self):
    """Sets up this service, including downloading any models."""
    pass

rest_service(service, kwargs)

Start a service as a REST service.

Source code in teanga/service.py
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
def rest_service(service, kwargs):
    """Start a service as a REST service."""
    from flask import Flask, request, jsonify
    app = Flask(__name__)

    @app.route("/", methods=["GET"])
    def get():
        if request.args.get("requires"):
            return jsonify(service.requires())
        elif request.args.get("produces"):
            return jsonify(service.produces())
        else:
            return "Hello, World!"

    @app.route("/", methods=["POST"])
    def post():
        doc = Document.from_json(request.json)
        return jsonify(service.execute(doc).to_json())

    app.run(**kwargs)