Merge remote-tracking branch 'origin/main'
This commit is contained in:
@@ -12,11 +12,11 @@ from swagger_server.util import get_random_filename, create_random_file_in_tmp_f
|
|||||||
import requests
|
import requests
|
||||||
from werkzeug.utils import secure_filename
|
from werkzeug.utils import secure_filename
|
||||||
|
|
||||||
# ATEapi_endpoint = "http://localhost:5000/predict"
|
|
||||||
|
|
||||||
|
|
||||||
ATEapi_endpoint = "http://ate-api:5000/predict"
|
ATEapi_endpoint = "http://ate-api:5000/predict"
|
||||||
|
|
||||||
|
# endpoint below to be used only for development purposes (don't need to run docker)
|
||||||
|
# ATEapi_endpoint = "http://localhost:5000/predict"
|
||||||
|
|
||||||
|
|
||||||
def do_izlusci(conllus, prepovedane_besede):
|
def do_izlusci(conllus, prepovedane_besede):
|
||||||
tmp_file_path = ""
|
tmp_file_path = ""
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import datetime
|
import datetime
|
||||||
import json
|
import json
|
||||||
import os.path
|
import os.path
|
||||||
|
import traceback
|
||||||
|
|
||||||
import peewee
|
import peewee
|
||||||
import asyncio
|
import asyncio
|
||||||
@@ -92,6 +93,7 @@ def try_do_jobs_ateapi():
|
|||||||
[ex.submit(execute_ateapi_job, job) for job in unfinished_jobs]
|
[ex.submit(execute_ateapi_job, job) for job in unfinished_jobs]
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Exception in try_do_jobs_ateapi")
|
print(f"Exception in try_do_jobs_ateapi")
|
||||||
|
traceback.print_exc()
|
||||||
finally:
|
finally:
|
||||||
time.sleep(3)
|
time.sleep(3)
|
||||||
|
|
||||||
@@ -120,6 +122,7 @@ def try_do_jobs_classla():
|
|||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Exception in try_do_jobs_classla")
|
print(f"Exception in try_do_jobs_classla")
|
||||||
|
traceback.print_exc()
|
||||||
finally:
|
finally:
|
||||||
time.sleep(3)
|
time.sleep(3)
|
||||||
|
|
||||||
@@ -136,6 +139,7 @@ def try_do_jobs_doc2text():
|
|||||||
[ex.submit(execute_doc2text_job, job) for job in unfinished_jobs]
|
[ex.submit(execute_doc2text_job, job) for job in unfinished_jobs]
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Exception in try_do_jobs_doc2text")
|
print(f"Exception in try_do_jobs_doc2text")
|
||||||
|
traceback.print_exc()
|
||||||
finally:
|
finally:
|
||||||
time.sleep(3)
|
time.sleep(3)
|
||||||
|
|
||||||
@@ -163,9 +167,9 @@ def execute_doc2text_job(job: Job):
|
|||||||
jtype = job.job_type
|
jtype = job.job_type
|
||||||
text = ""
|
text = ""
|
||||||
if jtype in [1, 12]:
|
if jtype in [1, 12]:
|
||||||
text = txt_utils.extract_text_prepResp(file)
|
text, _ = txt_utils.extract_text_prepResp(file)
|
||||||
elif jtype in [3, 32]:
|
elif jtype in [3, 32]:
|
||||||
text = txt_utils.ocr_text_prepResp(file)
|
text, _ = txt_utils.ocr_text_prepResp(file)
|
||||||
|
|
||||||
if jtype in [1, 3]:
|
if jtype in [1, 3]:
|
||||||
job.job_output = text
|
job.job_output = text
|
||||||
|
|||||||
@@ -12,18 +12,24 @@ import magic
|
|||||||
|
|
||||||
tika_server = "http://tika2:9999/tika"
|
tika_server = "http://tika2:9999/tika"
|
||||||
|
|
||||||
|
|
||||||
# endpoint below to be used only for development purposes (don't need to run docker)
|
# endpoint below to be used only for development purposes (don't need to run docker)
|
||||||
# tika_server = "http://rsdo.lhrs.feri.um.si:9998/tika"
|
# tika_server = "http://rsdo.lhrs.feri.um.si:9998/tika"
|
||||||
|
|
||||||
|
|
||||||
def extract_text_prepResp(file, content_type=""):
|
def extract_text_prepResp(file, content_type=""):
|
||||||
content_type = file.content_type
|
content_type = file.content_type
|
||||||
if content_type is None:
|
if content_type is None:
|
||||||
content_type = magic.from_file(file.stream.name, mime=True)
|
content_type = magic.from_file(file.stream.name, mime=True)
|
||||||
|
|
||||||
|
content = ""
|
||||||
if tika_responding():
|
if tika_responding():
|
||||||
|
try:
|
||||||
response = requests.put(tika_server, data=file, headers={"Accept": "text/plain; charset=UTF-8"})
|
response = requests.put(tika_server, data=file, headers={"Accept": "text/plain; charset=UTF-8"})
|
||||||
return response.text, 200
|
content = response.text
|
||||||
|
except:
|
||||||
|
content = "ERROR - something went wrong when reading file with tika"
|
||||||
|
|
||||||
|
if content == "":
|
||||||
if "openxmlformats-officedocument.wordprocessingml.document" in content_type:
|
if "openxmlformats-officedocument.wordprocessingml.document" in content_type:
|
||||||
content = '\n'.join([p.text for p in docx.Document(file).paragraphs])
|
content = '\n'.join([p.text for p in docx.Document(file).paragraphs])
|
||||||
elif "application/pdf" in content_type:
|
elif "application/pdf" in content_type:
|
||||||
@@ -38,17 +44,27 @@ def extract_text_prepResp(file, content_type=""):
|
|||||||
content = '\n'.join([pt.text for pt in plainText])
|
content = '\n'.join([pt.text for pt in plainText])
|
||||||
# elif "text/plain" in file.content_type:
|
# elif "text/plain" in file.content_type:
|
||||||
else:
|
else:
|
||||||
|
try:
|
||||||
content = file.read().decode('utf-8')
|
content = file.read().decode('utf-8')
|
||||||
|
except:
|
||||||
|
content = "ERROR - something went wrong when reading file with not-tika method!"
|
||||||
|
|
||||||
return content, 200
|
return content, 200
|
||||||
|
|
||||||
|
|
||||||
def ocr_text_prepResp(file):
|
def ocr_text_prepResp(file):
|
||||||
|
content = ""
|
||||||
if tika_responding():
|
if tika_responding():
|
||||||
|
try:
|
||||||
response = requests.put(tika_server, data=file,
|
response = requests.put(tika_server, data=file,
|
||||||
headers={"X-Tika-PDFOcrStrategy": "ocr_only", "X-Tika-OCRLanguage": "slv+eng","Accept": "text/plain; charset=UTF-8"})
|
headers={"X-Tika-PDFOcrStrategy": "ocr_only", "X-Tika-OCRLanguage": "slv+eng",
|
||||||
return response.text, 200
|
"Accept": "text/plain; charset=UTF-8"})
|
||||||
|
content = response.text
|
||||||
|
except:
|
||||||
|
content = "ERROR - something went wrong when reading file with tika (OCR)"
|
||||||
|
|
||||||
|
if content == "":
|
||||||
|
try:
|
||||||
win_p = "C:/Program Files/Tesseract-OCR/tesseract.exe"
|
win_p = "C:/Program Files/Tesseract-OCR/tesseract.exe"
|
||||||
if os.path.exists(win_p):
|
if os.path.exists(win_p):
|
||||||
pytesseract.pytesseract.tesseract_cmd = win_p
|
pytesseract.pytesseract.tesseract_cmd = win_p
|
||||||
@@ -59,7 +75,11 @@ def ocr_text_prepResp(file):
|
|||||||
img = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
|
img = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
|
||||||
|
|
||||||
conf = '-l eng+slv'
|
conf = '-l eng+slv'
|
||||||
return pytesseract.image_to_string(img, config=conf), 200
|
content = pytesseract.image_to_string(img, config=conf)
|
||||||
|
except:
|
||||||
|
content = "ERROR - something went wrong when reading file with not-tika method! (OCR)"
|
||||||
|
|
||||||
|
return content, 200
|
||||||
|
|
||||||
|
|
||||||
def tika_responding():
|
def tika_responding():
|
||||||
|
|||||||
Reference in New Issue
Block a user