Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -131,86 +131,114 @@ def fallback_supplier(text):
|
|
| 131 |
return None
|
| 132 |
|
| 133 |
def get_extraction_prompt(model_choice, txt):
|
| 134 |
-
return
|
| 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 |
def ensure_total_due(invoice_header):
|
| 216 |
if invoice_header.get("total_due") in [None, ""]:
|
|
|
|
| 131 |
return None
|
| 132 |
|
| 133 |
def get_extraction_prompt(model_choice, txt):
|
| 134 |
+
return f"""
|
| 135 |
+
You are an expert invoice parser.
|
| 136 |
+
Extract data according to the visible table structure and column headers in the invoice.
|
| 137 |
+
For every line item, only extract fields that correspond to the table columns for that row (do not include header/shipment fields in line items).
|
| 138 |
+
Merge all multi-line content within a single cell into that field (especially for the "description" and "notes").
|
| 139 |
+
Shipment/invoice-level fields such as CAR NUMBER, SHIPPING POINT, SHIPMENT NUMBER, CURRENCY, etc., must go ONLY into the "invoice_header", not as line item fields.
|
| 140 |
+
|
| 141 |
+
**For example, if the source document shows Payment Terms: 1% 10 ADI, ADF, NET 20 DAYS**
|
| 142 |
+
**Do not alter the sequence, combine, or separate phrases in any way.**
|
| 143 |
+
|
| 144 |
+
Discount Extraction:
|
| 145 |
+
Extract only fields explicitly labeled "Discount", "Special Discount", "Rebate", or "Early Payment Discount".
|
| 146 |
+
Do not extract "deduct" amounts as a discount if they are part of payment terms or incentives.
|
| 147 |
+
Exclude any ambiguous phrases from the discount field.
|
| 148 |
+
|
| 149 |
+
Use this schema:
|
| 150 |
+
{{
|
| 151 |
+
// ============================
|
| 152 |
+
// Invoice Header Details
|
| 153 |
+
// ============================
|
| 154 |
+
"invoice_header": {{
|
| 155 |
+
"id_number": "string or null",
|
| 156 |
+
"document_type": "string or null",
|
| 157 |
+
"invoice_number": "string or null",
|
| 158 |
+
"invoice_date": "string or null",
|
| 159 |
+
"due_date": "string or null",
|
| 160 |
+
"invoice_status": "string or null",
|
| 161 |
+
"mobile_no": "string or null",
|
| 162 |
+
"landline_no": "string or null",
|
| 163 |
+
"fax": "string or null",
|
| 164 |
+
"e_mail_id": "string or null",
|
| 165 |
+
"taxable_amount": "string or null",
|
| 166 |
+
"tax_amount": "string or null",
|
| 167 |
+
"tax_rate": "string or null",
|
| 168 |
+
"total_before_tax": "string or null",
|
| 169 |
+
"shipping_charges": "string or null",
|
| 170 |
+
"discount": "string or null",
|
| 171 |
+
"round_off": "string or null",
|
| 172 |
+
"grand_total": "string or null",
|
| 173 |
+
"order_number": "string or null",
|
| 174 |
+
"order_date": "string or null",
|
| 175 |
+
"purchase_order_number": "string or null",
|
| 176 |
+
"purchase_order_date": "string or null",
|
| 177 |
+
"customer_order_number": "string or null",
|
| 178 |
+
"our_order_number": "string or null",
|
| 179 |
+
"sales_order_number": "string or null",
|
| 180 |
+
"supplier_name": "string or null",
|
| 181 |
+
"supplier_address": "string or null",
|
| 182 |
+
"supplier_phone": "string or null",
|
| 183 |
+
"supplier_email": "string or null",
|
| 184 |
+
"supplier_tax_id": "string or null",
|
| 185 |
+
"customer_name": "string or null",
|
| 186 |
+
"customer_no": "string or null",
|
| 187 |
+
"customer_address": "string or null",
|
| 188 |
+
"customer_phone": "string or null",
|
| 189 |
+
"customer_email": "string or null",
|
| 190 |
+
"customer_tax_id": "string or null",
|
| 191 |
+
"bill_to_name": "string or null",
|
| 192 |
+
"bill_to_address": "string or null",
|
| 193 |
+
"ship_to_address": "string or null",
|
| 194 |
+
"ship_to_name": "string or null",
|
| 195 |
+
"remit_to": "string or null",
|
| 196 |
+
"pay_to": "string or null",
|
| 197 |
+
"shipment_number": "string or null",
|
| 198 |
+
"shipping_point": "string or null",
|
| 199 |
+
"shipped_via": "string or null",
|
| 200 |
+
"transportation_mode": "string or null",
|
| 201 |
+
"delivery_doc_no": "string or null",
|
| 202 |
+
"delivery_doc_date": "string or null",
|
| 203 |
+
"payment_terms": "string or null",
|
| 204 |
+
"payment_method": "string or null",
|
| 205 |
+
"payment_reference": "string or null",
|
| 206 |
+
"bank_account_number": "string or null",
|
| 207 |
+
"iban": "string or null",
|
| 208 |
+
"bank_account_name": "string or null",
|
| 209 |
+
"bank_name": "string or null",
|
| 210 |
+
"bank_branch": "string or null",
|
| 211 |
+
"notes_remarks": "string or null",
|
| 212 |
+
"additional_info": "string or null",
|
| 213 |
+
"delivery_terms": "string or null",
|
| 214 |
+
"aca_admin_fee": "string or null"
|
| 215 |
+
}},
|
| 216 |
+
// ============================
|
| 217 |
+
// Line Items Details
|
| 218 |
+
// ============================
|
| 219 |
+
"line_items": [
|
| 220 |
+
{{
|
| 221 |
+
"line_no": "string or null",
|
| 222 |
+
"item_no": "string or null",
|
| 223 |
+
"quantity": "string or null",
|
| 224 |
+
"uom": "string or null",
|
| 225 |
+
"units": "string or null",
|
| 226 |
+
"rate": "string or null",
|
| 227 |
+
"rate_per": "string or null",
|
| 228 |
+
"price": "string or null",
|
| 229 |
+
"line_amount": "string or null",
|
| 230 |
+
"description": "string or null",
|
| 231 |
+
"footage": "string or null",
|
| 232 |
+
"notes": "string or null"
|
| 233 |
+
}}
|
| 234 |
+
]
|
| 235 |
+
}}
|
| 236 |
+
If a field is missing for a line item or header, use null.
|
| 237 |
+
Do not invent fields. Do not add any header or shipment data to any line item. Return ONLY the JSON object, no explanation.
|
| 238 |
+
Invoice Text:
|
| 239 |
+
{txt}
|
| 240 |
+
"""
|
| 241 |
+
|
| 242 |
|
| 243 |
def ensure_total_due(invoice_header):
|
| 244 |
if invoice_header.get("total_due") in [None, ""]:
|