This model classifies document pages (text extracted via OCR) into one of 18 categories:
1 import re
2 import unicodedata
3
4 # Same symbols removed during training
5 SYMBOLS_TO_REMOVE = r"[`~!@#$%^&*()\-+=\[\]{\}/?><,\'\":;|»«§°·¦ʼ¬£€©΄´\\…\n]"
6
7 def strip_accents_and_lowercase ( text : str ) - > str :
8 """Remove accents and convert to lowercase."""
9 return "" . join (
10 c for c in unicodedata . normalize ( "NFD" , text )
11 if unicodedata . category ( c ) != "Mn"
12 ) . lower ( )
13
14 def clean_text ( text : str , symbols_to_remove : str | None = None ) - > str :
15 """
16 Main preprocessing function.
17
18 Steps:
19 1. Remove special symbols
20 2. Collapse multiple dots into single dot
21 3. Remove accents + lowercase
22 4. Normalize whitespace
23 """
24 if symbols_to_remove :
25 text = re . sub ( symbols_to_remove , " " , text )
26
27 text = re . sub ( r"\.{2,}" , ". " , text )
28 text = strip_accents_and_lowercase ( text )
29 text = re . sub ( r"\s+" , " " , text ) . strip ( )
30 return text
31
32 def preprocess_text ( text : str ) - > str :
33 return clean_text ( text , symbols_to_remove = SYMBOLS_TO_REMOVE )
1 import json
2 import re
3 import unicodedata
4 import torch
5 from transformers import AutoModelForSequenceClassification , AutoTokenizer
6
7 # Preprocessing (REQUIRED!)
8 SYMBOLS_TO_REMOVE = r"[`~!@#$%^&*()\-+=\[\]{\}/?><,\'\":;|»«§°·¦ʼ¬£€©΄´\\…\n]"
9
10 def strip_accents_and_lowercase ( text : str ) - > str :
11 return "" . join (
12 c for c in unicodedata . normalize ( "NFD" , text )
13 if unicodedata . category ( c ) != "Mn"
14 ) . lower ( )
15
16 def clean_text ( text : str , symbols_to_remove : str | None = None ) - > str :
17 if symbols_to_remove :
18 text = re . sub ( symbols_to_remove , " " , text )
19 text = re . sub ( r"\.{2,}" , ". " , text )
20 text = strip_accents_and_lowercase ( text )
21 text = re . sub ( r"\s+" , " " , text ) . strip ( )
22 return text
23
24 def preprocess_text ( text : str ) - > str :
25 return clean_text ( text , symbols_to_remove = SYMBOLS_TO_REMOVE )
26
27 # Load model and tokenizer
28 MODEL_PATH = "path/to/model"
29 tokenizer = AutoTokenizer . from_pretrained ( MODEL_PATH )
30 model = AutoModelForSequenceClassification . from_pretrained ( MODEL_PATH )
31 model . eval ( )
32
33 # Load label mapping
34 with open ( f" { MODEL_PATH } /id2label.json" , "r" , encoding = "utf-8" ) as f :
35 id2label = json . load ( f )
36
37 # Dummy texts (examples)
38 texts = [
39 "ΔΕΛΤΙΟ ΑΣΤΥΝΟΜΙΚΗΣ ΤΑΥΤΟΤΗΤΑΣ ΠΑΠΑΔΟΠΟΥΛΟΣ ΙΩΑΝΝΗΣ" ,
40 "ΕΝΤΥΠΟ Ε1 ΔΗΛΩΣΗ ΦΟΡΟΛΟΓΙΑΣ ΕΙΣΟΔΗΜΑΤΟΣ 2024" ,
41 ]
42
43 # Preprocess texts
44 preprocessed_texts = [ preprocess_text ( t ) for t in texts ]
45
46 # Tokenize
47 inputs = tokenizer (
48 preprocessed_texts ,
49 truncation = True ,
50 padding = "max_length" ,
51 max_length = 512 ,
52 return_tensors = "pt"
53 )
54
55 # Inference
56 with torch . no_grad ( ) :
57 outputs = model ( ** inputs )
58 logits = outputs . logits
59 probabilities = torch . sigmoid ( logits ) # Multi-label sigmoid
60 predictions = probabilities . argmax ( dim = 1 )
61
62 # Get labels
63 for i , pred in enumerate ( predictions ) :
64 label = id2label [ str ( pred . item ( ) ) ]
65 confidence = probabilities [ i ] [ pred ] . item ( )
66 print ( f"Text: { texts [ i ] [ : 50] } ..." )
67 print ( f"Prediction: { label } (confidence: { confidence : .4f } )" )
68 print ( )
Text: ΔΕΛΤΙΟ ΑΣΤΥΝΟΜΙΚΗΣ ΤΑΥΤΟΤΗΤΑΣ ΠΑΠΑΔΟΠΟΥΛΟΣ ΙΩΑΝΝΗΣ...
Prediction: AA_ID_Card (confidence: 0.9842)
Text: ΕΝΤΥΠΟ Ε1 ΔΗΛΩΣΗ ΦΟΡΟΛΟΓΙΑΣ ΕΙΣΟΔΗΜΑΤΟΣ 2024...
Prediction: AA_INCOME_TAX_RETURN_-_E1 (confidence: 0.9567)
AI Services Team - Archeiothiki S.A.
Internal use only.