This commit is contained in:
PhanThanhTrung 2024-03-11 15:48:18 +07:00
parent 13eb7ea06c
commit 056052f1a2

View File

@ -8,7 +8,7 @@ from copy import deepcopy
from fwd_api.celery_worker.worker import app
from fwd_api.models import SubscriptionRequest
from fwd_api.exception.exceptions import InvalidException
from fwd_api.models import SubscriptionRequest
from fwd_api.models import SubscriptionRequest, SubscriptionRequestFile
from fwd_api.constant.common import ProcessType
from fwd_api.utils.redis import RedisUtils
from fwd_api.utils import process as ProcessUtil
@ -147,7 +147,6 @@ def process_invoice_sbt_result(rq_id, result, metadata):
rq_id = rq_id.split("_sub_")[0]
rq: SubscriptionRequest = SubscriptionRequest.objects.filter(request_id=rq_id).first()
result["metadata"] = metadata
# status = to_status(result)
status = result.get("status", 200)
@ -171,23 +170,64 @@ def process_invoice_sbt_result(rq_id, result, metadata):
rq.preprocessing_time = result.get("metadata", {}).get("preprocessing_time", 0) # advancing the last result
rq.ai_inference_time = time.time() - rq.ai_inference_start_time
rq.save()
else:
rq.status = 404 # stop waiting
rq.predict_result = result
rq.save()
_update_subscription_rq_file(request_id=rq_id)
update_user(rq)
except IndexError as e:
print(e)
print("NotFound request by requestId, %d", rq_id)
rq.ai_inference_time = 0
rq.save()
_update_subscription_rq_file(request_id=rq_id)
except Exception as e:
print(e)
print("Fail Invoice %d", rq_id)
traceback.print_exc()
rq.ai_inference_time = 0
rq.save()
_update_subscription_rq_file(request_id=rq_id)
return "FailInvoice"
def _update_subscription_rq_file(request_id):
sub_rqs = SubscriptionRequest.objects.filter(request_id=request_id).first()
result = sub_rqs.predict_result
if result is None:
return
files = SubscriptionRequestFile.objects.filter(request=sub_rqs)
for image in files:
retailer_name = None
sold_to_party = None
purchase_date = []
imei_number = []
predicted_res = __get_actual_predict_result(result=result)
if len(predicted_res)!=0:
for elem in predicted_res:
if elem["label"] == "retailername":
retailer_name = elem['value']
elif elem["label"] == "sold_to_party":
sold_to_party = elem['value']
elif elem["label"] == "purchase_date":
purchase_date=elem['value']
else:
imei_number=elem['value']
_predict_result = {
"retailername": retailer_name,
"sold_to_party": sold_to_party,
"purchase_date": purchase_date,
"imei_number": imei_number
}
image.predict_result = _predict_result
image.save()
def __get_actual_predict_result(result: dict):
predicted_res = result.get('content', {}).get('document', [])
if len(predicted_res)==0:
return []
predicted_res = predicted_res[0].get('content', [])
return predicted_res