From a3459fe88625644ece0a4511c5361504d1e19f10 Mon Sep 17 00:00:00 2001 From: Viet Anh Nguyen Date: Thu, 14 Dec 2023 11:21:48 +0700 Subject: [PATCH] Bring back the test UI --- cope2n-api/fwd_api/api/ctel_view.py | 75 ++++++++++++++++++++++++++++- cope2n-fe/src/requests/kie.ts | 2 +- speedtest_sync.py | 8 +-- 3 files changed, 79 insertions(+), 6 deletions(-) diff --git a/cope2n-api/fwd_api/api/ctel_view.py b/cope2n-api/fwd_api/api/ctel_view.py index 6e8526f..c044e4d 100755 --- a/cope2n-api/fwd_api/api/ctel_view.py +++ b/cope2n-api/fwd_api/api/ctel_view.py @@ -15,7 +15,7 @@ from rest_framework_xml.renderers import XMLRenderer from fwd import settings from ..celery_worker.client_connector import c_connector from ..annotation.api import throw_on_failure -from ..constant.common import ProcessType, REQUEST_ID, FOLDER_TYPE, EntityStatus, pdf_extensions, allowed_file_extensions +from ..constant.common import ProcessType, REQUEST_ID, FOLDER_TYPE, EntityStatus, pdf_extensions, allowed_file_extensions, image_extensions, standard_ocr_list from ..exception.exceptions import RequiredFieldException, InvalidException, NotFoundException, \ PermissionDeniedException, LockedEntityException, FileContentInvalidException, ServiceTimeoutException from ..models import SubscriptionRequest, SubscriptionRequestFile, OcrTemplate @@ -25,6 +25,79 @@ from ..utils import FileUtils, ProcessUtil class CtelViewSet(viewsets.ViewSet): lookup_field = "username" size_to_compress = settings.SIZE_TO_COMPRESS + + @extend_schema(request={ + 'multipart/form-data': { + 'type': 'object', + 'properties': { + 'file': { + 'type': 'string', + 'format': 'binary' + }, + 'processType': { + 'type': 'string' + }, + }, + 'required': {'file', 'processType'} + } + }, responses=None, tags=['ocr']) + @action(detail=False, url_path="image/process", methods=["POST"]) + # @transaction.atomic + def process(self, request): + s_time = time.time() + # print(30*"=") + # print(f"[DEBUG]: request: {request}") + # print(30*"=") + user_info = ProcessUtil.get_user(request) + user = user_info.user + sub = user_info.current_sub + + validated_data = ProcessUtil.validate_ocr_request_and_get(request, sub) + + provider_code = 'SAP' + rq_id = provider_code + uuid.uuid4().hex + + file_obj: TemporaryUploadedFile = validated_data['file'] + file_extension = file_obj.name.split(".")[-1].lower() + p_type = validated_data['type'] + file_name = f"temp_{rq_id}.{file_extension}" + + total_page = 1 + + new_request: SubscriptionRequest = SubscriptionRequest(pages=total_page, + pages_left=total_page, + doc_type="all", + process_type=p_type, status=1, request_id=rq_id, + provider_code=provider_code, + subscription=sub) + new_request.save() + from ..celery_worker.client_connector import c_connector + file_obj.seek(0) + file_path = FileUtils.resize_and_save_file(file_name, new_request, file_obj, 100) + S3_path = FileUtils.save_to_S3(file_name, new_request, file_path) + + files: [{ + "file_name": file_name, + "file_path": file_path, # local path to file + "file_type": "" + },] + + if file_extension in pdf_extensions: + c_connector.do_pdf((rq_id, sub.id, p_type, user.id, files)) + # b_url = ProcessUtil.process_pdf_file(file_name, file_obj, new_request, user) + elif file_extension in image_extensions: + b_url = ProcessUtil.process_image_file(file_name, file_obj, new_request, user) + j_time = time.time() + print(f"[INFO]: Duration of Pre-processing: {j_time - s_time}s") + print(f"[INFO]: b_url: {b_url}") + if p_type in standard_ocr_list: + ProcessUtil.send_to_queue2(rq_id + "_sub_0", sub.id, b_url, user.id, p_type) + if p_type == ProcessType.TEMPLATE_MATCHING.value: + ProcessUtil.send_template_queue(rq_id, b_url, validated_data['template'], user.id) + else: + return JsonResponse(status=status.HTTP_406_NOT_ACCEPTABLE, data={"request_id": rq_id, "message": f"File {file_extension} is now allowed"}) + + return JsonResponse(status=status.HTTP_200_OK, data={"request_id": rq_id}) @extend_schema(request={ 'multipart/form-data': { diff --git a/cope2n-fe/src/requests/kie.ts b/cope2n-fe/src/requests/kie.ts index 941ed1b..89d2bd2 100644 --- a/cope2n-fe/src/requests/kie.ts +++ b/cope2n-fe/src/requests/kie.ts @@ -26,7 +26,7 @@ export async function createKieJob({ const response = await API.post<{ estimated_wating_time: number; request_id: string; - }>('/ctel/images/process_sync', formData); + }>('/ctel/image/process/', formData); return response.data.request_id; } diff --git a/speedtest_sync.py b/speedtest_sync.py index 7c7bc7d..9c90371 100644 --- a/speedtest_sync.py +++ b/speedtest_sync.py @@ -94,12 +94,12 @@ def process_file(data): -# invoice_files = [ -# ('invoice_file', ('invoice.pdf', open("test_samples/20220303025923NHNE_20220222_Starhub_Order_Confirmation_by_Email.pdf", "rb").read())), -# ] invoice_files = [ - ('invoice_file', ('invoice.jpg', open("test_samples/sbt/invoice.jpg", "rb").read())), + ('invoice_file', ('invoice.pdf', open("test_samples/20220303025923NHNE_20220222_Starhub_Order_Confirmation_by_Email.pdf", "rb").read())), ] +# invoice_files = [ +# ('invoice_file', ('invoice.jpg', open("test_samples/sbt/invoice.jpg", "rb").read())), +# ] imei_files = [ ('imei_files', ("test_samples/sbt/imei1.jpg", open("test_samples/sbt/invoice.jpg", "rb").read())), ('imei_files', ("test_samples/sbt/imei2.jpg", open("test_samples/sbt/imei2.jpg", "rb").read())),