Update app.py
Browse files
app.py
CHANGED
@@ -1,40 +1,47 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
3 |
from PIL import Image
|
4 |
-
import pytesseract # Install
|
5 |
|
6 |
# Load your fine-tuned model and tokenizer
|
7 |
model_name = "quadranttechnologies/Receipt_Image_Analyzer"
|
8 |
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
9 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
10 |
|
11 |
-
#
|
12 |
-
def
|
13 |
-
#
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
outputs = model(**inputs)
|
21 |
logits = outputs.logits
|
22 |
predicted_class = logits.argmax(-1).item()
|
23 |
-
|
24 |
-
#
|
25 |
-
|
26 |
-
"extracted_text":
|
27 |
"predicted_class": predicted_class
|
28 |
}
|
29 |
-
return result
|
30 |
|
31 |
# Create a Gradio interface
|
32 |
interface = gr.Interface(
|
33 |
-
fn=
|
34 |
-
inputs=gr.
|
35 |
-
outputs="json", #
|
36 |
title="Receipt Image Analyzer",
|
37 |
-
description="Upload a receipt
|
38 |
)
|
39 |
|
40 |
# Launch the Gradio app
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
3 |
from PIL import Image
|
4 |
+
import pytesseract # Install via `pip install pytesseract` and ensure Tesseract OCR is installed on your system
|
5 |
|
6 |
# Load your fine-tuned model and tokenizer
|
7 |
model_name = "quadranttechnologies/Receipt_Image_Analyzer"
|
8 |
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
9 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
10 |
|
11 |
+
# Function to preprocess image and extract text using OCR
|
12 |
+
def ocr_extract_text(image):
|
13 |
+
# Convert image to grayscale for better OCR accuracy
|
14 |
+
gray_image = image.convert("L")
|
15 |
+
# Use Tesseract OCR to extract text
|
16 |
+
extracted_text = pytesseract.image_to_string(gray_image)
|
17 |
+
return extracted_text
|
18 |
+
|
19 |
+
# Define a function to analyze the receipt image
|
20 |
+
def analyze_receipt_image(receipt_image):
|
21 |
+
# Extract text from the image
|
22 |
+
receipt_text = ocr_extract_text(receipt_image)
|
23 |
+
if not receipt_text.strip():
|
24 |
+
return {"error": "No text detected in the image."}
|
25 |
+
|
26 |
+
# Use the fine-tuned model to analyze the extracted text
|
27 |
+
inputs = tokenizer(receipt_text, return_tensors="pt", truncation=True, padding=True)
|
28 |
outputs = model(**inputs)
|
29 |
logits = outputs.logits
|
30 |
predicted_class = logits.argmax(-1).item()
|
31 |
+
|
32 |
+
# Return the extracted text and predicted class as JSON
|
33 |
+
return {
|
34 |
+
"extracted_text": receipt_text,
|
35 |
"predicted_class": predicted_class
|
36 |
}
|
|
|
37 |
|
38 |
# Create a Gradio interface
|
39 |
interface = gr.Interface(
|
40 |
+
fn=analyze_receipt_image,
|
41 |
+
inputs=gr.Image(type="pil"), # Updated to use gr.Image
|
42 |
+
outputs="json", # Output will be displayed as JSON
|
43 |
title="Receipt Image Analyzer",
|
44 |
+
description="Upload an image of a receipt. The app extracts text and analyzes it using a fine-tuned LLM model.",
|
45 |
)
|
46 |
|
47 |
# Launch the Gradio app
|