Skip to content

Commit b572d81

Browse files
committed
Api improvements.
1 parent 608e225 commit b572d81

File tree

4 files changed

+45
-25
lines changed

4 files changed

+45
-25
lines changed

config.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
# basic app settings
2222
OCR_SERVICE_PORT = os.environ.get("OCR_SERVICE_PORT", 8090)
2323

24-
# Tesseract model path
25-
TESSDATA_PREFIX = os.environ.get("OCR_TESSDATA_PREFIX", "/usr/local/share/tessdata")
24+
# Tesseract model path: macos - /opt/homebrew/share/tessdata | linux - "/usr/local/share/tessdata"
25+
TESSDATA_PREFIX = os.environ.get("OCR_TESSDATA_PREFIX", "/opt/homebrew/share/tessdata")
2626

2727
# Integer or Float - duration in seconds for the OCR processing, after which,
2828
# tesseract will terminate and raise RuntimeError
@@ -68,6 +68,9 @@
6868
# dpi used for images in TESSERACT and other stuff
6969
OCR_IMAGE_DPI = int(os.environ.get("OCR_SERVICE_IMAGE_DPI", 200))
7070

71+
# possible values: json (stringified output), dict (dict means no json.dumps() is applied to the output)
72+
OCR_SERVICE_RESPONSE_OUTPUT_TYPE: str = str(os.environ.get("OCR_SERVICE_RESPONSE_OUTPUT_TYPE", "json"))
73+
7174
# LIBRE OFFICE SECTION
7275

7376
# 60 seconds before terminating processes

env/ocr_service.env

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ OCR_WEB_SERVICE_WORKER_CLASS="sync"
1616
OCR_SERVICE_OPERATION_MODE=OCR
1717

1818
# 50 - CRITICAL, 40 - ERROR, 30 - WARNING, 20 - INFO, 10 - DEBUG, 0 - NOTSET
19-
OCR_SERVICE_LOG_LEVEL=40
19+
OCR_SERVICE_LOG_LEVEL=10
20+
21+
# possible values: json (stringified output), dict (dict means no json.dumps() is applied to the output)
22+
OCR_SERVICE_RESPONSE_OUTPUT_TYPE="dict"
2023

2124
######################################################################################################
2225

@@ -27,7 +30,7 @@ OCR_SERVICE_TESSERACT_TIMEOUT=30
2730
# change this to whatever language you are trying to OCR, e.g. eng, deu, fra, ita, nld, ron, spa
2831
# please note that you need to have the corresponding language pack installed in the container
2932
# check Dockerfile_multilang for more information and look for tessaract-ocr-[lang] packages
30-
OCR_SERVICE_TESSERACT_LANG="ron"
33+
OCR_SERVICE_TESSERACT_LANG="eng"
3134

3235
OCR_SERVICE_TESSERACT_NICE=-18
3336
OCR_SERVICE_TESSERACT_CUSTOM_CONFIG_FLAGS=""

env/ocr_service_text_only.env

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,7 @@ OCR_SERVICE_OPERATION_MODE=NO_OCR
1919
# change this to whatever language you are trying to OCR, e.g. eng, deu, fra, ita, nld, ron, spa
2020
# please note that you need to have the corresponding language pack installed in the container
2121
# check Dockerfile_multilang for more information and look for tessaract-ocr-[lang] packages
22-
OCR_SERVICE_TESSERACT_LANG="eng"
22+
OCR_SERVICE_TESSERACT_LANG="eng"
23+
24+
# possible values: json (stringified output), dict (dict means no json.dumps() is applied to the output)
25+
OCR_SERVICE_RESPONSE_OUTPUT_TYPE="json"

ocr_service/api/api.py

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from 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
1313
from ocr_service.api.api_blueprint import ApiBlueprint
1414
from 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"])
93102
def 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

Comments
 (0)