11#!/usr/bin/env python
2- # -*- coding: utf-8 -*-
32
43import logging
54import os
98from flask import Blueprint , Response , request
109
1110from medcat_service .nlp_service import NlpService
11+ from medcat_service .types import HealthCheckResponseContainer , ServiceInfo
1212
1313log = logging .getLogger ("API" )
1414log .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