File size: 10,011 Bytes
c45087c
 
 
8b40292
 
98a63cb
c45087c
 
 
 
 
 
 
8b40292
000aa6a
 
8b40292
 
c45087c
 
 
 
8b40292
c45087c
 
 
 
 
 
 
 
 
 
 
 
8b40292
 
 
 
 
 
 
 
 
000aa6a
98a63cb
c45087c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
000aa6a
 
 
 
 
c45087c
 
8b40292
 
c45087c
8b40292
 
 
 
c45087c
8b40292
c45087c
8b40292
 
 
c45087c
8b40292
 
c45087c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98a63cb
8b40292
8871311
 
98a63cb
 
 
 
 
c45087c
98a63cb
 
8b40292
98a63cb
 
 
 
 
 
30724cb
8871311
98a63cb
8b40292
98a63cb
c45087c
8871311
 
 
 
 
 
98a63cb
c45087c
 
8b40292
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2a0adb0
 
 
 
 
 
 
 
8b40292
2a0adb0
 
 
 
 
8b40292
2a0adb0
8b40292
 
 
2a0adb0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b40292
 
c45087c
 
5221130
c45087c
 
 
 
 
 
 
 
 
 
 
 
 
5221130
 
 
 
c45087c
5221130
c45087c
5221130
 
 
 
 
 
fe4033f
5221130
fe4033f
5221130
c45087c
 
 
 
 
 
 
 
 
 
c08cb44
c45087c
 
8b40292
c45087c
 
 
 
 
 
 
 
 
 
8b40292
c45087c
 
 
 
98a63cb
c45087c
8b40292
 
 
98a63cb
8b40292
98a63cb
 
8b40292
98a63cb
 
 
 
 
8b40292
 
 
 
98a63cb
8b40292
 
98a63cb
8b40292
98a63cb
8b40292
 
98a63cb
8b40292
98a63cb
 
 
 
 
 
c45087c
 
 
 
 
 
 
 
 
 
 
 
 
 
76bb564
 
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import os
import json
import io
import time
import gc
import numpy as np
import pandas as pd
from flask import Flask, request, send_file, jsonify
from flask_cors import CORS
import google.generativeai as genai
from groq import Groq
from PyPDF2 import PdfReader
from dotenv import load_dotenv

# OCR Imports (EasyOCR)
import easyocr
from pdf2image import convert_from_bytes, pdfinfo_from_bytes


app = Flask(__name__)
CORS(app, resources={r"/*": {"origins": "*"}})

# Load .env locally
basedir = os.path.abspath(os.path.dirname(__file__))
load_dotenv(os.path.join(basedir, ".env"))

# API Keys
GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")

print(f"DEBUG - Is Groq Key loaded? {'YES' if GROQ_API_KEY else 'NO'}")

genai.configure(api_key=GEMINI_API_KEY)
groq_client = Groq(api_key=GROQ_API_KEY)

# EasyOCR reader (load once)
easyocr_reader = easyocr.Reader(["en"], gpu=False)

# OCR settings
OCR_DPI = 200

# Groq batching settings
BATCH_SIZE = 3
MAX_RETRIES = 5


SYSTEM_PROMPT = """
You are a data extraction assistant. Extract the payroll data from the provided document or text.
Return ONLY a valid JSON object with a single key called "data". The value of "data" must be an array of objects.
Do not include markdown formatting, code blocks, or explanations.
Each object in the array must have exactly these keys: "SSN", "Participant", "Amount", "Contract", "Carrier Name".
If a specific field is missing for a participant, use null.

Example format:
{
  "data": [
    {"SSN": "123456789", "Participant": "John Doe", "Amount": "150.00", "Contract": null, "Carrier Name": "MG Trust Co"}
  ]
}
"""


@app.route("/")
def home():
    return jsonify({"status": "API running"}), 200


def generate_excel_buffer(json_data):
    """Convert JSON extracted data into an Excel file buffer."""
    records = json_data.get("data", [])
    df = pd.DataFrame(records)

    expected_columns = ["SSN", "Participant", "Amount", "Contract", "Carrier Name"]
    for col in expected_columns:
        if col not in df.columns:
            df[col] = None

    df = df[expected_columns]

    output = io.BytesIO()
    with pd.ExcelWriter(output, engine="openpyxl") as writer:
        df.to_excel(writer, index=False, sheet_name="Payroll Data")

    output.seek(0)
    return output


def extract_text_with_pypdf2(pdf_file):
    """Extract text from PDF using PyPDF2."""
    pdf_reader = PdfReader(pdf_file)
    extracted_text = ""

    for page in pdf_reader.pages:
        text = page.extract_text()
        if text:
            extracted_text += text + "\n"

    return extracted_text.strip()


def extract_text_with_easyocr_pagewise(pdf_bytes):
    """Extract OCR text page by page using EasyOCR (memory safe)."""
    info = pdfinfo_from_bytes(pdf_bytes)
    total_pages = info["Pages"]

    pages_text = []

    for i in range(1, total_pages + 1):
        print(f"🔍 OCR Processing Page {i}/{total_pages}")

        images = convert_from_bytes(
            pdf_bytes,
            dpi=OCR_DPI,
            first_page=i,
            last_page=i
        )

        img = images[0]
        img_np = np.array(img)

        result = easyocr_reader.readtext(img_np, detail=0, paragraph=True)
        page_text = "\n".join(result).strip()

        pages_text.append(page_text)

        # Free memory
        del images
        del img
        del img_np
        gc.collect()

    return pages_text


