Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,56 +1,47 @@
|
|
1 |
import streamlit as st
|
2 |
-
import fitz # PyMuPDF
|
3 |
-
import pytesseract
|
4 |
from PIL import Image
|
5 |
-
import
|
6 |
-
import
|
7 |
-
|
8 |
-
|
9 |
-
st.
|
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 |
-
|
47 |
-
|
48 |
-
|
49 |
-
st.subheader("Extracted Information:")
|
50 |
-
st.write(pd.DataFrame([extracted_data]))
|
51 |
-
|
52 |
-
# Option to download Excel
|
53 |
-
df = pd.DataFrame([extracted_data])
|
54 |
-
csv = df.to_csv(index=False)
|
55 |
-
st.download_button("📥 Download as CSV", csv, "invoice_data.csv", "text/csv")
|
56 |
|
|
|
1 |
import streamlit as st
|
|
|
|
|
2 |
from PIL import Image
|
3 |
+
from transformers import DonutProcessor, VisionEncoderDecoderModel
|
4 |
+
import torch
|
5 |
+
|
6 |
+
# Load Donut model and processor
|
7 |
+
@st.cache_resource
|
8 |
+
def load_model():
|
9 |
+
processor = DonutProcessor.from_pretrained("naver-clova-ix/donut-base-finetuned-docvqa")
|
10 |
+
model = VisionEncoderDecoderModel.from_pretrained("naver-clova-ix/donut-base-finetuned-docvqa")
|
11 |
+
return processor, model
|
12 |
+
|
13 |
+
processor, model = load_model()
|
14 |
+
|
15 |
+
st.title("🧾 Invoice Table Extractor - Hugging Face Donut")
|
16 |
+
st.write("Upload an invoice image to extract the table (code article, designation, quantity, unit prices, totals).")
|
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 |
+
prompt = "<s_docvqa><question>Extract the invoice items table with code article, designation, quantity, unit prices, and totals.</question><answer>"
|
30 |
+
decoder_input_ids = processor.tokenizer(prompt, add_special_tokens=False, return_tensors="pt").input_ids
|
31 |
+
|
32 |
+
# Generate prediction
|
33 |
+
outputs = model.generate(
|
34 |
+
pixel_values,
|
35 |
+
decoder_input_ids=decoder_input_ids,
|
36 |
+
max_length=512,
|
37 |
+
early_stopping=True
|
38 |
+
)
|
39 |
+
|
40 |
+
# Decode response
|
41 |
+
result = processor.batch_decode(outputs, skip_special_tokens=True)[0]
|
42 |
+
result = result.replace("<s_docvqa><question>", "").replace("</question><answer>", "").strip()
|
43 |
+
|
44 |
+
st.subheader("📋 Extracted Table Info")
|
45 |
+
st.code(result)
|
46 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|