99
1010from multiprocessing import Pool
1111
12- from config import CPU_THREADS , TESSERACT_TIMEOUT , LOG_LEVEL
12+ from config import CPU_THREADS , TESSERACT_TIMEOUT , LOG_LEVEL , OCR_SERVICE_RESPONSE_OUTPUT_TYPE
1313from ocr_service .api .api_blueprint import ApiBlueprint
1414from ocr_service .utils .utils import build_response , get_app_info , setup_logging
1515
@@ -36,7 +36,7 @@ def process() -> Response:
3636 Response: json with the result of the OCR processing
3737 """
3838 stream = None
39- file_name : str = ""
39+ file_name : str | None = ""
4040
4141 footer = {}
4242
@@ -48,7 +48,7 @@ def process() -> Response:
4848 stream = file .stream .read ()
4949 file_name = file .filename
5050 del file
51- log .info ("Processing file given via 'file' parameter, file name: " + file_name )
51+ log .info ("Processing file given via 'file' parameter, file name: " + str ( file_name ) )
5252 else :
5353 # if it is sent as a data-binary
5454 log .info ("Processing binary as data-binary, generating temporary file name..." )
@@ -70,37 +70,48 @@ def process() -> Response:
7070
7171 log .info ("Stream contains valid JSON." )
7272 except json .JSONDecodeError :
73- log .warning ("Stream does not contain valid JSON." )
73+ log .error ("Stream does not contain valid JSON." )
7474
75- output_text , doc_metadata = api .processor .process_stream (stream = stream , file_name = file_name )
75+ output_text , doc_metadata = api .processor .process_stream (stream = stream , file_name = file_name ) # type: ignore
7676
77+ success = False
78+ code = 200
79+ log_message = ""
7780 if len (output_text ) > 0 :
78- response = build_response (output_text , footer = footer , metadata = doc_metadata )
79- return Response (response = json .dumps ({"result" : response }),
80- status = 200 ,
81- mimetype = "application/json" )
81+ success = True
8282 else :
83- response = build_response (output_text ,
84- metadata = doc_metadata ,
85- success = False ,
86- log_message = "No text has been generated" )
87-
88- return Response (response = json .dumps ({"result" : response }),
89- status = 500 ,
90- mimetype = "application/json" )
83+ code = 500
84+ log_message = "No text has been generated"
85+
86+ response = build_response (output_text ,
87+ footer = footer ,
88+ metadata = doc_metadata ,
89+ success = success ,
90+ log_message = log_message )
91+
92+ if OCR_SERVICE_RESPONSE_OUTPUT_TYPE == "json" :
93+ response = json .dumps ({"result" : response })
94+ elif OCR_SERVICE_RESPONSE_OUTPUT_TYPE == "dict" :
95+ response = {"result" : response }
96+
97+ return Response (response = response ,
98+ status = code ,
99+ mimetype = "application/json" )
91100
92101@api .route ("/process_file" , methods = ["POST" ])
93102def process_file () -> Response :
94103 stream = None
95- file_name : str = ""
104+ file_name : str | None = ""
105+
106+ global log
96107
97108 # if it is sent via the file parameter (file keeps its original name)
98109 if len (request .files ):
99110 file = list (request .files .values ())[0 ]
100111 stream = file .stream .read ()
101112 file_name = file .filename
102113 del file
103- log .info ("Processing file given via 'file' parameter, file name: " + file_name )
114+ log .info ("Processing file given via 'file' parameter, file name: " + str ( file_name ) )
104115 else :
105116 # if it is sent as a data-binary
106117 log .info ("Processing binary as data-binary, generating temporary file name..." )
@@ -109,7 +120,7 @@ def process_file() -> Response:
109120
110121 stream = request .get_data (cache = False , as_text = False , parse_form_data = False )
111122
112- output_text , doc_metadata = api .processor .process_stream (stream = stream , file_name = file_name )
123+ output_text , doc_metadata = api .processor .process_stream (stream = stream , file_name = file_name ) # type: ignore
113124
114125 if len (output_text ) > 0 :
115126 response = build_response (output_text , metadata = doc_metadata )
0 commit comments