Update app.py
Browse files
app.py
CHANGED
@@ -1,33 +1,25 @@
|
|
|
|
1 |
from transformers import LayoutLMv3Processor, LayoutLMv3ForTokenClassification
|
2 |
from PIL import Image
|
3 |
-
import gradio as gr
|
4 |
|
5 |
-
# Load processor and model
|
6 |
processor = LayoutLMv3Processor.from_pretrained("microsoft/layoutlmv3-base")
|
7 |
model = LayoutLMv3ForTokenClassification.from_pretrained("microsoft/layoutlmv3-base", num_labels=5)
|
8 |
|
9 |
-
# Define function to process invoices
|
10 |
def extract_invoice_data(image):
|
11 |
-
# Open image
|
12 |
image = Image.open(image).convert("RGB")
|
13 |
-
|
14 |
-
# Encode image
|
15 |
encoding = processor(image, return_tensors="pt")
|
16 |
-
|
17 |
-
# Perform inference
|
18 |
outputs = model(**encoding)
|
19 |
predictions = outputs.logits.argmax(-1).squeeze().tolist()
|
20 |
-
|
21 |
-
# Return predictions (adjust to return structured data)
|
22 |
return {"Predictions": predictions}
|
23 |
|
24 |
-
#
|
25 |
interface = gr.Interface(
|
26 |
fn=extract_invoice_data,
|
27 |
-
inputs=gr.
|
28 |
outputs="json",
|
29 |
-
title="Invoice Data Extraction
|
30 |
)
|
31 |
|
32 |
-
# Launch Gradio app
|
33 |
interface.launch()
|
|
|
1 |
+
import gradio as gr
|
2 |
from transformers import LayoutLMv3Processor, LayoutLMv3ForTokenClassification
|
3 |
from PIL import Image
|
|
|
4 |
|
5 |
+
# Load the processor and model
|
6 |
processor = LayoutLMv3Processor.from_pretrained("microsoft/layoutlmv3-base")
|
7 |
model = LayoutLMv3ForTokenClassification.from_pretrained("microsoft/layoutlmv3-base", num_labels=5)
|
8 |
|
|
|
9 |
def extract_invoice_data(image):
|
10 |
+
# Open and preprocess the image
|
11 |
image = Image.open(image).convert("RGB")
|
|
|
|
|
12 |
encoding = processor(image, return_tensors="pt")
|
|
|
|
|
13 |
outputs = model(**encoding)
|
14 |
predictions = outputs.logits.argmax(-1).squeeze().tolist()
|
|
|
|
|
15 |
return {"Predictions": predictions}
|
16 |
|
17 |
+
# Update Gradio interface
|
18 |
interface = gr.Interface(
|
19 |
fn=extract_invoice_data,
|
20 |
+
inputs=gr.Image(type="file"),
|
21 |
outputs="json",
|
22 |
+
title="Invoice Data Extraction"
|
23 |
)
|
24 |
|
|
|
25 |
interface.launch()
|