Dileep7729 commited on
Commit
235c3b6
·
verified ·
1 Parent(s): 31d0a3f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -40
app.py CHANGED
@@ -1,40 +1,27 @@
1
- import gradio as gr
2
- from transformers import LayoutLMv3Processor, LayoutLMv3ForTokenClassification
3
- from PIL import Image
4
- import pytesseract
5
-
6
- # Load model and processor
7
- model = LayoutLMv3ForTokenClassification.from_pretrained("./model")
8
- processor = LayoutLMv3Processor.from_pretrained("./model")
9
-
10
- # Define label mapping
11
- id2label = {0: "company", 1: "date", 2: "address", 3: "total", 4: "other"}
12
-
13
- def predict_receipt(image):
14
- ocr_data = pytesseract.image_to_data(image, output_type=pytesseract.Output.DICT)
15
- texts = ocr_data["text"]
16
- boxes = [
17
- [
18
- ocr_data["left"][i],
19
- ocr_data["top"][i],
20
- ocr_data["left"][i] + ocr_data["width"][i],
21
- ocr_data["top"][i] + ocr_data["height"][i],
22
- ]
23
- for i in range(len(ocr_data["text"]))
24
- ]
25
- encoding = processor(image, text=texts, boxes=boxes, return_tensors="pt", truncation=True, padding="max_length")
26
- outputs = model(**{k: v for k, v in encoding.items()})
27
- predictions = outputs.logits.argmax(-1).squeeze().tolist()
28
- labeled_output = {id2label[pred]: texts[i] for i, pred in enumerate(predictions) if pred != 4}
29
- return labeled_output
30
-
31
- interface = gr.Interface(
32
- fn=predict_receipt,
33
- inputs=gr.Image(type="pil"),
34
- outputs="json",
35
- title="Receipt Analyzer",
36
- description="Upload a receipt image to extract key information."
37
- )
38
-
39
- if __name__ == "__main__":
40
- interface.launch()
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer
3
+
4
+ # Load your fine-tuned model and tokenizer
5
+ model_name = "quadranttechnologies/Receipt_Image_Analyzer"
6
+ model = AutoModelForSequenceClassification.from_pretrained(model_name)
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+
9
+ # Define a prediction function
10
+ def analyze_receipt(receipt_text):
11
+ inputs = tokenizer(receipt_text, return_tensors="pt", truncation=True, padding=True)
12
+ outputs = model(**inputs)
13
+ logits = outputs.logits
14
+ predicted_class = logits.argmax(-1).item()
15
+ return f"Predicted Class: {predicted_class}"
16
+
17
+ # Create a Gradio interface
18
+ interface = gr.Interface(
19
+ fn=analyze_receipt,
20
+ inputs="text",
21
+ outputs="text",
22
+ title="Receipt Image Analyzer",
23
+ description="Analyze receipts for relevant information using a fine-tuned LLM model.",
24
+ )
25
+
26
+ # Launch the Gradio app
27
+ interface.launch()