File size: 1,435 Bytes
235c3b6
 
7e5af81
 
235c3b6
 
 
 
 
 
7e5af81
 
 
 
 
 
 
 
 
235c3b6
 
 
7e5af81
 
 
 
 
 
 
235c3b6
 
 
 
7e5af81
 
235c3b6
7e5af81
235c3b6
 
 
 
1
2
3
4
5
6
7
8
9
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
import gradio as gr
from transformers import AutoModelForSequenceClassification, AutoTokenizer
from PIL import Image
import pytesseract  # Install using `pip install pytesseract` and ensure Tesseract is installed

# Load your fine-tuned model and tokenizer
model_name = "quadranttechnologies/Receipt_Image_Analyzer"
model = AutoModelForSequenceClassification.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)

# Define a function to preprocess the image and predict
def analyze_receipt(image):
    # Perform OCR to extract text from the image
    extracted_text = pytesseract.image_to_string(image)
    
    # Tokenize the extracted text
    inputs = tokenizer(extracted_text, return_tensors="pt", truncation=True, padding=True)
    
    # Get model predictions
    outputs = model(**inputs)
    logits = outputs.logits
    predicted_class = logits.argmax(-1).item()
    
    # Optionally return extracted text and prediction as JSON
    result = {
        "extracted_text": extracted_text,
        "predicted_class": predicted_class
    }
    return result

# Create a Gradio interface
interface = gr.Interface(
    fn=analyze_receipt,
    inputs=gr.inputs.Image(type="pil"),  # Accept image input
    outputs="json",  # Return JSON output
    title="Receipt Image Analyzer",
    description="Upload a receipt image to analyze and classify its contents.",
)

# Launch the Gradio app
interface.launch()