File size: 10,694 Bytes
cc37d53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e0bfcf
cc37d53
 
 
 
 
6e0bfcf
cc37d53
 
 
 
 
 
6e0bfcf
cc37d53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import io
import json
import os
import shutil
import time
from collections import Counter
from pathlib import Path

import fitz
import numpy as np
import pandas as pd
import plotly.express as px
import streamlit as st
import torch
import torch.nn.functional as F
from easyocr import Reader
from PIL import Image
from tqdm import tqdm
from transformers import (LayoutLMv3FeatureExtractor,
                          LayoutLMv3ForSequenceClassification,
                          LayoutLMv3Processor, LayoutLMv3TokenizerFast)

DEVICE = "cuda:0" if torch.cuda.is_available() else "cpu"
# DEVICE = "cpu"
MICROSOFT_HODEL_NAME = "microsoft/layoutlmv3-base"
MODEL_NAME = "arthur-lima/layoutlmv3-triagem-documentos"


def create_bounding_box(bbox_data, width_scale: float, height_scale: float):
    xs = []
    ys = []
    for x, y in bbox_data:
        xs.append(x)
        ys.append(y)
    left = int(min(xs) * width_scale)
    top = int(min(ys) * height_scale)
    right = int(max(xs) * width_scale)
    bottom = int(max(ys) * height_scale)
    return [left, top, right, bottom]


@st.cache_data
def create_ocr_reader():
    return Reader(["pt", "en"], gpu=True)
    # return Reader(["pt", "en"], gpu=False)


@st.cache_data
def create_processor():
    feature_extractor = LayoutLMv3FeatureExtractor(apply_ocr=False)
    tokenizer = LayoutLMv3TokenizerFast.from_pretrained(MICROSOFT_HODEL_NAME)
    return LayoutLMv3Processor(feature_extractor, tokenizer)


@st.cache_data
def create_model(revision="main"):
    model = LayoutLMv3ForSequenceClassification.from_pretrained(MODEL_NAME, revision=revision)
    return model.eval().to(DEVICE)

def pdf2jpg(src: Path, dest_path: Path=None, dpi=100, limit=None):
    """
    Converte um arquivo PDF em JPG. 
    Se forem várias páginas, serão geradas várias imagens
    """
    # Tratamento dos caminhos de destino
    if (dest_path is None):
        # Não passou caminho
        dest = src.parent / src.stem
    elif (dest_path.suffix == ""):
        # Só passou uma pasta
        dest = dest_path / src.stem
    else:
        # Passou um caminho com arquivo
        dest = dest_path.parent / dest_path.stem

    zoom = dpi / 72  # zoom factor, standard: 72 dpi
    magnify = fitz.Matrix(zoom, zoom)  # magnifies in x, resp. y direction
    try:
        doc = fitz.open(src)  # open document
        for page in doc:
            pix = page.get_pixmap(matrix=magnify)  # render page to an image
            dest_final_filename = Path(str(dest) + f"-{page.number}.jpg")
            pix.save(dest_final_filename)
        return True
    except Exception as e:
        print(f"\nProblemas na conversão para JPG do arquivo PDF {src}: " + str(e))
        return False

def classifyPDF(
    pdfpath: Path, model, processor, reader: Reader = None, dpi=100
) -> str:
    def create_bounding_box(bbox_data, width_scale: float = 1, height_scale: float = 1):
        xs = []
        ys = []
        for x, y in bbox_data:
            xs.append(x)
            ys.append(y)
        left = int(min(xs) * width_scale)
        top = int(min(ys) * height_scale)
        right = int(max(xs) * width_scale)
        bottom = int(max(ys) * height_scale)
        return [left, top, right, bottom]

    # Cria pasta temporária para converter em JPG
    tmp = Path("temp")
    if os.path.exists(tmp):
        tmp = Path("temp_classification")
        shutil.rmtree(tmp, ignore_errors=True)
    os.mkdir(tmp)
    image_path = tmp / Path(pdfpath.name).with_suffix(".jpg")
    pdf2jpg(pdfpath, image_path, dpi)
    if reader is None:
        reader = Reader(["pt", "en"])
    time.sleep(0.5)

    # Verificar se há várias páginas
    if len(os.listdir(tmp)) > 1:
        # Várias páginas, escolher a da maioria
        results = []
        all_probs = []
        for img in tqdm(os.listdir(tmp)):
            image_path = tmp / img
            # Ler cada página (em bytes) via OCR
            image = Image.open(image_path)
            with open(image_path, "rb") as f:
                image_bytes = f.read()
            ocr_result = reader.readtext(image_bytes, batch_size=1)
            ocr_page = []
            for bbox, word, confidence in ocr_result:
                ocr_page.append(
                    {"word": word, "bounding_box": create_bounding_box(bbox)}
                )
                with Path(image_path).with_suffix(".json").open("w") as f:
                    json.dump(ocr_page, f)

            # Fazer a previsão
            predicted_class, probabilities = predict(
                image, image_bytes, reader, processor, model
            )
            # result = model.config.id2label[predicted_class]
            results.append(predicted_class)

            if (len(all_probs) == 0): all_probs = np.array(probabilities)
            else: all_probs += np.array(probabilities)
        # Resultado é o mais comum
        result = Counter(results).most_common(1)
        result = result[0][0]
        all_probs = all_probs * (1 / len(os.listdir(tmp)))
        predicted_class, probabilities = result, all_probs


    else:
        # Uma página
        image_path = tmp / (os.listdir(tmp)[0])

        # Ler a imagem via OCR
        image = Image.open(image_path)
        with open(image_path, "rb") as f:
            image_bytes = f.read()
        ocr_result = reader.readtext(image_bytes, batch_size=1)
        ocr_page = []
        for bbox, word, confidence in ocr_result:
            ocr_page.append({"word": word, "bounding_box": create_bounding_box(bbox)})
            with image_path.with_suffix(".json").open("w") as f:
                json.dump(ocr_page, f)

        # Fazer a previsão
        predicted_class, probabilities = predict(
            image, image_bytes, reader, processor, model
        )
        probabilities = np.array(probabilities)
        # result = model.config.id2label[predicted_class]

    probabilities = probabilities / np.sqrt(np.sum(probabilities**2))
    return predicted_class, probabilities


