Skip to content

GCP custom entity extractor added (AutoML) #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions auto_labeling_pipeline/mappings.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ class GCPEntitiesTemplate(MappingTemplate):
label_collection = SequenceLabels
template_file = 'gcp_entities.jinja2'

class GCPCustomEntitiesTemplate(MappingTemplate):
label_collection = SequenceLabels
template_file = 'gcp_custom_entities.jinja2'

class AmazonRekognitionLabelDetectionTemplate(MappingTemplate):
label_collection = ClassificationLabels
Expand Down
5 changes: 5 additions & 0 deletions auto_labeling_pipeline/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ def register(cls, task: Type[t.Task], model: Type[mo.RequestModel], template: Ty
mo.GCPEntitiesRequestModel,
mp.GCPEntitiesTemplate
)
Options.register(
t.SequenceLabeling,
mo.GCPCustomEntitiesRequestModel,
mp.GCPCustomEntitiesTemplate
)
Options.register(
t.SequenceLabeling,
mo.AmazonComprehendEntityRequestModel,
Expand Down
26 changes: 26 additions & 0 deletions auto_labeling_pipeline/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,32 @@ def send(self, text: str):
response = requests.post(url, headers=headers, params=params, json=body).json()
return response

class GCPCustomEntitiesRequestModel(RequestModel):
"""
This allow you to analyze entities in a text using an AutoML custom entity extractor
<a href="https://cloud.google.com/natural-language/automl/docs"> AutoML Natural Language API</a>.
"""
url: str
authorization: str

class Config:
title = 'GCP Custom Entity Analysis'

def send(self, text: str):
url = self.url
headers = {'Content-Type': 'application/json',
'Authorization': self.authorization}
params = {}
body = {
"payload": {
"textSnippet": {
"content": text,
"mime_type": "text/plain"
}
}
}
response = requests.post(url, headers=headers, params=params, json=body).json()
return response

class AWSMixin(BaseModel):
aws_access_key: str
Expand Down
14 changes: 14 additions & 0 deletions auto_labeling_pipeline/templates/gcp_custom_entities.jinja2
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{% for entity in input.payload %}
{% set outer_loop = loop %}
{%- set start_offset = entity.textExtraction.textSegment.startOffset -%}
{%- set end_offset = entity.textExtraction.textSegment.endOffset -%}
{% if (start_offset) and (end_offset) %}
{
"label": "{{ entity.displayName }}",
"start_offset": {{ start_offset }},
"end_offset": {{ end_offset }}
}{% if not outer_loop.last %},{% endif %}
{% endif %}
{% endfor %}
]