File size: 8,090 Bytes
295b4b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68b4cc3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
295b4b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
"""
Receipt Scanner
Upload a receipt photo and extract structured data (items, prices, totals).
"""

import gradio as gr
from huggingface_hub import InferenceClient
from PIL import Image
import base64
from io import BytesIO
import json
import pandas as pd
import re
import os
import sys

sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
from shared.components import create_method_panel, create_premium_hero

# Initialize Hugging Face Inference Client
client = InferenceClient()

EXTRACTION_PROMPT = """Analyze this receipt image and extract ALL information in a structured format.

Extract:
1. **Merchant/Store Name**
2. **Date** (in YYYY-MM-DD format if possible)
3. **Time** (if visible)
4. **Items** - List each item with its price
5. **Subtotal** (if shown)
6. **Tax** (if shown)
7. **Total Amount**
8. **Payment Method** (if visible)

Format your response EXACTLY as JSON:
```json
{
  "merchant": "Store Name",
  "date": "YYYY-MM-DD",
  "time": "HH:MM",
  "items": [
    {"name": "Item 1", "price": 0.00},
    {"name": "Item 2", "price": 0.00}
  ],
  "subtotal": 0.00,
  "tax": 0.00,
  "total": 0.00,
  "payment_method": "Card/Cash/etc"
}
```

Be precise with numbers. If something is unclear, use null."""


def extract_json_from_text(text):
    """Extract JSON from markdown code blocks or raw text."""
    # Try to find JSON in code blocks first
    json_match = re.search(r'```(?:json)?\s*(\{.*?\})\s*```', text, re.DOTALL)
    if json_match:
        return json_match.group(1)

    # Try to find raw JSON
    json_match = re.search(r'\{.*\}', text, re.DOTALL)
    if json_match:
        return json_match.group(0)

    return None


def scan_receipt(image):
    """Extract structured data from receipt using VLM."""
    if image is None:
        return "❌ Please upload a receipt first!", "", ""

    try:
        if not os.getenv("HF_TOKEN"):
            data = {
                "merchant": "Manual review required",
                "date": None,
                "time": None,
                "items": [],
                "subtotal": None,
                "tax": None,
                "total": None,
                "payment_method": None,
                "note": "HF_TOKEN is not configured for hosted vision inference. The image was received, but field extraction needs a Space secret or manual entry.",
            }
            summary = """# 🧾 Receipt Ready For Review

The image uploaded correctly, but hosted vision inference is not configured on this Space.

To enable automatic extraction, add a Hugging Face token as a Space secret named `HF_TOKEN`.

Until then, this Space still documents the expected schema and downstream JSON shape.
"""
            return summary, pd.DataFrame(columns=["name", "price"]), json.dumps(data, indent=2)

        # Convert PIL Image to base64
        buffered = BytesIO()
        if isinstance(image, str):
            image = Image.open(image)
        image.save(buffered, format="PNG")
        img_str = base64.b64encode(buffered.getvalue()).decode()

        # Use Florence-2 or Qwen2-VL for OCR + understanding
        response = client.chat_completion(
            model="Qwen/Qwen2-VL-7B-Instruct",
            messages=[
                {
                    "role": "user",
                    "content": [
                        {"type": "image_url", "image_url": {"url": f"data:image/png;base64,{img_str}"}},
                        {"type": "text", "text": EXTRACTION_PROMPT}
                    ]
                }
            ],
            max_tokens=1000,
            temperature=0.1  # Low temperature for accuracy
        )

        raw_response = response.choices[0].message.content

        # Extract JSON from response
        json_str = extract_json_from_text(raw_response)
        if not json_str:
            return f"⚠️ Could not parse receipt data.\n\nRaw response:\n{raw_response}", "", ""

        # Parse JSON
        data = json.loads(json_str)

        # Create formatted summary
        summary = f"""# 🧾 Receipt Analysis

**Merchant**: {data.get('merchant', 'N/A')}
**Date**: {data.get('date', 'N/A')}
**Time**: {data.get('time', 'N/A')}

---

## πŸ“¦ Items

"""
        # Add items table
        if data.get('items'):
            for item in data['items']:
                name = item.get('name', 'Unknown')
                price = item.get('price', 0.0)
                summary += f"- **{name}**: ${price:.2f}\n"
        else:
            summary += "*No items found*\n"

        summary += f"""
---

## πŸ’° Totals

- **Subtotal**: ${data.get('subtotal', 0.0):.2f}
- **Tax**: ${data.get('tax', 0.0):.2f}
- **Total**: ${data.get('total', 0.0):.2f}

**Payment**: {data.get('payment_method', 'N/A')}
"""

        # Create DataFrame for table view
        if data.get('items'):
            df = pd.DataFrame(data['items'])
            df['price'] = df['price'].apply(lambda x: f"${x:.2f}")
        else:
            df = pd.DataFrame(columns=['name', 'price'])

        # Format JSON for download
        json_output = json.dumps(data, indent=2)

        return summary, df, json_output

    except json.JSONDecodeError as e:
        return f"❌ Error parsing JSON: {str(e)}\n\nRaw response:\n{raw_response}", "", ""
    except Exception as e:
        return f"❌ Error scanning receipt: {str(e)}", "", ""


