Spaces:
Runtime error
Runtime error
| 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() | |