Spaces:
Sleeping
Sleeping
João Pedro
commited on
Commit
·
056ccc3
1
Parent(s):
861182c
add tentative app.py
Browse files
app.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import LayoutLMv3Processor, LayoutLMv3ForTokenClassification
|
3 |
+
from pdf2image import convert_from_path
|
4 |
+
from PIL import Image
|
5 |
+
|
6 |
+
# Load model and processor
|
7 |
+
processor = LayoutLMv3Processor.from_pretrained("microsoft/layoutlmv3-base")
|
8 |
+
model = LayoutLMv3ForTokenClassification.from_pretrained("microsoft/layoutlmv3-base")
|
9 |
+
|
10 |
+
st.title("Document Classification with LayoutLMv3")
|
11 |
+
|
12 |
+
# File uploader for PDFs, JPGs, and PNGs
|
13 |
+
uploaded_files = st.file_uploader(
|
14 |
+
"Upload Document", type=["pdf", "jpg", "png"], accept_multiple_files=False
|
15 |
+
)
|
16 |
+
|
17 |
+
if uploaded_files:
|
18 |
+
for uploaded_file in uploaded_files:
|
19 |
+
if uploaded_file.type == "application/pdf":
|
20 |
+
images = convert_from_path(uploaded_file)
|
21 |
+
else:
|
22 |
+
images = [Image.open(uploaded_file)]
|
23 |
+
|
24 |
+
# Process each image for classification
|
25 |
+
for i, image in enumerate(images):
|
26 |
+
st.image(image, caption=f'Uploaded Image {i}', use_column_width=True)
|
27 |
+
# Prepare image for model input
|
28 |
+
encoding = processor(image, return_tensors="pt")
|
29 |
+
outputs = model(**encoding)
|
30 |
+
predictions = outputs.logits.argmax(-1)
|
31 |
+
|
32 |
+
# Display predictions (you may want to map indices to labels)
|
33 |
+
st.write(f"Predictions: {predictions}")
|
34 |
+
|
35 |
+
# User feedback section
|
36 |
+
feedback = st.radio(
|
37 |
+
"Is the classification correct?", ("Yes", "No")
|
38 |
+
)
|
39 |
+
if feedback == "No":
|
40 |
+
correct_label = st.text_input(
|
41 |
+
"Please provide the correct label:"
|
42 |
+
)
|
43 |
+
# Here you can implement logic to store or process feedback
|