# Gradio Interface
with gr.Blocks(theme=gr.themes.Soft()) as demo:
    create_premium_hero(
        "Receipt Scanner",
        "Extract merchant, items, totals, and payment details from receipt images with a vision-language model workflow.",
        "🧾",
        badge="Document Vision",
        highlights=["Vision-language extraction", "Structured JSON", "CSV export"],
    )
    create_method_panel({
        "Technique": "Image-to-structured-data extraction with schema parsing and tabular validation.",
        "What it proves": "You can turn multimodal model output into reliable downstream data products.",
        "HF capability": "Designed for Hub-hosted VLM inference and lightweight Space deployment.",
    })

    with gr.Row():
        with gr.Column(scale=1):
            image_input = gr.Image(
                label="πŸ“Έ Upload Receipt Photo",
                type="pil",
                height=400
            )

            scan_btn = gr.Button("πŸ” Scan Receipt", variant="primary", size="lg")

            gr.Markdown("""
            ### πŸ’‘ Tips for Best Results:
            - Good lighting, minimal shadows
            - Receipt should be flat and clear
            - Include the entire receipt
            - High contrast works best
            """)

        with gr.Column(scale=1):
            summary_output = gr.Markdown(label="πŸ“Š Summary")

    with gr.Row():
        with gr.Column():
            table_output = gr.Dataframe(
                label="πŸ“‹ Items Table",
                headers=["name", "price"],
                interactive=False
            )

        with gr.Column():
            json_output = gr.Textbox(
                label="πŸ“„ JSON Data (copy to download)",
                lines=15,
                max_lines=20
            )

    # Event handler
    scan_btn.click(
        fn=scan_receipt,
        inputs=[image_input],
        outputs=[summary_output, table_output, json_output],
        api_name="scan"
    )

    gr.Markdown("""
    ---
    ### πŸŽ“ What This App Does:

    1. **OCR + Understanding**: Doesn't just read text, understands structure
    2. **Data Extraction**: Identifies items, prices, totals, dates
    3. **JSON Export**: Download structured data for expense tracking
    4. **Table View**: See items in an organized format

    ### πŸ“Š Use Cases:

    - **Expense Tracking**: Digitize receipts for accounting
    - **Budget Apps**: Auto-import spending data
    - **Tax Records**: Organize business expenses
    - **Reimbursements**: Submit itemized claims
    - **Personal Finance**: Track spending categories

    *Note: Accuracy depends on receipt clarity and format. Complex layouts may require manual verification.*
    """)

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