Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from PIL import Image
|
2 |
+
import pytesseract
|
3 |
+
import openai
|
4 |
+
import gradio as gr
|
5 |
+
import os
|
6 |
+
|
7 |
+
# Load the API key from .env or environment variable
|
8 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
9 |
+
|
10 |
+
def extract_receipt_data(image):
|
11 |
+
try:
|
12 |
+
raw_text = pytesseract.image_to_string(image)
|
13 |
+
|
14 |
+
response = openai.ChatCompletion.create(
|
15 |
+
model="gpt-4",
|
16 |
+
messages=[
|
17 |
+
{"role": "system", "content": "You extract structured data from receipts in JSON format with keys like 'Bank Name', 'Date', 'Items', 'Total', etc."},
|
18 |
+
{"role": "user", "content": f"Convert the following receipt text to JSON:\n\n{raw_text}"}
|
19 |
+
]
|
20 |
+
)
|
21 |
+
|
22 |
+
return response['choices'][0]['message']['content']
|
23 |
+
|
24 |
+
except Exception as e:
|
25 |
+
return f"❌ Error: {str(e)}"
|
26 |
+
|
27 |
+
ui = gr.Interface(
|
28 |
+
fn=extract_receipt_data,
|
29 |
+
inputs=gr.Image(type="pil"),
|
30 |
+
outputs="text",
|
31 |
+
title="Receipt OCR & Data Extractor",
|
32 |
+
description="Upload a receipt image and get structured JSON data extracted."
|
33 |
+
)
|
34 |
+
|
35 |
+
if __name__ == "__main__":
|
36 |
+
ui.launch()
|