Skip to content
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

[pull] master from CogStack:master #18

Merged
merged 16 commits into from
Apr 1, 2024
Merged
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: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ ENV CRYPTOGRAPHY_DONT_BUILD_RUST=1
WORKDIR /cat
COPY ./medcat_service/requirements.txt /cat
RUN pip install --upgrade pip

# Install requirements for the app
RUN pip install -r requirements.txt
RUN pip3 install --no-cache-dir -r requirements.txt

# Get the spacy model
ARG SPACY_MODELS="en_core_web_sm en_core_web_md en_core_web_lg"
Expand Down
9 changes: 7 additions & 2 deletions Dockerfile_gpu
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ RUN echo "deb-src http://deb.debian.org/debian bookworm-backports main contrib n

RUN apt-get update && apt-get upgrade -y && \
apt-get install -y software-properties-common

# NVIDIA Docker
RUN export distribution=$(. /etc/os-release;echo $ID$VERSION_ID)

Expand All @@ -26,13 +25,19 @@ RUN apt-get update && apt-get upgrade -y

RUN apt-get install -y nvidia-container-toolkit nvidia-docker2

# clean up apt
RUN apt-get clean autoclean && apt-get autoremove --purge -y

# Set the python path and preapre the base layer
WORKDIR /cat
COPY ./medcat_service/requirements.txt /cat
RUN pip install --upgrade pip

# clean up pip
RUN pip3 cache purge

# Install requirements for the app
RUN pip install -r requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Get the spacy model
ARG SPACY_MODELS="en_core_web_sm en_core_web_md en_core_web_lg"
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ docker run -it -p 5000:5000 \

By default the MedCAT service will be running on port `5000`. MedCAT models will be mounted from local directory `<models-local-dir>` into the container at `/cat/models`.

### GPU support

If you have a gpu and wish to use it, please change the `docker/docker-compose.yml` file, use the `cogstacksystems/medcat-service-gpu:latest` image or change the `build:` directive to build `../Dockerfile_gpu`.

### <span style="color:red">IMPORTANT !</span>
If you wish to run this docker service manually, use the docker/docker-compose.yml file, execute `docker-compose up -d` whilst in the `docker` folder.

Expand Down
4 changes: 2 additions & 2 deletions config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import os


def post_fork(server, worker):
server.log.info("Worker spawned (pid: %s)", worker.pid)
cuda_device_count = int(os.getenv("APP_CUDA_DEVICE_COUNT", -1))
Expand All @@ -14,5 +16,3 @@ def post_fork(server, worker):
os.environ["CUDA_VISIBLE_DEVICES"] = str(cudaid)
else:
worker.log.info("APP_CUDA_DEVICE_COUNT device variables not set")


25 changes: 19 additions & 6 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
version: "3.6"

services:
medcat-service:
container_name: medcat-service
nlp-medcat-service-production:
container_name: cogstack-medcat-service-production
### Multiple images available:
## default image, only CPU support: cogstacksystems/medcat-service:latest
## GPU support: cogstacksystems/medcat-service-gpu:latest
# image: cogstacksystems/medcat-service:latest
platform: linux
build: ..
restart: always
## Default dockerfile: ../Dockerfile
## GPU dockerfile: ../Dockerfile_gpu
build:
context: ../
dockerfile: "Dockerfile"
environment:
- http_proxy=$HTTP_PROXY
- https_proxy=$HTTPS_PROXY
Expand All @@ -14,7 +22,12 @@ services:
- ../envs/env_app
- ../envs/env_medcat
volumes:
- ../models/medmen/:/cat/models/:rw
- ../models:/cat/models/:rw
ports:
- "5000:5000"

- "5555:5000"
networks:
- cognet

networks:
cognet:
name: cogstack-net
8 changes: 6 additions & 2 deletions envs/env_app
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# IMPORTANT : log level set
# CRITICAL - 50, ERROR - 40, WARNING - 30, INFO - 20, DEBUG - 10, NOTSET - 0
APP_LOG_LEVEL=ERROR
APP_LOG_LEVEL=INFO

# can include only one model for NER
APP_NAME=MedCAT
Expand Down Expand Up @@ -31,4 +31,8 @@ SERVER_HOST=0.0.0.0
SERVER_PORT=5000
SERVER_WORKERS=1
SERVER_WORKER_TIMEOUT=300
SERVER_THREADS=16
SERVER_THREADS=16

# GPU SETTING
# CAUTION, use only if you are using the GPU docker image.
APP_TORCH_THREADS=0
3 changes: 3 additions & 0 deletions envs/env_medcat
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ SPACY_MODEL=
# (the NiFi groovy scripts and annotation ingester are two such services that process the output, and so they might require further customisation)
# POSSIBLE VALUES: [list, dict], if left empty then "dict" is the default.
ANNOTATIONS_ENTITY_OUTPUT_MODE=dict

