File size: 1,206 Bytes
1880d16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bfb9266
1880d16
 
 
 
 
e14fab5
f9cf908
1880d16
 
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
import gradio as gr


def classify(input_img):

    from transformers import (
        AutoModelForSequenceClassification,
        LayoutLMv2FeatureExtractor,
        LayoutLMv2Tokenizer,
        LayoutLMv2Processor,
    )

    model = AutoModelForSequenceClassification.from_pretrained(
        "fedihch/InvoiceReceiptClassifier"
    )
    feature_extractor = LayoutLMv2FeatureExtractor()
    tokenizer = LayoutLMv2Tokenizer.from_pretrained("microsoft/layoutlmv2-base-uncased")
    processor = LayoutLMv2Processor(feature_extractor, tokenizer)
    encoded_inputs = processor(input_img, return_tensors="pt")
    for k, v in encoded_inputs.items():
        encoded_inputs[k] = v.to(model.device)
    outputs = model(**encoded_inputs)
    logits = outputs.logits
    predicted_class_idx = logits.argmax(-1).item()

    id2label = {0: "invoice", 1: "receipt"}
    return id2label[predicted_class_idx]

examples =[['Receipt.jpg'],['invoice.webp'],]
demo = gr.Interface(
    fn=classify,
    inputs=gr.Image(shape=(200, 200)),
    outputs="text",
    allow_flagging="manual",
    description="Upload an invoice or receipt image and the model will classify it!",
    examples=examples,
)
demo.launch(share=True)