Merge pull request #33 from tienthienhd/bugfix/fix-ocr-custom

fix: ocr key
This commit is contained in:
Trần Minh Đức 2024-06-04 20:32:36 +07:00 committed by GitHub
commit 7fa2c66f4a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -149,18 +149,20 @@ def custom_get_parser_class_for_mime_type(mime_type: str) -> Optional[type["Docu
if not options: if not options:
return None return None
k = ApplicationConfiguration.objects.filter().first() k = ApplicationConfiguration.objects.filter().first()
best_parser = sorted(options, key=lambda _: _["weight"], reverse=True)[1] best_parser = sorted(options, key=lambda _: _["weight"], reverse=True)[0]
if k.ocr_key!='': if len(best_parser)>1:
headers = { best_parser = sorted(options, key=lambda _: _["weight"], reverse=True)[1]
'Authorization': f'Bearer {k.ocr_key}' if k.ocr_key!='':
} headers = {
url_ocr_pdf_by_fileid = settings.TCGROUP_OCR_CUSTOM["URL"]["URL_OCR_BY_FILEID"] 'Authorization': f'Bearer {k.ocr_key}'
response_ocr = requests.post(url_ocr_pdf_by_fileid, headers=headers) }
logger.debug(f'status code: {response_ocr.status_code}') url_ocr_pdf_by_fileid = settings.TCGROUP_OCR_CUSTOM["URL"]["URL_OCR_BY_FILEID"]
if response_ocr.status_code != 401: response_ocr = requests.post(url_ocr_pdf_by_fileid, params={"file_id":0}, headers=headers)
best_parser = sorted(options, key=lambda _: _["weight"], reverse=True)[0] logger.debug(f'status code: {response_ocr.status_code}')
logger.debug('Successful key authentication ...') if response_ocr.status_code == 404:
logger.debug('Fail key authentication ...', best_parser["parser"]) best_parser = sorted(options, key=lambda _: _["weight"], reverse=True)[0]
logger.debug('Successful key authentication ...')
logger.debug('Fail key authentication ...', best_parser["parser"])
# Return the parser with the highest weight. # Return the parser with the highest weight.
return best_parser["parser"] return best_parser["parser"]