File size: 1,328 Bytes
70c99a9
 
 
 
 
 
89263fd
70c99a9
 
63fe004
89263fd
 
 
 
 
70c99a9
89263fd
 
 
 
 
 
 
70c99a9
89263fd
70c99a9
89263fd
 
70c99a9
89263fd
 
 
70c99a9
89263fd
 
70c99a9
89263fd
 
70c99a9
 
 
 
89263fd
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
43
44
from PIL import Image
import pytesseract
import openai
import gradio as gr
import os

# Load OpenAI API key
openai.api_key = os.getenv("OPENAI_API_KEY")


def extract_multiple_receipts(images):
    results = []
    for image in images:
        try:
            raw_text = pytesseract.image_to_string(image)

            response = openai.ChatCompletion.create(
                model="gpt-4",
                messages=[
                    {"role": "system", "content": "You extract structured data from receipts in JSON format with keys like 'Bank Name', 'Date', 'Items', 'Total', etc."},
                    {"role": "user", "content": f"Convert the following receipt text to JSON:\n\n{raw_text}"}
                ]
            )

            results.append(response['choices'][0]['message']['content'])

        except Exception as e:
            results.append(f"❌ Error processing image: {str(e)}")

    return "\n\n---\n\n".join(results)

# Gradio UI for batch upload
ui = gr.Interface(
    fn=extract_multiple_receipts,
    inputs=gr.File(file_types=["image"], label="Upload Receipt Images", file_count="multiple"),
    outputs="text",
    title="Receipt OCR & Data Extractor (Bulk Upload)",
    description="Upload up to 5 receipt images to extract structured JSON data."
)

if __name__ == "__main__":
    ui.launch()