Skip to content

Commit

Permalink
Fix missing docker image tag (#30)
Browse files Browse the repository at this point in the history
* Fix tag instruction

* Small refactor

* Fix tag

* Fix ci workflow

* Fix CI workflow

* Fix model name in Dockerfile

* Fix style

* Fix remaining model name occurrences

* Fix config name

* Remove save_bert.py

* Fix ci
  • Loading branch information
tschaffter authored Oct 2, 2021
1 parent 2c1ef0a commit 3228faf
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 28 deletions.
1 change: 1 addition & 0 deletions .env.bert-base-ner
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
CONFIG_NAME=bert-base-ner
MODEL_NAME=dslim/bert-base-NER
1 change: 1 addition & 0 deletions .env.bert-base-ner-uncased
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
CONFIG_NAME=bert-base-ner-uncased
MODEL_NAME=dslim/bert-base-NER-uncased
1 change: 1 addition & 0 deletions .env.bert-large-ner
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
CONFIG_NAME=bert-large-ner
MODEL_NAME=dslim/bert-large-NER
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ jobs:
run: |
IMAGE=${{ env.docker_repository_base }}-${{ matrix.config-name }}
TAGS=${{ env.tags }}
NEW_TAGS="$(echo $TAGS | tr "," "\n" ":" | awk -v image="$IMAGE" '{print image ":" $1}' | paste -sd "," -)"
NEW_TAGS="$(echo $TAGS | tr "," "\n" | awk -v image="$IMAGE" '{print image ":" $1}' | paste -sd "," -)"
echo tags=$NEW_TAGS >> $GITHUB_ENV
- name: Set up QEMU
Expand All @@ -158,7 +158,7 @@ jobs:
push: ${{ env.push }}
tags: ${{ env.tags }}
build-args: |
HUGGINGFACE_MODEL=${{ env.MODEL_NAME }}
MODEL_NAME=${{ env.MODEL_NAME }}
labels: |
org.opencontainers.image.created=${{ env.created }}
org.opencontainers.image.source=${{ github.repositoryUrl }}
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ services:
build:
context: server
# args:
# HUGGINGFACE_MODEL: "dslim/bert-base-NER"
# MODEL_NAME: "dslim/bert-base-NER"
container_name: phi-annotator
networks:
- nlpsandbox-internal
Expand Down
6 changes: 3 additions & 3 deletions server/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
FROM python:3.9.6-slim-buster
ARG HUGGINGFACE_MODEL="dslim/bert-base-NER"

ARG MODEL_NAME="dslim/bert-base-NER"

ENV APP_DIR=/opt/app

Expand Down Expand Up @@ -27,14 +28,13 @@ RUN pip install --no-cache-dir \
&& useradd --create-home --shell /bin/bash nlp

# Download model from HuggingFace:
ENV HUGGINGFACE_MODEL=${HUGGINGFACE_MODEL}
ENV MODEL_NAME=${MODEL_NAME}
COPY save_model.py ./
RUN python save_model.py

# Copy source code
COPY openapi_server openapi_server/


WORKDIR /
COPY docker-entrypoint.sh .
RUN chmod +x docker-entrypoint.sh
Expand Down
50 changes: 50 additions & 0 deletions server/openapi_server/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import os
from enum import Enum

defaultValues = {
"CONFIG_NAME": "",
"MODEL_NAME": ""
}


class ConfigName(Enum):
BERT_BASE_NER = "bert-base-ner"
BERT_BASE_NER_UNCASED = "bert-base-ner-uncased"
BERT_LARGE_NER = "bert-large-ner"


class AbstractConfig(object):
"""
Parent class containing get_property to return the environment variable or
default value if not found.
"""
def __init__(self):
self._defaultValues = defaultValues

def get_property(self, property_name):
if os.getenv(property_name) is not None:
return os.getenv(property_name)
# we don't want KeyError?
if property_name not in self._defaultValues.keys():
return None # No default value found
# return default value
return self._defaultValues[property_name]


class Config(AbstractConfig):
"""
This class is used to provide coniguration values to the application, first
using environment variables and if not found, defaulting to those values
provided in the defaultValues dictionary above.
"""

@property
def config_name(self):
return self.get_property('CONFIG_NAME')

@property
def model_name(self):
return self.get_property('MODEL_NAME')


config = Config()
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from openapi_server.models.text_location_annotation import TextLocationAnnotation # noqa: E501
from openapi_server.models.text_location_annotation_request import TextLocationAnnotationRequest # noqa: E501
from openapi_server.models.text_location_annotation_response import TextLocationAnnotationResponse # noqa: E501
from openapi_server.nlp_config import bert
from openapi_server.huggingface import huggingFace


def create_text_location_annotations(): # noqa: E501
Expand All @@ -23,7 +23,7 @@ def create_text_location_annotations(): # noqa: E501
annotation_request = TextLocationAnnotationRequest.from_dict(connexion.request.get_json()) # noqa: E501
note = annotation_request._note
annotations = []
location_annotations = bert.get_entities(note.text, 'LOC')
location_annotations = huggingFace.get_entities(note.text, 'LOC')
add_location_annotation(annotations, location_annotations)
res = TextLocationAnnotationResponse(annotations)
status = 200
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from openapi_server.models.text_person_name_annotation import TextPersonNameAnnotation
from openapi_server.models.text_person_name_annotation_request import TextPersonNameAnnotationRequest # noqa: E501
from openapi_server.models.text_person_name_annotation_response import TextPersonNameAnnotationResponse # noqa: E501
from openapi_server.nlp_config import bert
from openapi_server.huggingface import huggingFace


def create_text_person_name_annotations(): # noqa: E501
Expand All @@ -20,7 +20,7 @@ def create_text_person_name_annotations(): # noqa: E501
annotation_request = TextPersonNameAnnotationRequest.from_dict(connexion.request.get_json()) # noqa: E501
note = annotation_request._note # noqa: E501
annotations = []
name_annotations = bert.get_entities(note.text, "PER")
name_annotations = huggingFace.get_entities(note.text, "PER")
add_name_annotation(annotations, name_annotations)
res = TextPersonNameAnnotationResponse(annotations)
status = 200
Expand Down
13 changes: 6 additions & 7 deletions server/openapi_server/controllers/tool_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from openapi_server.models.tool_dependencies import ToolDependencies # noqa: E501
from openapi_server.models.tool_type import ToolType # noqa: E501
from openapi_server.models.license import License
from openapi_server.nlp_config import bert
from openapi_server.config import config


def get_tool(): # noqa: E501
Expand All @@ -14,15 +14,14 @@ def get_tool(): # noqa: E501
:rtype: Tool
"""
tool = Tool(
name="phi-annotator-example",
version="1.2.1",
name=f"phi-annotator-huggingface-{config.config_name}",
version="1.0.0",
license=License.APACHE_2_0,
repository="github:nlpsandbox/phi-annotator-example",
description="Implementation of the NLP Sandbox PHI Annotator using "
"HuggingFace Model: '%s'" % (bert.get_name()),
repository="github:nlpsandbox/phi-annotator-huggingface",
description="Hugging Face PHI annotator ({config.model_name})",
author="NLP Sandbox Team",
author_email="[email protected]",
url="https://github.com/nlpsandbox/phi-annotator-example",
url="https://github.com/nlpsandbox/phi-annotator-huggingface",
type=ToolType.PHI_ANNOTATOR,
api_version="1.2.0"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def __call__(self, inputs: Union[str, List[str]], **kwargs):
return answers


class Bert:
class HuggingFace:
def __init__(self):
self.tokenizer = at.from_pretrained("hf")
self.model = amc.from_pretrained("hf")
Expand All @@ -148,4 +148,4 @@ def get_name(self):
return self.name


bert = Bert()
huggingFace = HuggingFace()
8 changes: 0 additions & 8 deletions server/save_bert.py

This file was deleted.

2 changes: 1 addition & 1 deletion server/save_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from transformers import AutoModelForTokenClassification as amc, AutoTokenizer as at


MODEL_NAME = os.getenv('HUGGINGFACE_MODEL', 'dslim/bert-base-NER')
MODEL_NAME = os.getenv('MODEL_NAME', 'dslim/bert-base-NER')
LOCAL_DIRECTORY = 'hf'

# Save tokenizer
Expand Down

0 comments on commit 3228faf

Please sign in to comment.