DEID_MODE=False
DEID_REDACT=True
6 changes: 2 additions & 4 deletions medcat_service/app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import os
import sys

import flask_injector
import injector
from flask import Flask
from flask_injector import FlaskInjector

from medcat_service.api import api
from medcat_service.nlp_processor import MedCatProcessor
Expand Down Expand Up @@ -53,9 +53,7 @@ def configure(binder):
binder.bind(MedCatProcessor, to=MedCatProcessor, scope=injector.singleton)
binder.bind(NlpService, to=MedCatService, scope=injector.singleton)

flask_injector.FlaskInjector(
app=app,
modules=[configure])
FlaskInjector(app=app, modules=[configure])

# remember to return the app
return app
98 changes: 73 additions & 25 deletions medcat_service/nlp_processor/medcat_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@

import logging
import os
from datetime import datetime, timezone
import time
from datetime import datetime, timezone

import simplejson as json
from medcat.cat import CAT
from medcat.cdb import CDB
from medcat.config import Config
from medcat.meta_cat import MetaCAT
from medcat.utils.ner.deid import DeIdModel
from medcat.vocab import Vocab


class NlpProcessor:
"""
This class defines an interface for NLP Processor
"""

def __init__(self):
app_log_level = os.getenv("APP_LOG_LEVEL", logging.INFO)
medcat_log_level = os.getenv("LOG_LEVEL", logging.INFO)
Expand Down Expand Up @@ -50,6 +53,7 @@ class MedCatProcessor(NlpProcessor):
MedCAT Processor class is wrapper over MedCAT that implements annotations extractions functionality
(both single and bulk processing) that can be easily exposed for an API.
"""

def __init__(self):
super().__init__()

Expand All @@ -61,13 +65,12 @@ def __init__(self):
self.app_model = os.getenv("APP_MODEL_NAME", "unknown")
self.entity_output_mode = os.getenv("ANNOTATIONS_ENTITY_OUTPUT_MODE", "dict").lower()


self.cat = self._create_cat()
self.cat.train = os.getenv("APP_TRAINING_MODE", False)

self.bulk_nproc = int(os.getenv("APP_BULK_NPROC", 8))

self.torch_threads = int(os.getenv("APP_TORCH_THREADS", -1))
self.DEID_MODE = eval(os.getenv("DEID_MODE", "False"))
self.DEID_REDACT = eval(os.getenv("DEID_REDACT", "True"))
self.model_card_info = {}

# this is available to constrain torch threads when there
# isn't a GPU
# You probably want to set to 1
Expand All @@ -77,7 +80,9 @@ def __init__(self):
torch.set_num_threads(self.torch_threads)
self.log.info("Torch threads set to " + str(self.torch_threads))


self.cat = self._create_cat()
self.cat.train = os.getenv("APP_TRAINING_MODE", False)

self.log.info("MedCAT processor is ready")

def get_app_info(self):
Expand All @@ -88,7 +93,9 @@ def get_app_info(self):
return {"service_app_name": self.app_name,
"service_language": self.app_lang,
"service_version": self.app_version,
"service_model": self.app_model}
"service_model": self.app_model,
"model_card_info": self.model_card_info
}

def process_entities(self, entities):
if type(entities) is dict:
Expand All @@ -109,10 +116,10 @@ def process_content(self, content):
if "text" not in content:
error_msg = "'text' field missing in the payload content."
nlp_result = {
"success": False,
"errors": [error_msg],
"timestamp": NlpProcessor._get_timestamp(),
}
"success": False,
"errors": [error_msg],
"timestamp": NlpProcessor._get_timestamp(),
}

return nlp_result

Expand All @@ -123,22 +130,26 @@ def process_content(self, content):

start_time_ns = time.time_ns()

if text is not None and len(text.strip()) > 0:
entities = self.cat.get_entities(text)
else:
entities = []
if self.DEID_MODE:
entities = self.cat.get_entities(text)["entities"]
text = self.cat.deid_text(text, redact=self.DEID_REDACT)
else:
if text is not None and len(text.strip()) > 0:
entities = self.cat.get_entities(text)
else:
entities = []

elapsed_time = (time.time_ns() - start_time_ns) / 10e8 # nanoseconds to seconds

entities = self.process_entities(entities)

nlp_result = {
"text": str(text),
"annotations": entities,
"success": True,
"timestamp": NlpProcessor._get_timestamp(),
"elapsed_time": elapsed_time
}
"text": str(text),
"annotations": entities,
"success": True,
"timestamp": NlpProcessor._get_timestamp(),
"elapsed_time": elapsed_time
}

# append the footer
if "footer" in content:
Expand Down Expand Up @@ -169,6 +180,7 @@ def process_content_bulk(self, content):
nproc += 1

self.log.debug("NPROC:" + str(nproc))
self.log.debug("Batch size:" + str(batch_size))

# use generators both to provide input documents and to provide resulting annotations
# to avoid too many mem-copies
Expand All @@ -178,8 +190,16 @@ def process_content_bulk(self, content):
start_time_ns = time.time_ns()

try:
ann_res = self.cat.multiprocessing(
MedCatProcessor._generate_input_doc(content, invalid_doc_ids), nproc=nproc)
if self.DEID_MODE:
_text_res = []
for text_record in content:
_text_res.append(self.cat.deid_text(text_record["text"], redact=self.DEID_REDACT))
content = [{"text": txt} for txt in _text_res]
self.log
else:
ann_res = self.cat.multiprocessing_batch_docs_size(
MedCatProcessor._generate_input_doc(content, invalid_doc_ids), batch_size=batch_size, nproc=nproc)

except Exception as e:
self.log.error(repr(e))

Expand Down Expand Up @@ -207,6 +227,13 @@ def retrain_medcat(self, content, replace_cdb):

return {"results": [p, r, f1, tp_dict, fp_dict, fn_dict]}

def _populate_model_card_info(self, config: Config):
self.model_card_info["ontologies"] = config.version.ontology \
if (isinstance(config.version.ontology, list)) else str(config.version.ontology)
self.model_card_info["meta_cat_model_names"] = [i["Category Name"] for i in config.version.meta_cats] \
if (isinstance(config.version.meta_cats, list)) else str(config.version.meta_cats)
self.model_card_info["model_last_modified_on"] = str(config.version.last_modified)

# helper MedCAT methods
#
def _create_cat(self):
Expand All @@ -223,14 +250,24 @@ def _create_cat(self):
cuis_to_keep = [line for line in all_lines if line] # filter blank lines

model_pack_path = os.getenv("APP_MEDCAT_MODEL_PACK", "").strip()

if model_pack_path != "":
self.log.info("Loading model pack...")
cat = CAT.load_model_pack(model_pack_path)

if self.DEID_MODE:
cat = DeIdModel.load_model_pack(model_pack_path)

# Apply CUI filter if provided
if os.getenv("APP_MODEL_CUI_FILTER_PATH", None) is not None:
self.log.debug("Applying CUI filter ...")
cat.cdb.filter_by_cui(cuis_to_keep)

if self.app_model.lower() in ["", "unknown", "medmen"]:
self.app_model = cat.config.version.id

self._populate_model_card_info(cat.config)

return cat
else:
self.log.info("APP_MEDCAT_MODEL_PACK not set, skipping....")
Expand Down Expand Up @@ -282,10 +319,15 @@ def _create_cat(self):
if cat:
meta_models.extend(cat._meta_cats)

if self.app_model.lower() in [None, "unknown"]:
self.app_model = cdb.config.version.id

config.general["log_level"] = os.getenv("LOG_LEVEL", logging.INFO)

cat = CAT(cdb=cdb, config=config, vocab=vocab, meta_cats=meta_models)

self._populate_model_card_info(cat.config)

return cat

# helper generator functions to avoid multiple copies of data
Expand Down Expand Up @@ -320,7 +362,7 @@ def _generate_result(self, in_documents, annotations, invalid_doc_idx, additiona

for i in range(len(in_documents)):
in_ct = in_documents[i]
if i in annotations.keys():
if not self.DEID_MODE and i in annotations.keys():
# generate output for valid annotations

entities = self.process_entities(annotations.get(i))
Expand All @@ -331,6 +373,12 @@ def _generate_result(self, in_documents, annotations, invalid_doc_idx, additiona
"success": True,
"timestamp": NlpProcessor._get_timestamp()}
out_res.update(additional_info)
elif self.DEID_MODE:
out_res = {"text": in_ct["text"],
"annotations": [],
"success": True,
"timestamp": NlpProcessor._get_timestamp()}
out_res.update(additional_info)
else:
# Don't fetch an annotation set
# as the document was invalid
Expand Down
8 changes: 4 additions & 4 deletions medcat_service/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Flask==2.3.2
Flask==3.0.2
gunicorn==20.1.0
injector==0.20.1
flask-injector==0.14.0
medcat==1.7.3
flask-injector==0.15.0
medcat==1.10.2
setuptools==65.5.1
simplejson==3.17.6
werkzeug==2.3.3
werkzeug==3.0.1
setuptools_rust==1.6.0
Loading
Loading