Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,17 +5,16 @@ import layoutparser as lp
|
|
| 5 |
import numpy as np
|
| 6 |
import cv2
|
| 7 |
|
| 8 |
-
# โ
Version
|
| 9 |
VERSION = "v2.0 - Donut + Bounding Box Overlay"
|
| 10 |
|
| 11 |
-
#
|
| 12 |
donut_model = VisionEncoderDecoderModel.from_pretrained("naver-clova-ix/donut-base-finetuned-rvlcdip")
|
| 13 |
donut_processor = DonutProcessor.from_pretrained("naver-clova-ix/donut-base-finetuned-rvlcdip")
|
| 14 |
|
| 15 |
-
#
|
| 16 |
ocr_agent = lp.TesseractAgent(languages="eng")
|
| 17 |
|
| 18 |
-
# ----[3] Run Donut inference ----
|
| 19 |
def extract_json_donut(image: Image.Image) -> dict:
|
| 20 |
task_prompt = "<s_rvlcdip>"
|
| 21 |
pixel_values = donut_processor(image, return_tensors="pt").pixel_values
|
|
@@ -24,7 +23,6 @@ def extract_json_donut(image: Image.Image) -> dict:
|
|
| 24 |
result = donut_processor.batch_decode(outputs, skip_special_tokens=True)[0]
|
| 25 |
return result
|
| 26 |
|
| 27 |
-
# ----[4] Draw matched fields ----
|
| 28 |
def highlight_matched_fields(image: Image.Image, field_values: list):
|
| 29 |
image_cv = np.array(image.convert("RGB"))
|
| 30 |
layout = ocr_agent.detect(image_cv)
|
|
@@ -39,35 +37,27 @@ def highlight_matched_fields(image: Image.Image, field_values: list):
|
|
| 39 |
draw.text((box[0], box[1] - 10), field_val, fill="green")
|
| 40 |
return image
|
| 41 |
|
| 42 |
-
# ----[5] Process and return result ----
|
| 43 |
def analyze_receipt(image):
|
| 44 |
try:
|
| 45 |
json_result = extract_json_donut(image)
|
| 46 |
-
|
| 47 |
-
# Extract words to highlight (simplified filter)
|
| 48 |
field_keywords = ["total", "tax", "date", "amount", "price", "item"]
|
| 49 |
highlight_candidates = [
|
| 50 |
word.strip(",:.") for word in json_result.split()
|
| 51 |
if any(k in word.lower() for k in field_keywords)
|
| 52 |
]
|
| 53 |
-
|
| 54 |
boxed_image = highlight_matched_fields(image.copy(), highlight_candidates)
|
| 55 |
-
|
| 56 |
-
# Append version to output
|
| 57 |
final_output = f"VERSION: {VERSION}\n\n{json_result}"
|
| 58 |
return final_output, boxed_image
|
| 59 |
-
|
| 60 |
except Exception as e:
|
| 61 |
return f"Error: {str(e)}\nVERSION: {VERSION}", image
|
| 62 |
|
| 63 |
-
#
|
| 64 |
with gr.Blocks(title=f"Donut OCR ({VERSION})") as demo:
|
| 65 |
-
gr.Markdown(f"# ๐งพ Donut
|
| 66 |
-
gr.Markdown("Upload a receipt to extract
|
| 67 |
-
image_input = gr.Image(type="pil", label="Upload Receipt")
|
| 68 |
-
output_json = gr.Textbox(label="๐ Extracted
|
| 69 |
-
output_image = gr.Image(label="๐ธ Highlighted Receipt")
|
| 70 |
-
|
| 71 |
btn = gr.Button("Analyze")
|
| 72 |
btn.click(analyze_receipt, inputs=image_input, outputs=[output_json, output_image])
|
| 73 |
|
|
|
|
| 5 |
import numpy as np
|
| 6 |
import cv2
|
| 7 |
|
| 8 |
+
# โ
Version
|
| 9 |
VERSION = "v2.0 - Donut + Bounding Box Overlay"
|
| 10 |
|
| 11 |
+
# Load Donut model
|
| 12 |
donut_model = VisionEncoderDecoderModel.from_pretrained("naver-clova-ix/donut-base-finetuned-rvlcdip")
|
| 13 |
donut_processor = DonutProcessor.from_pretrained("naver-clova-ix/donut-base-finetuned-rvlcdip")
|
| 14 |
|
| 15 |
+
# Load LayoutParser OCR
|
| 16 |
ocr_agent = lp.TesseractAgent(languages="eng")
|
| 17 |
|
|
|
|
| 18 |
def extract_json_donut(image: Image.Image) -> dict:
|
| 19 |
task_prompt = "<s_rvlcdip>"
|
| 20 |
pixel_values = donut_processor(image, return_tensors="pt").pixel_values
|
|
|
|
| 23 |
result = donut_processor.batch_decode(outputs, skip_special_tokens=True)[0]
|
| 24 |
return result
|
| 25 |
|
|
|
|
| 26 |
def highlight_matched_fields(image: Image.Image, field_values: list):
|
| 27 |
image_cv = np.array(image.convert("RGB"))
|
| 28 |
layout = ocr_agent.detect(image_cv)
|
|
|
|
| 37 |
draw.text((box[0], box[1] - 10), field_val, fill="green")
|
| 38 |
return image
|
| 39 |
|
|
|
|
| 40 |
def analyze_receipt(image):
|
| 41 |
try:
|
| 42 |
json_result = extract_json_donut(image)
|
|
|
|
|
|
|
| 43 |
field_keywords = ["total", "tax", "date", "amount", "price", "item"]
|
| 44 |
highlight_candidates = [
|
| 45 |
word.strip(",:.") for word in json_result.split()
|
| 46 |
if any(k in word.lower() for k in field_keywords)
|
| 47 |
]
|
|
|
|
| 48 |
boxed_image = highlight_matched_fields(image.copy(), highlight_candidates)
|
|
|
|
|
|
|
| 49 |
final_output = f"VERSION: {VERSION}\n\n{json_result}"
|
| 50 |
return final_output, boxed_image
|
|
|
|
| 51 |
except Exception as e:
|
| 52 |
return f"Error: {str(e)}\nVERSION: {VERSION}", image
|
| 53 |
|
| 54 |
+
# Gradio UI
|
| 55 |
with gr.Blocks(title=f"Donut OCR ({VERSION})") as demo:
|
| 56 |
+
gr.Markdown(f"# ๐งพ Donut Receipt AI (with Visual Overlay)\n**Version:** `{VERSION}`")
|
| 57 |
+
gr.Markdown("Upload a receipt to extract fields using Donut + highlight using LayoutParser.")
|
| 58 |
+
image_input = gr.Image(type="pil", label="๐ค Upload Receipt")
|
| 59 |
+
output_json = gr.Textbox(label="๐ Extracted Data (with Version)")
|
| 60 |
+
output_image = gr.Image(label="๐ธ Highlighted Receipt Image")
|
|
|
|
| 61 |
btn = gr.Button("Analyze")
|
| 62 |
btn.click(analyze_receipt, inputs=image_input, outputs=[output_json, output_image])
|
| 63 |
|