File size: 1,256 Bytes
4fe8a8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import re
import gradio as gr

import torch


from transformers import AutoFeatureExtractor, AutoModelForImageClassification

extractor = AutoFeatureExtractor.from_pretrained("DunnBC22/dit-base-Business_Documents_Classified_v2")

model = AutoModelForImageClassification.from_pretrained("DunnBC22/dit-base-Business_Documents_Classified_v2")

device = "cuda" if torch.cuda.is_available() else "cpu"
model.to(device)


def classify_documents(image):
    # input_image = image.convert("RGB")
    inputs = extractor(images=image, return_tensor='pt')
    tensors = torch.from_numpy(inputs.pixel_values[0]).unsqueeze(0)
    model_output = model(tensors).logits
    max_index = torch.argmax(model_output)

    document_class = model.config.id2label[max_index.item()]

    return {
        "result" : str(document_class)
    }

article = "<p style='text-align: center'><a href='https://www.xelpmoc.in/' target='_blank'>Made by Xelpmoc</a></p>"

demo = gr.Interface(
    fn=classify_documents,
    inputs="image",
    outputs="json",
    title="Document Classification",
    article=article,
    enable_queue=True,
    examples=[

        ["./test_images/email_image_2.jpg"],
        ["./test_images/form_image_3.jpg"]
    ],
    cache_examples=False)

demo.launch()