Nasma commited on
Commit
89263fd
·
verified ·
1 Parent(s): 70c99a9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -18
app.py CHANGED
@@ -4,33 +4,39 @@ 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()
 
 
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
+