|
import gradio as gr |
|
from transformers import AutoModelForSequenceClassification, AutoTokenizer |
|
from PIL import Image |
|
import pytesseract |
|
|
|
|
|
model_name = "quadranttechnologies/Receipt_Image_Analyzer" |
|
model = AutoModelForSequenceClassification.from_pretrained(model_name) |
|
tokenizer = AutoTokenizer.from_pretrained(model_name) |
|
|
|
|
|
def ocr_extract_text(image): |
|
|
|
gray_image = image.convert("L") |
|
|
|
extracted_text = pytesseract.image_to_string(gray_image) |
|
return extracted_text |
|
|
|
|
|
def analyze_receipt_image(receipt_image): |
|
|
|
receipt_text = ocr_extract_text(receipt_image) |
|
if not receipt_text.strip(): |
|
return {"error": "No text detected in the image."} |
|
|
|
|
|
inputs = tokenizer(receipt_text, return_tensors="pt", truncation=True, padding=True) |
|
outputs = model(**inputs) |
|
logits = outputs.logits |
|
predicted_class = logits.argmax(-1).item() |
|
|
|
|
|
return { |
|
"extracted_text": receipt_text, |
|
"predicted_class": predicted_class |
|
} |
|
|
|
|
|
interface = gr.Interface( |
|
fn=analyze_receipt_image, |
|
inputs=gr.Image(type="pil"), |
|
outputs="json", |
|
title="Receipt Image Analyzer", |
|
description="Upload an image of a receipt. The app extracts text and analyzes it using a fine-tuned LLM model.", |
|
) |
|
|
|
|
|
interface.launch() |
|
|