Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -94,56 +94,77 @@ def ai_extract_invoice(text):
|
|
| 94 |
|
| 95 |
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
- invoice
|
| 104 |
-
- receipt
|
| 105 |
-
- order confirmation
|
| 106 |
-
- total amount charged
|
| 107 |
-
|
| 108 |
-
Extract structured JSON array.
|
| 109 |
-
|
| 110 |
-
Format:
|
| 111 |
-
|
| 112 |
-
[
|
| 113 |
-
{{
|
| 114 |
-
"document_type": "",
|
| 115 |
-
"invoice_or_order_number": "",
|
| 116 |
-
"invoice_date": "",
|
| 117 |
-
"vendor_name": "",
|
| 118 |
-
"vendor_gstin": null,
|
| 119 |
-
"taxable_amount": null,
|
| 120 |
-
"cgst": null,
|
| 121 |
-
"sgst": null,
|
| 122 |
-
"igst": null,
|
| 123 |
-
"total_amount": null
|
| 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 |
# ---------------------------------------------------------
|
|
|
|
| 94 |
|
| 95 |
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
| 96 |
|
| 97 |
+
# Split document into logical chunks
|
| 98 |
+
separators = [
|
| 99 |
+
"TAX INVOICE",
|
| 100 |
+
"Invoice number:",
|
| 101 |
+
"Invoice Number:",
|
| 102 |
+
"Order number"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
]
|
| 104 |
|
| 105 |
+
chunks = [text]
|
| 106 |
|
| 107 |
+
for sep in separators:
|
| 108 |
+
temp = []
|
| 109 |
+
for chunk in chunks:
|
| 110 |
+
parts = chunk.split(sep)
|
| 111 |
+
for i, part in enumerate(parts):
|
| 112 |
+
if i == 0:
|
| 113 |
+
continue
|
| 114 |
+
temp.append(sep + part)
|
| 115 |
+
if temp:
|
| 116 |
+
chunks = temp
|
| 117 |
|
| 118 |
+
invoices = []
|
| 119 |
+
|
| 120 |
+
for chunk in chunks:
|
| 121 |
+
|
| 122 |
+
if len(chunk.strip()) < 200:
|
| 123 |
+
continue
|
| 124 |
+
|
| 125 |
+
prompt = f"""
|
| 126 |
+
Extract financial transaction details.
|
| 127 |
+
|
| 128 |
+
If this text contains invoice / receipt / order / payment,
|
| 129 |
+
return JSON:
|
| 130 |
|
| 131 |
+
{{
|
| 132 |
+
"document_type": "",
|
| 133 |
+
"invoice_or_order_number": "",
|
| 134 |
+
"invoice_date": "",
|
| 135 |
+
"vendor_name": "",
|
| 136 |
+
"vendor_gstin": null,
|
| 137 |
+
"taxable_amount": null,
|
| 138 |
+
"cgst": null,
|
| 139 |
+
"sgst": null,
|
| 140 |
+
"igst": null,
|
| 141 |
+
"total_amount": null
|
| 142 |
+
}}
|
| 143 |
|
| 144 |
+
If not financial document, return null.
|
| 145 |
+
|
| 146 |
+
Text:
|
| 147 |
+
{chunk}
|
| 148 |
+
"""
|
| 149 |
+
|
| 150 |
+
try:
|
| 151 |
+
completion = client.chat.completions.create(
|
| 152 |
+
model="llama-3.1-8b-instant",
|
| 153 |
+
messages=[{"role": "user", "content": prompt}],
|
| 154 |
+
temperature=0
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
+
result = completion.choices[0].message.content.strip()
|
| 158 |
+
result = result.replace("```json", "").replace("```", "")
|
| 159 |
+
data = json.loads(result)
|
| 160 |
+
|
| 161 |
+
if data:
|
| 162 |
+
invoices.append(data)
|
| 163 |
+
|
| 164 |
+
except:
|
| 165 |
+
continue
|
| 166 |
+
|
| 167 |
+
return invoices
|
| 168 |
|
| 169 |
|
| 170 |
# ---------------------------------------------------------
|