Dileep7729 commited on
Commit
fcaf9a0
·
verified ·
1 Parent(s): 7e5af81

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -19
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 using `pip install pytesseract` and ensure Tesseract is installed
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
- # Define a function to preprocess the image and predict
12
- def analyze_receipt(image):
13
- # Perform OCR to extract text from the image
14
- extracted_text = pytesseract.image_to_string(image)
15
-
16
- # Tokenize the extracted text
17
- inputs = tokenizer(extracted_text, return_tensors="pt", truncation=True, padding=True)
18
-
19
- # Get model predictions
 
 
 
 
 
 
 
 
20
  outputs = model(**inputs)
21
  logits = outputs.logits
22
  predicted_class = logits.argmax(-1).item()
23
-
24
- # Optionally return extracted text and prediction as JSON
25
- result = {
26
- "extracted_text": extracted_text,
27
  "predicted_class": predicted_class
28
  }
29
- return result
30
 
31
  # Create a Gradio interface
32
  interface = gr.Interface(
33
- fn=analyze_receipt,
34
- inputs=gr.inputs.Image(type="pil"), # Accept image input
35
- outputs="json", # Return JSON output
36
  title="Receipt Image Analyzer",
37
- description="Upload a receipt image to analyze and classify its contents.",
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