Skip to content

Commit 2186268

Browse files
refactor(medcat-service): Add pydantic. Add types to Health and Info APIs (#51)
* refactor(medcat-service): Add pydantic. Add types to Health and Info APIs * refactor(medcat-service): Fix types to not be global. Add port to test functions to make easier to use
1 parent 2e7b390 commit 2186268

File tree

8 files changed

+342
-74
lines changed

8 files changed

+342
-74
lines changed

.devcontainer/devcontainer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
"workspaceMount": "source=${localWorkspaceFolder},target=${localWorkspaceFolder},type=bind",
3030
"mounts": [
3131
"source=${localEnv:HOME}/.cache/pip,target=/home/vscode/.cache/pip,type=bind",
32-
"source=${localEnv:HOME}/.cache/uv,target=/home/vscode/.cache/uv,type=bind"
32+
"source=${localEnv:HOME}/.cache/uv,target=/home/vscode/.cache/uv,type=bind",
33+
"source=${localEnv:HOME}/.cache/huggingface,target=/home/vscode/.cache/huggingface,type=bind",
3334
],
3435
"customizations": {
3536
"vscode": {

.gitignore

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
2+
# Python gitignore https://github.com/github/gitignore/blob/main/Python.gitignore
3+
# Byte-compiled / optimized / DLL files
4+
__pycache__/
5+
*.py[codz]
6+
*$py.class
7+
8+
# C extensions
9+
*.so
10+
11+
# Distribution / packaging
12+
.Python
13+
build/
14+
develop-eggs/
15+
dist/
16+
downloads/
17+
eggs/
18+
.eggs/
19+
lib/
20+
lib64/
21+
parts/
22+
sdist/
23+
var/
24+
wheels/
25+
share/python-wheels/
26+
*.egg-info/
27+
.installed.cfg
28+
*.egg
29+
MANIFEST
30+
31+
# PyInstaller
32+
# Usually these files are written by a python script from a template
33+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
34+
*.manifest
35+
*.spec
36+
37+
# Installer logs
38+
pip-log.txt
39+
pip-delete-this-directory.txt
40+
41+
# Unit test / coverage reports
42+
htmlcov/
43+
.tox/
44+
.nox/
45+
.coverage
46+
.coverage.*
47+
.cache
48+
nosetests.xml
49+
coverage.xml
50+
*.cover
51+
*.py.cover
52+
.hypothesis/
53+
.pytest_cache/
54+
cover/
55+
56+
# Translations
57+
*.mo
58+
*.pot
59+
60+
# Django stuff:
61+
*.log
62+
local_settings.py
63+
db.sqlite3
64+
db.sqlite3-journal
65+
66+
# Flask stuff:
67+
instance/
68+
.webassets-cache
69+
70+
# Scrapy stuff:
71+
.scrapy
72+
73+
# Sphinx documentation
74+
docs/_build/
75+
76+
# PyBuilder
77+
.pybuilder/
78+
target/
79+
80+
# Jupyter Notebook
81+
.ipynb_checkpoints
82+
83+
# IPython
84+
profile_default/
85+
ipython_config.py
86+
87+
# pyenv
88+
# For a library or package, you might want to ignore these files since the code is
89+
# intended to run in multiple environments; otherwise, check them in:
90+
# .python-version
91+
92+
# pipenv
93+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
94+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
95+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
96+
# install all needed dependencies.
97+
#Pipfile.lock
98+
99+
# UV
100+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
101+
# This is especially recommended for binary packages to ensure reproducibility, and is more
102+
# commonly ignored for libraries.
103+
#uv.lock
104+
105+
# poetry
106+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
107+
# This is especially recommended for binary packages to ensure reproducibility, and is more
108+
# commonly ignored for libraries.
109+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
110+
#poetry.lock
111+
#poetry.toml
112+
113+
# pdm
114+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
115+
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
116+
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
117+
#pdm.lock
118+
#pdm.toml
119+
.pdm-python
120+
.pdm-build/
121+
122+
# pixi
123+
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
124+
#pixi.lock
125+
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
126+
# in the .venv directory. It is recommended not to include this directory in version control.
127+
.pixi
128+
129+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
130+
__pypackages__/
131+
132+
# Celery stuff
133+
celerybeat-schedule
134+
celerybeat.pid
135+
136+
# SageMath parsed files
137+
*.sage.py
138+
139+
# Environments
140+
.env
141+
.envrc
142+
.venv
143+
env/
144+
venv/
145+
ENV/
146+
env.bak/
147+
venv.bak/
148+
149+
# Spyder project settings
150+
.spyderproject
151+
.spyproject
152+
153+
# Rope project settings
154+
.ropeproject
155+
156+
# mkdocs documentation
157+
/site
158+
159+
# mypy
160+
.mypy_cache/
161+
.dmypy.json
162+
dmypy.json
163+
164+
# Pyre type checker
165+
.pyre/
166+
167+
# pytype static type analyzer
168+
.pytype/
169+
170+
# Cython debug symbols
171+
cython_debug/
172+
173+
# PyCharm
174+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
175+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
176+
# and can be added to the global gitignore or merged into this file. For a more nuclear
177+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
178+
#.idea/
179+
180+
# Abstra
181+
# Abstra is an AI-powered process automation framework.
182+
# Ignore directories containing user credentials, local state, and settings.
183+
# Learn more at https://abstra.io/docs
184+
.abstra/
185+
186+
# Visual Studio Code
187+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
188+
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
189+
# and can be added to the global gitignore or merged into this file. However, if you prefer,
190+
# you could uncomment the following to ignore the entire vscode folder
191+
# .vscode/
192+
193+
# Ruff stuff:
194+
.ruff_cache/
195+
196+
# PyPI configuration file
197+
.pypirc
198+
199+
# Cursor
200+
# Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to
201+
# exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
202+
# refer to https://docs.cursor.com/context/ignore-files
203+
.cursorignore
204+
.cursorindexingignore
205+
206+
# Marimo
207+
marimo/_static/
208+
marimo/_lsp/
209+
__marimo__/
210+
211+
# Streamlit
212+
.streamlit/secrets.toml

medcat-service/medcat_service/api/api.py

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/usr/bin/env python
2-
# -*- coding: utf-8 -*-
32

43
import logging
54
import os
@@ -9,6 +8,7 @@
98
from flask import Blueprint, Response, request
109

1110
from medcat_service.nlp_service import NlpService
11+
from medcat_service.types import HealthCheckResponseContainer, ServiceInfo
1212

1313
log = logging.getLogger("API")
1414
log.setLevel(level=os.getenv("APP_LOG_LEVEL", logging.INFO))
@@ -29,8 +29,8 @@ def info(nlp_service: NlpService) -> Response:
2929
:param nlp_service: NLP Service provided by dependency injection
3030
:return: Flask Response
3131
"""
32-
app_info = nlp_service.nlp.get_app_info()
33-
return Response(response=json.dumps(app_info), status=200, mimetype="application/json")
32+
app_info: ServiceInfo = nlp_service.nlp.get_app_info()
33+
return Response(response=app_info.model_dump_json(), status=200, mimetype="application/json")
3434

3535

3636
@api.route('/process', methods=['POST'])
@@ -48,10 +48,12 @@ def process(nlp_service: NlpService) -> Response:
4848
meta_anns_filters = payload.get('meta_anns_filters', None)
4949

5050
try:
51-
result = nlp_service.nlp.process_content(payload['content'], meta_anns_filters=meta_anns_filters)
52-
app_info = nlp_service.nlp.get_app_info()
53-
response = {'result': result, 'medcat_info': app_info}
54-
return Response(response=json.dumps(response, iterable_as_array=True), status=200, mimetype="application/json")
51+
result = nlp_service.nlp.process_content(
52+
payload['content'], meta_anns_filters=meta_anns_filters)
53+
app_info: ServiceInfo = nlp_service.nlp.get_app_info()
54+
response = {'result': result, 'medcat_info': app_info.model_dump()}
55+
return Response(response=json.dumps(response, iterable_as_array=True, default=str),
56+
status=200, mimetype="application/json")
5557

5658
except Exception as e:
5759
log.error(traceback.format_exc())
@@ -71,10 +73,12 @@ def process_bulk(nlp_service: NlpService) -> Response:
7173

7274
try:
7375
result = nlp_service.nlp.process_content_bulk(payload['content'])
74-
app_info = nlp_service.nlp.get_app_info()
76+
app_info: ServiceInfo = nlp_service.nlp.get_app_info()
7577

76-
response = {'result': result, 'medcat_info': app_info}
77-
return Response(response=json.dumps(response, iterable_as_array=True), status=200, mimetype="application/json")
78+
response = {'result': result,
79+
'medcat_info': app_info.model_dump()}
80+
return Response(response=json.dumps(response, iterable_as_array=True, default=str),
81+
status=200, mimetype="application/json")
7882

7983
except Exception as e:
8084
log.error(traceback.format_exc())
@@ -91,7 +95,8 @@ def retrain_medcat(nlp_service: NlpService) -> Response:
9195
try:
9296
result = nlp_service.nlp.retrain_medcat(payload['content'], payload['replace_cdb'])
9397
app_info = nlp_service.nlp.get_app_info()
94-
response = {'result': result, 'annotations': payload['content'], 'medcat_info': app_info}
98+
response = {'result': result,
99+
'annotations': payload['content'], 'medcat_info': app_info}
95100
return Response(response=json.dumps(response), status=200, mimetype="application/json")
96101

97102
except Exception as e:
@@ -104,8 +109,8 @@ def liveness():
104109
"""
105110
Liveness API checks if the application is running.
106111
"""
107-
response = {"status": "UP", "checks": []}
108-
return Response(response=json.dumps(response), status=200)
112+
response = HealthCheckResponseContainer(status="UP", checks=[])
113+
return Response(response=response.model_dump_json(), status=200)
109114

110115

111116
@api.route('/health/ready')
@@ -115,9 +120,11 @@ def readiness(nlp_service: NlpService) -> Response:
115120
"""
116121
medcat_is_ready = nlp_service.get_processor().is_ready()
117122

118-
if medcat_is_ready["status"] == "UP":
119-
response = {"status": "UP", "checks": [medcat_is_ready]}
120-
return Response(response=json.dumps(response), status=200)
123+
if medcat_is_ready.status == "UP":
124+
response = HealthCheckResponseContainer(
125+
status="UP", checks=[medcat_is_ready])
126+
return Response(response=response.model_dump_json(), status=200)
121127
else:
122-
response = {"status": "DOWN", "checks": [medcat_is_ready]}
123-
return Response(response=json.dumps(response), status=503)
128+
response = HealthCheckResponseContainer(
129+
status="DOWN", checks=[medcat_is_ready])
130+
return Response(response=response.model_dump_json(), status=503)

0 commit comments

Comments
 (0)