Delete app.py
Browse files
app.py
DELETED
@@ -1,57 +0,0 @@
|
|
1 |
-
from PIL import Image
|
2 |
-
from transformers import LayoutLMv3ForTokenClassification, LayoutLMv3Processor
|
3 |
-
import gradio as gr
|
4 |
-
import torch
|
5 |
-
import pytesseract
|
6 |
-
import os
|
7 |
-
import os
|
8 |
-
|
9 |
-
|
10 |
-
# Set the Linux path for Tesseract
|
11 |
-
pytesseract.pytesseract.tesseract_cmd = "/usr/bin/tesseract"
|
12 |
-
print("Tesseract version:", os.popen("tesseract --version").read())
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
# Load the fine-tuned model and processor from local files
|
17 |
-
model_path = "./" # Path to the directory containing the uploaded model files
|
18 |
-
model = LayoutLMv3ForTokenClassification.from_pretrained(model_path)
|
19 |
-
processor = LayoutLMv3Processor.from_pretrained(model_path, apply_ocr=True)
|
20 |
-
|
21 |
-
|
22 |
-
# Define label mapping
|
23 |
-
id2label = {0: "company", 1: "date", 2: "address", 3: "total", 4: "other"}
|
24 |
-
|
25 |
-
# Define prediction function
|
26 |
-
def predict_receipt(image):
|
27 |
-
try:
|
28 |
-
# Preprocess the image
|
29 |
-
encoding = processor(image, return_tensors="pt", truncation=True, padding="max_length", max_length=512)
|
30 |
-
input_ids = encoding["input_ids"]
|
31 |
-
attention_mask = encoding["attention_mask"]
|
32 |
-
bbox = encoding["bbox"]
|
33 |
-
pixel_values = encoding["pixel_values"]
|
34 |
-
|
35 |
-
# Get model predictions
|
36 |
-
outputs = model(input_ids=input_ids, attention_mask=attention_mask, bbox=bbox, pixel_values=pixel_values)
|
37 |
-
predictions = outputs.logits.argmax(-1).squeeze().tolist()
|
38 |
-
|
39 |
-
# Map predictions to labels
|
40 |
-
labeled_output = {id2label[pred]: idx for idx, pred in enumerate(predictions) if pred != 4}
|
41 |
-
|
42 |
-
return labeled_output
|
43 |
-
except Exception as e:
|
44 |
-
return {"error": str(e)}
|
45 |
-
|
46 |
-
# Create Gradio Interface
|
47 |
-
interface = gr.Interface(
|
48 |
-
fn=predict_receipt,
|
49 |
-
inputs=gr.Image(type="pil"),
|
50 |
-
outputs="json",
|
51 |
-
title="Receipt Information Analyzer",
|
52 |
-
description="Upload a scanned receipt image to extract information like company name, date, address, and total."
|
53 |
-
)
|
54 |
-
|
55 |
-
# Launch the interface
|
56 |
-
if __name__ == "__main__":
|
57 |
-
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|