def call_groq_with_retry(text_block):
    """Call Groq with retry support (handles 429)."""
    for attempt in range(MAX_RETRIES):
        try:
            chat_completion = groq_client.chat.completions.create(
                messages=[
                    {"role": "system", "content": SYSTEM_PROMPT.replace("\xa0", " ")},
                    {"role": "user", "content": f"Extract payroll rows ONLY from this text:\n\n{text_block}"},
                ],
                model="llama-3.1-8b-instant",
                response_format={"type": "json_object"},
            )

            response_text = chat_completion.choices[0].message.content
            clean_text = response_text.replace("\xa0", " ").strip()

            return json.loads(clean_text)

        except Exception as e:
            error_msg = str(e)

            if "429" in error_msg:
                wait_time = (attempt + 1) * 3
                print(f"⚠️ Groq Rate Limit Hit (429). Waiting {wait_time}s...")
                time.sleep(wait_time)
                continue

            raise e

    raise Exception("Groq API failed after multiple retries due to rate limit.")


def clean_rows(rows):
    """
    Merge duplicate rows smartly.
    If same Participant+Amount appears again and SSN is missing in old row,
    update it using new row.
    """

    merged = {}
    
    for row in rows:
        ssn = str(row.get("SSN", "")).strip() if row.get("SSN") else ""
        participant = str(row.get("Participant", "")).strip() if row.get("Participant") else ""
        amount = str(row.get("Amount", "")).strip() if row.get("Amount") else ""
        contract = str(row.get("Contract", "")).strip() if row.get("Contract") else ""
        carrier = str(row.get("Carrier Name", "")).strip() if row.get("Carrier Name") else ""

        # skip completely empty rows
        if not participant and not ssn:
            continue

        # Main key: participant + amount (because SSN may appear later)
        key = (participant.lower(), amount)

        if key not in merged:
            merged[key] = {
                "SSN": ssn if ssn else None,
                "Participant": participant if participant else None,
                "Amount": amount if amount else None,
                "Contract": contract if contract else None,
                "Carrier Name": carrier if carrier else None,
            }
        else:
            # merge missing fields
            if not merged[key]["SSN"] and ssn:
                merged[key]["SSN"] = ssn

            if not merged[key]["Contract"] and contract:
                merged[key]["Contract"] = contract

            if not merged[key]["Carrier Name"] and carrier:
                merged[key]["Carrier Name"] = carrier

            # if participant missing (rare case)
            if not merged[key]["Participant"] and participant:
                merged[key]["Participant"] = participant

    return list(merged.values())


@app.route("/download_with_image_model", methods=["POST", "OPTIONS"])
def process_with_gemini():
    """Endpoint for Gemini (Image Model) PDF processing."""
    if "file" not in request.files:
        return jsonify({"error": "No file uploaded"}), 400

    file = request.files["file"]
    if file.filename == "":
        return jsonify({"error": "No selected file"}), 400

    try:
        file_bytes = file.read()
        mime_type = file.mimetype

        model = genai.GenerativeModel("gemini-2.5-flash")

        contents = [
            SYSTEM_PROMPT,
            {"mime_type": mime_type, "data": file_bytes},
        ]

        response = model.generate_content(contents)

        response_text = (
            response.text.strip()
            .removeprefix("```json")
            .removesuffix("```")
            .strip()
        )

        parsed_data = json.loads(response_text)

        excel_buffer = generate_excel_buffer(parsed_data)

        return send_file(
            excel_buffer,
            mimetype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
            as_attachment=True,
            download_name="Extracted_Data_Gemini.xlsx",
        )

    except Exception as e:
        return jsonify({"error": str(e)}), 500
        
@app.route("/download_with_text_model", methods=["POST", "OPTIONS"])
def process_with_groq():
    """Groq endpoint with OCR fallback + batching to avoid 429."""
    if "file" not in request.files:
        return jsonify({"error": "No file uploaded"}), 400

    file = request.files["file"]
    if file.filename == "":
        return jsonify({"error": "No selected file"}), 400

    try:
        pdf_bytes = file.read()

        # Reset pointer for PyPDF2
        file.seek(0)

        extracted_text = extract_text_with_pypdf2(file)

        all_rows = []

        # Case 1: Text PDF
        if len(extracted_text.strip()) > 150:
            print("✅ Text detected using PyPDF2. Sending full document to Groq...")

            parsed_data = call_groq_with_retry(extracted_text)
            all_rows.extend(parsed_data.get("data", []))

        # Case 2: Scanned PDF
        else:
            print("⚠️ No proper text found. Running EasyOCR page-by-page...")

            pages_text = extract_text_with_easyocr_pagewise(pdf_bytes)

            # Batch pages to reduce Groq calls
            for i in range(0, len(pages_text), BATCH_SIZE):
                batch_pages = pages_text[i:i + BATCH_SIZE]
                batch_text = "\n\n--- PAGE BREAK ---\n\n".join(batch_pages).strip()

                if len(batch_text) < 50:
                    continue

                print(f"📤 Sending Pages {i+1} to {min(i+BATCH_SIZE, len(pages_text))} to Groq...")

                parsed_batch = call_groq_with_retry(batch_text)
                all_rows.extend(parsed_batch.get("data", []))

        all_rows = clean_rows(all_rows)

        if not all_rows:
            return jsonify({"error": "No payroll rows extracted from document."}), 400

        final_json = {"data": all_rows}
        excel_buffer = generate_excel_buffer(final_json)

        return send_file(
            excel_buffer,
            mimetype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
            as_attachment=True,
            download_name="Extracted_Data_Groq.xlsx",
        )

    except Exception as e:
        print(f"\n--- CRITICAL BACKEND ERROR --- \n{str(e)}\n------------------------------\n")
        return jsonify({"error": str(e)}), 500


if __name__ == "__main__":
    port = int(os.environ.get("PORT", 7860))
    app.run(host="0.0.0.0", port=port)