Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,33 +4,39 @@ import openai
|
|
| 4 |
import gradio as gr
|
| 5 |
import os
|
| 6 |
|
| 7 |
-
# Load
|
| 8 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 9 |
|
| 10 |
-
def
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
|
| 22 |
-
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
|
|
|
|
|
|
|
|
|
|
| 27 |
ui = gr.Interface(
|
| 28 |
-
fn=
|
| 29 |
-
inputs=gr.
|
| 30 |
outputs="text",
|
| 31 |
-
title="Receipt OCR & Data Extractor",
|
| 32 |
-
description="Upload
|
| 33 |
)
|
| 34 |
|
| 35 |
if __name__ == "__main__":
|
| 36 |
ui.launch()
|
|
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
import os
|
| 6 |
|
| 7 |
+
# Load OpenAI API key
|
| 8 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 9 |
|
| 10 |
+
def extract_multiple_receipts(images):
|
| 11 |
+
results = []
|
| 12 |
+
for image in images:
|
| 13 |
+
try:
|
| 14 |
+
raw_text = pytesseract.image_to_string(image)
|
| 15 |
|
| 16 |
+
response = openai.ChatCompletion.create(
|
| 17 |
+
model="gpt-4",
|
| 18 |
+
messages=[
|
| 19 |
+
{"role": "system", "content": "You extract structured data from receipts in JSON format with keys like 'Bank Name', 'Date', 'Items', 'Total', etc."},
|
| 20 |
+
{"role": "user", "content": f"Convert the following receipt text to JSON:\n\n{raw_text}"}
|
| 21 |
+
]
|
| 22 |
+
)
|
| 23 |
|
| 24 |
+
results.append(response['choices'][0]['message']['content'])
|
| 25 |
|
| 26 |
+
except Exception as e:
|
| 27 |
+
results.append(f"❌ Error processing image: {str(e)}")
|
| 28 |
|
| 29 |
+
return "\n\n---\n\n".join(results)
|
| 30 |
+
|
| 31 |
+
# Gradio UI for batch upload
|
| 32 |
ui = gr.Interface(
|
| 33 |
+
fn=extract_multiple_receipts,
|
| 34 |
+
inputs=gr.File(file_types=["image"], label="Upload Receipt Images", file_count="multiple"),
|
| 35 |
outputs="text",
|
| 36 |
+
title="Receipt OCR & Data Extractor (Bulk Upload)",
|
| 37 |
+
description="Upload up to 5 receipt images to extract structured JSON data."
|
| 38 |
)
|
| 39 |
|
| 40 |
if __name__ == "__main__":
|
| 41 |
ui.launch()
|
| 42 |
+
|