Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -17,6 +17,34 @@ st.write("Upload an invoice image to extract the table (code article, designatio
|
|
| 17 |
|
| 18 |
uploaded_file = st.file_uploader("Choose an image", type=["png", "jpg", "jpeg"])
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
if uploaded_file is not None:
|
| 21 |
image = Image.open(uploaded_file).convert("RGB")
|
| 22 |
st.image(image, caption="Uploaded Invoice", use_column_width=True)
|
|
@@ -43,5 +71,21 @@ if uploaded_file is not None:
|
|
| 43 |
|
| 44 |
st.subheader("π Extracted Table Info")
|
| 45 |
st.code(result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
|
|
|
|
| 17 |
|
| 18 |
uploaded_file = st.file_uploader("Choose an image", type=["png", "jpg", "jpeg"])
|
| 19 |
|
| 20 |
+
if uploaded_file is not None:
|
| 21 |
+
image = Image.open(uploaded_file).convert("RGB")
|
| 22 |
+
st.image(image, caption="Uploaded Invoice", use_column_width=True)
|
| 23 |
+
|
| 24 |
+
with st.spinner("π Analyzing..."):
|
| 25 |
+
# Preprocess image
|
| 26 |
+
pixel_values = processor(image, return_tensors="pt").pixel_values
|
| 27 |
+
|
| 28 |
+
# Prompt for table extraction
|
| 29 |
+
import streamlit as st
|
| 30 |
+
from PIL import Image
|
| 31 |
+
from transformers import DonutProcessor, VisionEncoderDecoderModel
|
| 32 |
+
import torch
|
| 33 |
+
|
| 34 |
+
# Load Donut model and processor
|
| 35 |
+
@st.cache_resource
|
| 36 |
+
def load_model():
|
| 37 |
+
processor = DonutProcessor.from_pretrained("naver-clova-ix/donut-base-finetuned-docvqa")
|
| 38 |
+
model = VisionEncoderDecoderModel.from_pretrained("naver-clova-ix/donut-base-finetuned-docvqa")
|
| 39 |
+
return processor, model
|
| 40 |
+
|
| 41 |
+
processor, model = load_model()
|
| 42 |
+
|
| 43 |
+
st.title("π§Ύ Invoice Table Extractor - Hugging Face Donut")
|
| 44 |
+
st.write("Upload an invoice image to extract the table (code article, designation, quantity, unit prices, totals).")
|
| 45 |
+
|
| 46 |
+
uploaded_file = st.file_uploader("Choose an image", type=["png", "jpg", "jpeg"])
|
| 47 |
+
|
| 48 |
if uploaded_file is not None:
|
| 49 |
image = Image.open(uploaded_file).convert("RGB")
|
| 50 |
st.image(image, caption="Uploaded Invoice", use_column_width=True)
|
|
|
|
| 71 |
|
| 72 |
st.subheader("π Extracted Table Info")
|
| 73 |
st.code(result)
|
| 74 |
+
decoder_input_ids = processor.tokenizer(prompt, add_special_tokens=False, return_tensors="pt").input_ids
|
| 75 |
+
|
| 76 |
+
# Generate prediction
|
| 77 |
+
outputs = model.generate(
|
| 78 |
+
pixel_values,
|
| 79 |
+
decoder_input_ids=decoder_input_ids,
|
| 80 |
+
max_length=512,
|
| 81 |
+
early_stopping=True
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
# Decode response
|
| 85 |
+
result = processor.batch_decode(outputs, skip_special_tokens=True)[0]
|
| 86 |
+
result = result.replace("<s_docvqa><question>", "").replace("</question><answer>", "").strip()
|
| 87 |
+
|
| 88 |
+
st.subheader("π Extracted Table Info")
|
| 89 |
+
st.code(result)
|
| 90 |
|
| 91 |
|