-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
1456 lines (1200 loc) · 50.6 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
#from torch import margin_ranking_loss, tensor
if os.path.exists( '/home/build'):
os.environ['HF_HOME'] = '/home/build'
else:
os.environ['HF_HOME'] = './build'
import uvicorn
from typing import Union
import uuid
import base64
import json
import re
from fastapi import Depends, FastAPI, HTTPException, BackgroundTasks, status
from fastapi.encoders import jsonable_encoder
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
from fastapi.responses import FileResponse
from fastapi.openapi.docs import get_swagger_ui_html
import jwt
from passlib.context import CryptContext
import asyncio
from typing import List
from enum import Enum
from pydantic import BaseModel
from datetime import datetime, timezone, timedelta
import locale
from classification import Classification
pydocuClassfication: Classification = Classification()
pydocuClassfication.load_Models()
from invoice2data_txt import main as invoice2data_txt_main
#Set time zone of the server
LOCAL_TIMEZONE = datetime.now(timezone.utc).astimezone().tzinfo
APT_GET_PATH = "/usr/bin/apt-get"
if os.path.exists( '/home/pydoc'):
LOCAL_TEMPDIR = "/home/pydoc"
else:
LOCAL_TEMPDIR = "./pydoc"
# to get a string like this run:
# openssl rand -hex 32
SECRET_KEY = "b9934564b9761d0734536bf821b67cc3883a58268aa80344e526551a62afd7e8"
ALGORITHM = "HS256"
ACCESS_TOKEN_EXPIRE_MINUTES = 30
fake_users_db = {
"admin": {
"username": "admin",
"full_name": "admin",
"email": "admin.pydoc@gmail.com",
"roles": ["admin"],
"tenants": ["1000"],
"hashed_password":"$2b$12$IrZX5u1uPOGXzWai55Y3wuj7Y98Ke7IblXVCAPwFGsPUzvqautCU2", #: test
"disabled": False,
},
}
app = FastAPI(
title="pyDocu",
description="API for PDF documents with classification and data extraction. "+
"The API call supports different tenants. Master data for the sender, "+
"recipient and objects or cost centers can be stored for each tenant.",
version="0.1.0")
""" Application for main data and function
"""
class Token(BaseModel):
access_token: str
token_type: str
class TokenData(BaseModel):
username: Union[str, None] = None
class User(BaseModel):
username: str
email: Union[str, None] = None
full_name: Union[str, None] = None
tenants: list[str] = []
roles: list[str] = []
disabled: Union[bool, None] = None
class UserInDB(User):
hashed_password: str
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
def verify_password(plain_password, hashed_password):
return pwd_context.verify(plain_password, hashed_password)
def get_password_hash(password):
return pwd_context.hash(password)
def get_user(db, username: str):
if username in db:
user_dict = db[username]
return UserInDB(**user_dict)
def authenticate_user(fake_db, username: str, password: str):
user = get_user(fake_db, username)
if not user:
return False
if not verify_password(password, user.hashed_password):
return False
return user
def create_access_token(data: dict, expires_delta: Union[timedelta, None] = None):
to_encode = data.copy()
if expires_delta:
#expire = datetime.utcnow() + expires_delta
expire = datetime.now(timezone.utc) + expires_delta
else:
#expire = datetime.utcnow() + timedelta(minutes=15)
expire = datetime.now(timezone.utc) + timedelta(minutes=15)
to_encode.update({"exp": expire})
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
return encoded_jwt
async def get_current_user(token: str = Depends(oauth2_scheme)):
credentials_exception = HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Could not validate credentials",
headers={"WWW-Authenticate": "Bearer"},
)
try:
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
username: str = payload.get("sub")
if username is None:
raise credentials_exception
token_data = TokenData(username=username)
except jwt.ExpiredSignatureError:
raise credentials_exception
user = get_user(fake_users_db, username=token_data.username)
if user is None:
raise credentials_exception
return user
async def get_current_active_user(current_user: User = Depends(get_current_user)):
if current_user.disabled:
raise HTTPException(status_code=400, detail="Inactive user")
return current_user
@app.post("/token", response_model=Token)
async def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends()):
"""get assecc token by login """
user = authenticate_user(fake_users_db, form_data.username, form_data.password)
if not user:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Incorrect username or password",
headers={"WWW-Authenticate": "Bearer"},
)
access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
access_token = create_access_token(
data={"sub": user.username}, expires_delta=access_token_expires
)
return {"access_token": access_token, "token_type": "bearer"}
@app.get("/users/me/", response_model=User)
async def read_users_me(current_user: User = Depends(get_current_active_user)):
"""get user data """
return current_user
class PhraseEnum(str, Enum):
gs = "gs"
tesseract = "tesseract"
models = "models"
class Application:
def __init__(self):
self.gs_path: str = ""
self.tr_path: str = ""
self.temp_dir: str = ""
self.background_task: int = 0
self.protocol: list[str] = []
def check_options(self, tenant: str = "") -> bool:
"""check App options
- working directory
- tenant
- installation of gs and tesseract
return: True / False
"""
if not self.temp_dir:
self.temp_dir = LOCAL_TEMPDIR
if not os.path.exists(self.temp_dir):
os.mkdir(self.temp_dir)
if not os.path.exists(self.temp_dir):
self.temp_dir = ""
return False
if tenant:
if not os.path.exists(self.temp_dir + "/" + tenant):
return False
if not self.gs_path:
with os.popen("which gs") as output:
while True:
lines = output.readlines()
if lines:
if "/gs" in lines[0]:
self.gs_path = lines[0].replace("\n", "")
else:
break
if not self.tr_path:
with os.popen("which tesseract") as output:
while True:
lines = output.readlines()
if lines:
if "/tesseract" in lines[0]:
self.tr_path = lines[0].replace("\n", "")
else:
break
return True
def get_gs_version(self):
if self.gs_path:
with os.popen(self.gs_path + " -v") as output:
while True:
line = output.readline().replace("\n", "")
break
return line
else:
return ""
def get_tr_version(self):
if self.tr_path:
with os.popen(self.tr_path + " -v") as output:
while True:
line = output.readline().replace("\n", "")
break
return line
else:
return ""
def get_status(self):
"""
get services status\n
returns
tenants list[str]\n
background_tasks int
"""
t_count = []
files = os.listdir(self.temp_dir)
for file in files:
if not "." in file:
t_count.append(file)
return {
"tenants": t_count,
"background_tasks": app_data.background_task,
}
app_data = Application()
class TenantApi(BaseModel):
id: str = ""
name: str = ""
class MasterDataEnum(str, Enum):
sender = "sender"
receiver = "receiver"
entities = "entities"
class EntityApi(BaseModel):
id: str = None
name: str = ""
receiver_id: str = "" #
sender_id: str = ""
address: str = ""
tax_id: str = ""
iban: str = ""
tel: str = ""
email: str = ""
exact: str = ""
similar: str = ""
regexp: str = ""
class PredictEntity(BaseModel):
item: EntityApi
score: float = 0.0
method: str = ""
class Entity(EntityApi):
def predict(self,i_str:str) -> PredictEntity:
i_str_trim = i_str.replace(" ", "")
if self.tax_id:
if self.tax_id.replace(" ", "") in i_str_trim:
return PredictEntity(score=100, item=self, methode="tax_id")
if self.iban:
if self.iban.replace(" ", "").upper() in i_str_trim.upper():
return PredictEntity(score=100, item=self, methode="iban")
if self.tel:
if self.tel.replace(" ", "") in i_str_trim:
return PredictEntity(score=100, item=self, methode="tel")
if self.email:
if self.email.replace(" ", "").upper() in i_str_trim.upper():
return PredictEntity(score=100, item=self, methode="email")
if self.exact:
if self.exact.replace("\n"," ").replace(" ", " ") in i_str.replace("\n"," ").replace(" ", " "):
return PredictEntity(score=100, item=self, methode="exact")
if self.regexp:
if re.search(
self.regexp,
i_str,
re.IGNORECASE + re.MULTILINE
):
return PredictEntity(score=100, item=self, methode="regexp")
if self.similar:
perdict = pydocuClassfication.predict_sts([i_str],[self.similar])
score: float = perdict["score"][0][0]
return PredictEntity(score=score*100, item=self, method="similar")
return None
class ModusEnum(str, Enum):
append = "append"
replace = "replace"
delete = "delete"
class EntityList(BaseModel):
entities: List[Entity] = []
class EntityListApi(EntityList):
modus: ModusEnum = ModusEnum.append
class ClassesApi(BaseModel):
labels: List[str] = []
class TenantSave(TenantApi):
count_documents: int = 0
count_pages: int = 0
start: Union[str,None] = None
class Tenant(TenantSave):
classes: Union[ClassesApi, None] = None
sender: Union[EntityList,None] = None
receiver: Union[EntityList,None] = None
entities: Union[EntityList,None] = None
def save(self):
tenant_save = TenantSave.model_validate(self, strict=False, from_attributes=True)
filename = app_data.temp_dir + "/" + self.id + "/tenant.txt"
with open(filename, "wt") as file:
file.write(json.dumps(jsonable_encoder(tenant_save)))
filename = app_data.temp_dir + "/" + self.id + "/classes.txt"
if self.classes != None:
with open(filename, "wt") as file:
file.write(json.dumps(jsonable_encoder(self.classes)))
filename = app_data.temp_dir + "/" + self.id + "/sender.txt"
if self.sender != None:
with open(filename, "wt") as file:
file.write(json.dumps(jsonable_encoder(self.sender)))
filename = app_data.temp_dir + "/" + self.id + "/receiver.txt"
if self.receiver != None:
with open(filename, "wt") as file:
file.write(json.dumps(jsonable_encoder(self.receiver)))
return
def load_tenant( id: str, classes:bool=False, sender:bool=False, receiver:bool=False, entities:bool=False) -> Tenant:
"""load all data for a tenant from txt files in tenant directory"""
MyTenant = Tenant()
MyTenant.id = id
if not id:
raise ValueError("tenant id wrong")
filename = app_data.temp_dir + "/" + id + "/tenant.txt"
if os.path.exists(filename):
with open(filename, "rt") as file:
tenant_save = TenantSave.model_validate(json.load(file), strict=False, from_attributes=True)
MyTenant = Tenant.model_validate(tenant_save, strict=False, from_attributes=True)
filename = app_data.temp_dir + "/" + MyTenant.id + "/classes.txt"
if classes and os.path.exists(filename):
with open(filename, "rt") as file:
MyTenant.classes = ClassesApi.model_validate(json.load(file), strict=False, from_attributes=True)
filename = app_data.temp_dir + "/" + MyTenant.id + "/sender.txt"
if sender and os.path.exists(filename):
with open(filename, "rt") as file:
MyTenant.sender = EntityList.model_validate(json.load(file), strict=False, from_attributes=True)
filename = app_data.temp_dir + "/" + MyTenant.id + "/receiver.txt"
if receiver and os.path.exists(filename):
with open(filename, "rt") as file:
MyTenant.receiver = EntityList.model_validate(json.load(file), strict=False, from_attributes=True)
filename = app_data.temp_dir + "/" + MyTenant.id + "/entities.txt"
if entities and os.path.exists(filename):
with open(filename, "rt") as file:
MyTenant.entities = EntityList.model_validate(json.load(file), strict=False, from_attributes=True)
return MyTenant
class ClassificationResult(BaseModel):
label: str = ""
score: float = 0.0
class LanguEnum(str, Enum):
deu = "deu"
eng = "eng"
class InputEnum(str, Enum):
pdf = "pdf"
email = "email"
class DocumentApi(BaseModel):
id: str = ""
ext_id: Union[str, None] = None
inputpath: InputEnum = InputEnum.pdf
base64: Union[str, None] = None
email_text: Union[str, None] = None
langu: LanguEnum = LanguEnum.deu
class DocumentData(BaseModel):
sender_id: Union[str, None] = None
receiver_id: Union[str, None] = None
datum: Union[datetime, None] = None
class Document(DocumentApi):
created_at: str = ""
task: str = ""
tenant_id: str = ""
filename: str = ""
ocr_all: Union[str, None] = None
ocr_p1: Union[str, None] = None
pages: int = 0
protocol: list[str] = []
classification: list[ClassificationResult] = []
senders: list[PredictEntity] = []
receiver: list[PredictEntity] = []
entities: list[PredictEntity] = []
data: Union[DocumentData, None] = None
parse: dict = None
def save(self):
"""save data as json for async"""
if not self.filename:
raise ValueError("document "+self.id+" filename missing")
with open(self.filename + ".json", "w") as file:
file.write(json.dumps(jsonable_encoder(self)))
def do_parse(self):
"""Perform parse text-data from template"""
self.task = "60 - parse template "
self.protocol.append(
datetime.now(LOCAL_TIMEZONE).isoformat() + "/" + self.task
)
template_folder = app_data.temp_dir + "/" + self.tenant_id + "/template"
if not os.path.exists(template_folder):
#if the template directory does not exist, create it now
os.mkdir(template_folder)
msg = (
datetime.now(LOCAL_TIMEZONE).isoformat()
+ "/W - "
+ self.task
+ " Warning: "
+ "template directory does not exist, create it now"
)
self.protocol.append(msg)
#there can be no templates then -> out
return
if self.ocr_all == None or self.ocr_all == "":
msg = (
datetime.now(LOCAL_TIMEZONE).isoformat()
+ "/W - "
+ self.task
+ " Warning: "
+ "no ocr/txt data found"
)
self.protocol.append(msg)
return
#create textfile for parsing
with open(app_data.temp_dir + "/"+self.id + "_parse.txt", "w") as file:
file.write(self.ocr_all)
MyArgs = dict(
input_reader='textfile',
emplate_folder=template_folder,
exclude_built_in_templates=True,
output_format='json',
output_name=app_data.temp_dir + "/"+self.id + "_parse.json",
input_files=app_data.temp_dir + "/"+self.id + "_parse.txt"
)
invoice2data_txt_main(args=MyArgs)
with open(app_data.temp_dir + "/"+self.id + "_parse.json", "rt") as file:
self.parse = json.load(file)
if os.path.exists(app_data.temp_dir + "/"+self.id + "_parse.txt"):
os.remove(app_data.temp_dir + "/"+self.id + "_parse.txt")
if os.path.exists(app_data.temp_dir + "/"+self.id + "_parse.json"):
os.remove(app_data.temp_dir + "/"+self.id + "_parse.json")
def do_classification(self):
"""Performs classification for the document"""
self.task = "40 - classification"
self.protocol.append(
datetime.now(LOCAL_TIMEZONE).isoformat() + "/" + self.task
)
tenant = load_tenant(self.tenant_id,classes=True)
if tenant.classes != None and len(tenant.classes.labels) != 0 and self.ocr_p1:
perdict = pydocuClassfication.predict_zs(self.ocr_p1,tenant.classes.labels)
my_score = perdict["score"]
result = []
for i in range(len(tenant.classes.labels)):
result.append({"label":tenant.classes.labels[i], "score":my_score[i]*100})
def get_score(ele):
return ele["score"]
result.sort(reverse=True,key=get_score)
self.classification = result[:5]
self.save()
return
def do_find(self, list_name: str):
if list_name == "receiver":
self.task = "41 - find receiver"
tenant_list = load_tenant(self.tenant_id,receiver=True).receiver
result_list = self.receiver
elif list_name == "sender":
self.task = "42 - find sender"
tenant_list = load_tenant(self.tenant_id,sender=True).sender
result_list = self.senders
elif list_name == "entities":
self.task = "43 - find entities"
tenant_list = load_tenant(self.tenant_id,entities=True).entities
result_list = self.entities
else:
raise ValueError("list_name is wrong")
self.protocol.append(
datetime.now(LOCAL_TIMEZONE).isoformat() + "/" + self.task
)
save_true = False
if self.ocr_all and tenant_list != None:
for person in tenant_list.entities:
if ((not self.data.receiver_id or
( not person.receiver_id or
person.receiver_id == self.data.receiver_id)) and
(not self.data.sender_id or
( not person.sender_id or
person.sender_id == self.data.sender_id))):
predict = person.predict(self.ocr_all)
if predict != None:
result_list.append(predict)
save_true = True
if save_true == True:
def get_score_entity(ele:PredictEntity):
return ele.score
result_list.sort(reverse=True,key=get_score_entity)
result_list = result_list[:5]
self.save()
return
async def async_job(command):
line: str = ""
with os.popen(command) as output:
while True:
line = output.readline()
break
return line
def find_date(i_str:str, langu: str="de-DE"):
regex_list = [
{"find": "Datum[ :]*(\d{1,2}\.\d{1,2}\.\d{4})[ \\s]*", "format": "%d.%m.%Y"},
{"find":"(?:[A-Za-z])(?:,)(?: den)? (\d{1,2}\.\d{1,2}\.\d{4})[ \\s]", "format": "%d.%m.%Y"},
{"find": "Datum[ :]*(\d{1,2}\.\d{1,2}\.\d{2})[ \\s]*", "format": "%d.%m.%y"},
{"find":"(?:[A-Za-z])(?:,)(?: den)? (\d{1,2}\.\d{1,2}\.\d{2})[ \\s]", "format": "%d.%m.%y"},
{"find": "Datum[ :]*(\d{1,2}\. (?:Januar|Februar|März|April|Mai|Juni|Juli|August|September|Oktober|November|Dezember) \d{2,4})[ \\s]*", "format": "%d. %B %Y"},
{"find":"(?:[A-Za-z])(?:,)(?: den)? (\d{1,2}\. (?:Januar|Februar|März|April|Mai|Juni|Juli|August|September|Oktober|November|Dezember) \d{2,4})[ \\s]", "format": "%d. %B %Y"},
{"find": " (\d{1,2}\. (?:Januar|Februar|März|April|Mai|Juni|Juli|August|September|Oktober|November|Dezember) \d{2,4})[ \\s]*", "format": "%d. %B %Y"},
{"find": " (\d{1,2}\.\d{1,2}\.\d{4})[ \\s]*", "format": "%d.%m.%Y"},
{"find": " (\d{1,2}\.\d{1,2}\.\d{2})[ \\s]*", "format": "%d.%m.%y"},
]
try:
locale.setlocale(locale.LC_TIME, )
for ele in regex_list:
match = re.search(ele["find"], i_str, re.MULTILINE)
if match:
return datetime.strptime(match.group(1), ele["format"]), None
except Exception as err:
return None, err
return None, None
"""-----------------------------------"""
def background_task(document: Document, task: str = None):
"""background task for one document """
try:
app_data.background_task += 1
"""-----------------------------------"""
if (task == None or task == "10") and document.inputpath == InputEnum.pdf:
document.task = "10 - save as pdf"
document.protocol.append(
datetime.now(LOCAL_TIMEZONE).isoformat() + "/" + document.task
)
# convert to binary
data = base64.b64decode(document.base64)
with open(document.filename + ".pdf", "wb") as output_file:
output_file.write(data)
# save data as json for async
document.save()
"""-----------------------------------"""
if (task == None or task == "11") and document.inputpath == InputEnum.email:
document.task = "11 - save as pdf from email"
document.protocol.append(
datetime.now(LOCAL_TIMEZONE).isoformat() + "/" + document.task
)
# convert to binary
data = base64.b64decode(document.base64)
with open(document.filename + ".pdf", "wb") as output_file:
output_file.write(data)
# save data as json for async
document.save()
"""-----------------------------------"""
if (task == None or task == "20") and document.inputpath == InputEnum.pdf:
document.task = "20 - convert page to jpg"
document.protocol.append(
datetime.now(LOCAL_TIMEZONE).isoformat() + "/" + document.task
)
command = (
app_data.gs_path
+ " -dSAFER -dBATCH -dNOPAUSE -r1200 -sDEVICE=jpeg"
+ " -sOutputFile="
+ document.filename
+ "page%03d.jpg "
+ document.filename
+ ".pdf"
)
out = os.popen(command).read()
if not out:
out = "gs ready"
document.protocol.append(
datetime.now(LOCAL_TIMEZONE).isoformat() + "/ " + out
)
document.save()
#todo: wenn das pdf quer gescannt wurden, dann ggf. drehen / oder tesseract mode 12 dreht automatisch
"""-----------------------------------"""
if task == None or task == "30":
document.task = "30 - ocr"
document.protocol.append(
datetime.now(LOCAL_TIMEZONE).isoformat() + "/" + document.task
)
files = os.listdir(app_data.temp_dir + "/" + document.tenant_id)
files.sort()
document.pages = 0
document.ocr_all = ""
document.ocr_p1 = ""
for file in files:
if document.id in file:
if ".jpg" in file:
"""-----------------------------------"""
document.task = "03 - ocr file:" + file
document.protocol.append(
datetime.now(LOCAL_TIMEZONE).isoformat()
+ "/"
+ document.task
)
document.pages += 1
command = (
app_data.tr_path
+ " "
+ app_data.temp_dir
+ "/"
+ document.tenant_id
+ "/"
+ file
+ " "
+ app_data.temp_dir
+ "/"
+ document.tenant_id
+ "/"
+ file
+ " -l "
+ document.langu
+ " gosseract.ini"
)
out = os.popen(command).read()
if not out:
out = "tesseract ready"
document.protocol.append(
datetime.now(LOCAL_TIMEZONE).isoformat() + "/ " + out
)
document.save()
if os.path.exists(
app_data.temp_dir
+ "/"
+ document.tenant_id
+ "/"
+ file
+ ".txt"
):
with open(
app_data.temp_dir
+ "/"
+ document.tenant_id
+ "/"
+ file
+ ".txt",
"rt",
) as f:
document.ocr_all = document.ocr_all + f.read()
if document.pages == 1:
document.ocr_p1 = document.ocr_all
os.remove(
app_data.temp_dir
+ "/"
+ document.tenant_id
+ "/"
+ file
+ ".txt"
)
else:
document.protocol.append(
datetime.now(LOCAL_TIMEZONE).isoformat()
+ "/ file:'"
+ app_data.temp_dir
+ "/"
+ document.tenant_id
+ "/"
+ file
+ ".txt' not found"
)
os.remove(
app_data.temp_dir + "/" + document.tenant_id + "/" + file
)
document.save()
"""-----------------------------------"""
if task == None or task == "40":
if pydocuClassfication.model_sts == None:
document.task = "40 - load models for ai classification"
document.protocol.append(
datetime.now(LOCAL_TIMEZONE).isoformat()
+ "/"
+ document.task
)
pydocuClassfication.load_Models()
document.do_classification()
if not document.data:
document.data = DocumentData()
"""-----------------------------------"""
if task == None or task == "41":
document.do_find("receiver")
if len(document.receiver) != 0:
document.data.receiver_id = document.receiver[0].item.id
"""-----------------------------------"""
if task == None or task == "42":
document.do_find("sender")
if len(document.senders) != 0:
document.data.sender_id = document.senders[0].item.id
"""-----------------------------------"""
if task == None or task == "43":
document.do_find("entities")
if len(document.entities) != 0:
def find_first_receiver_id():
for entity in document.entities:
if entity.item.receiver_id:
return entity.item.receiver_id
return None
def find_first_sender_id():
for entity in document.entities:
if entity.item.sender_id:
return entity.item.sender_id
return None
if not document.data.receiver_id and find_first_receiver_id():
document.data.receiver_id = find_first_receiver_id()
if not document.data.sender_id and find_first_sender_id():
document.data.sender_id = find_first_sender_id()
"""-----------------------------------"""
if task == None or task == "51":
document.task = "51 - find date "
document.protocol.append(
datetime.now(LOCAL_TIMEZONE).isoformat() + "/" + document.task
)
if document.langu == "deu":
langu_iso = "de-DE"
elif document.langu == "eng":
langu_iso = "en_US"
else:
langu_iso = "de-DE"
finding_datum, err = find_date(document.ocr_p1,langu_iso)
if finding_datum:
if not document.data:
document.data = DocumentData()
document.data.datum = finding_datum
if err:
msg = (
datetime.now(LOCAL_TIMEZONE).isoformat()
+ "/E - "
+ document.task
+ " Error: "
+ err.args[0]
)
document.protocol.append(msg)
"""-----------------------------------"""
if task == None or task == "60":
document.do_parse()
"""-----------------------------------"""
document.task = "99 - end"
document.protocol.append(
datetime.now(LOCAL_TIMEZONE).isoformat() + "/" + document.task
)
# remove pdf-file
if document.filename:
os.remove(document.filename + ".pdf")
document.save()
tenant = load_tenant(document.tenant_id)
tenant.count_documents += 1
tenant.count_pages += document.pages
tenant.save()
except Exception as err:
msg = (
datetime.now(LOCAL_TIMEZONE).isoformat()
+ "/E - "
+ document.task
+ " Error: "
+ err.args[0]
)
document.protocol.append(msg)
document.task = "99 - end"
document.save()
print(msg)
app_data.background_task -= 1
return
@app.get("/")
async def get_main(current_user: User = Depends(get_current_active_user)):
"""system informations"""
if not len(current_user.roles):
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Incorrect role",
headers={"WWW-Authenticate": "Bearer"},
)
# prüfen, ob gs und tesseract vorhanden
if not app_data.check_options():
raise HTTPException(status_code=500, detail="installation check is invalide")
if pydocuClassfication.classifier_zs == None:
l_msg = "no"
else:
l_msg = "yes"
return {
"name": "pydocu",
"description": "Services for processing documents.",
"version": "0.1.0",
"datetime": datetime.now(LOCAL_TIMEZONE).isoformat(), # [:-3] + 'Z',
"ghostscript": app_data.get_gs_version(),
"tesseract": app_data.get_tr_version(),
"models": l_msg,
"temp_dir": app_data.temp_dir,
"status": app_data.get_status(),
"user": current_user,
}
@app.get('/favicon.ico', include_in_schema=False)
async def favicon():
return FileResponse('./favicon.ico')
@app.get("/docs", include_in_schema=False)
async def swagger_ui_html():
return get_swagger_ui_html(
openapi_url="/openapi.json",
title="FastAPI",
swagger_favicon_url="/favicon.ico"
)
@app.post("/install/{phrase}")
async def install_phrase(phrase: PhraseEnum, background_tasks: BackgroundTasks, current_user: User = Depends(get_current_active_user)):
"""Installed asynchronously
args: phrase with gs, tesseract or models"""
if not "admin" in current_user.roles:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Incorrect role",
headers={"WWW-Authenticate": "Bearer"},
)
if not app_data.check_options():
raise HTTPException(status_code=500, detail="installation check is invalide")
l_msg = "{0} available".format(phrase)
l_rcode = 0
if phrase == PhraseEnum.gs:
if not ("/gs" in app_data.gs_path):
def inst_gs_taks():
job = [
async_job(
APT_GET_PATH
+ " -y update; "
+ APT_GET_PATH
+ " -y install ghostscript"