jayanth-2005 commited on
Commit
5d33904
·
verified ·
1 Parent(s): ddad13a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -44
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
- prompt = f"""
98
- You are a financial document extraction engine.
99
-
100
- If this document contains:
101
- - payment confirmation
102
- - subscription charge
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
- If not purchase-related, return [].
128
 
129
- Document:
130
- {text}
131
- """
 
 
 
 
 
 
 
132
 
133
- try:
134
- completion = client.chat.completions.create(
135
- model="llama-3.1-8b-instant",
136
- messages=[{"role": "user", "content": prompt}],
137
- temperature=0
138
- )
 
 
 
 
 
 
139
 
140
- result = completion.choices[0].message.content.strip()
141
- result = result.replace("```json", "").replace("```", "")
142
- return json.loads(result)
 
 
 
 
 
 
 
 
 
143
 
144
- except Exception as e:
145
- print("AI error:", e)
146
- return []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
  # ---------------------------------------------------------