def predict(
    image: Image.Image,
    image_bytes: bytes,
    reader: Reader,
    processor: LayoutLMv3Processor,
    model: LayoutLMv3ForSequenceClassification,
):

    ocr_result = reader.readtext(image_bytes)

    width, height = image.size
    width_scale = 1000 / width
    height_scale = 1000 / height

    words = []
    boxes = []
    for bbox, word, _ in ocr_result:
        boxes.append(create_bounding_box(bbox, width_scale, height_scale))
        words.append(word)

    encoding = processor(
        image,
        words,
        boxes=boxes,
        max_length=512,
        padding="max_length",
        truncation=True,
        return_tensors="pt",
    )

    with torch.inference_mode():
        output = model(
            input_ids=encoding["input_ids"].to(DEVICE),
            attention_mask=encoding["attention_mask"].to(DEVICE),
            bbox=encoding["bbox"].to(DEVICE),
            pixel_values=encoding["pixel_values"].to(DEVICE),
        )

    logits = output.logits
    predicted_class = logits.argmax()
    probabilities = (
        F.softmax(logits, dim=-1).flatten().tolist()
    )  # Convertendo em probabilidades novamente
    # return model.config.id2label[predicted_class.item()]
    return predicted_class.detach().item(), probabilities


reader = create_ocr_reader()
processor = create_processor()
model = create_model(revision="e34c270")

# Logo
c1, c2, c3 = st.columns([2.7,5,1])
c2.image("resources/previsa_cinza.png", width=250)

# Caixas de Upload
col1, col2 = st.columns(2)
with col1:
    uploaded_file = st.file_uploader("Upload: Notas Fiscais de Entrada", ["jpg", "pdf"])
    uploaded_file = st.file_uploader("Upload: Notas Fiscais de Saída", ["jpg", "pdf"])
    uploaded_file = st.file_uploader("Upload: Notas Fiscais de Retenção", ["jpg", "pdf"])
    uploaded_file = st.file_uploader("Upload: Notas Fiscais de Serviços", ["jpg", "pdf"])
with col2:
    uploaded_file = st.file_uploader("Upload: Documentos Aluguel", ["jpg", "pdf"])
    uploaded_file = st.file_uploader("Upload: Documentos Contábeis", ["jpg", "pdf"])
    uploaded_file = st.file_uploader("Upload: Documentos Tributos", ["jpg", "pdf"])
    uploaded_file = st.file_uploader("Upload: Documentos MEI", ["jpg", "pdf"])
uploaded_file = st.file_uploader("Upload: Extrato Bancário", ["jpg", "pdf"])

def plot_confianca(probabilities, model):
    # Desenhar o gráfico de confianças
    with st.spinner("Criando gráficos de confiança..."):
        df_predictions = pd.DataFrame(
            {
                "Tipo Documento": list(model.config.id2label.values()),
                "Confiança": probabilities,
            }
        )
        fig = px.bar(df_predictions, x="Tipo Documento", y="Confiança")
        fig.update_layout({
            'plot_bgcolor': '#FFFFFF'
        })
        fig.update_traces(marker_color='#fcaf17')
        st.plotly_chart(fig, use_container_width=True)

# Processamento
if uploaded_file is not None:
    c1, c2, c3 = st.columns([2.4,5,1])

    try: 
        # Tentar decodificar como PDF
        if os.path.exists("temp"): 
            shutil.rmtree("temp", ignore_errors=True)
        os.mkdir("temp")
        doc = fitz.Document(stream=uploaded_file.getvalue())
        pdfPath = Path("temp/temp.pdf")
        doc.save(pdfPath)

        # Imprimir a primeira página
        for page in doc:
            pix = page.get_pixmap()
            pix.save("temp/icon-page-1.jpg")
            c2.image("temp/icon-page-1.jpg", "Página do documento", width=300)
            break

        # Fazer a previsão
        with st.spinner("Fazendo previsão..."):
            predicted_class, probabilities = classifyPDF(pdfPath, model, processor, reader)
        print(probabilities)
    except fitz.fitz.FileDataError:
        # Carregar a imagem passada
        image_bytes = uploaded_file.getvalue()
        bytes_data = io.BytesIO(image_bytes)
        image = Image.open(bytes_data)

        # Mostrar a imagem
        c2.image(image, "Página do documento", width=300)

        # Fazer a previsão
        with st.spinner("Fazendo previsão..."):
            predicted_class, probabilities = predict(
                image, image_bytes, reader, processor, model
            )
    finally:
        # Remover a pasta temporária se ainda existir
        if os.path.exists("temp"): 
            shutil.rmtree("temp", ignore_errors=True)
        if os.path.exists("temp_classification"): 
            shutil.rmtree("temp_classification", ignore_errors=True)

    # Imprimir o resultado na tela
    predicted_label = model.config.id2label[predicted_class]
    st.markdown(f"Tipo do documento previsto: **{predicted_label}**")

    plot_confianca(probabilities, model)