Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,14 +4,31 @@ import re
|
|
| 4 |
import base64
|
| 5 |
import gc
|
| 6 |
import tempfile
|
|
|
|
|
|
|
| 7 |
from typing import List, Dict, Optional, Tuple
|
|
|
|
|
|
|
| 8 |
|
| 9 |
from fastapi import FastAPI, File, UploadFile, Form, HTTPException, BackgroundTasks
|
| 10 |
-
from fastapi.
|
| 11 |
-
from fastapi.responses import JSONResponse
|
| 12 |
from starlette.requests import Request
|
| 13 |
import fitz # PyMuPDF
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
# Google Gemini - optional import
|
| 16 |
try:
|
| 17 |
import google.generativeai as genai
|
|
@@ -19,12 +36,14 @@ try:
|
|
| 19 |
GEMINI_AVAILABLE = True
|
| 20 |
except ImportError:
|
| 21 |
GEMINI_AVAILABLE = False
|
| 22 |
-
print("Warning: google-generativeai not installed.Image-based PDFs won't be supported.")
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
app = FastAPI(title="Invoice Splitter API")
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
Request.max_body_size = 200 * 1024 * 1024 # 200MB
|
| 28 |
|
| 29 |
app.add_middleware(
|
| 30 |
CORSMiddleware,
|
|
@@ -34,65 +53,543 @@ app.add_middleware(
|
|
| 34 |
allow_headers=["*"],
|
| 35 |
)
|
| 36 |
|
| 37 |
-
#
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
gemini_model = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
def get_gemini_model():
|
| 46 |
"""Get or create Gemini model instance."""
|
| 47 |
global gemini_model
|
| 48 |
|
| 49 |
if not GEMINI_AVAILABLE:
|
| 50 |
-
print("Gemini SDK not available")
|
| 51 |
return None
|
| 52 |
|
| 53 |
if gemini_model is None:
|
| 54 |
if not GEMINI_API_KEY:
|
| 55 |
-
print("Warning: Gemini API key not found in environment variables.")
|
| 56 |
return None
|
| 57 |
|
| 58 |
try:
|
| 59 |
genai.configure(api_key=GEMINI_API_KEY)
|
| 60 |
-
|
| 61 |
-
|
|
|
|
| 62 |
except Exception as e:
|
| 63 |
-
print(f"Failed to initialize Gemini
|
| 64 |
return None
|
| 65 |
|
| 66 |
return gemini_model
|
| 67 |
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
Tax\s*Invoice\s*No\.?|
|
| 79 |
-
Invoice\s*#|
|
| 80 |
-
Inv\s*#
|
| 81 |
-
)
|
| 82 |
-
[\s:\-]*(?:(?:Order|Ref|No|Dt|Date)\b[\s:\-]*)*
|
| 83 |
-
\s*
|
| 84 |
-
([A-Z0-9][A-Z0-9\-\/]{2,})
|
| 85 |
-
""",
|
| 86 |
-
re. IGNORECASE | re.VERBOSE
|
| 87 |
-
)
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
def is_image_based_pdf(doc: fitz.Document, sample_pages: int = 3) -> Tuple[bool, float]:
|
| 98 |
total_text_length = 0
|
|
@@ -100,16 +597,13 @@ def is_image_based_pdf(doc: fitz.Document, sample_pages: int = 3) -> Tuple[bool,
|
|
| 100 |
|
| 101 |
for i in range(pages_to_check):
|
| 102 |
text = doc.load_page(i).get_text("text") or ""
|
| 103 |
-
total_text_length += len(text.
|
| 104 |
|
| 105 |
avg_text_length = total_text_length / pages_to_check
|
| 106 |
is_image_based = avg_text_length < 50
|
| 107 |
|
| 108 |
-
print(
|
| 109 |
-
|
| 110 |
-
print(
|
| 111 |
-
f" Classification: {'IMAGE-BASED' if is_image_based else 'TEXT-BASED'} PDF")
|
| 112 |
-
|
| 113 |
return is_image_based, avg_text_length
|
| 114 |
|
| 115 |
|
|
@@ -122,158 +616,106 @@ def normalize_text_for_search(s: str) -> str:
|
|
| 122 |
return s
|
| 123 |
|
| 124 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
def try_extract_invoice_from_text(text: str) -> Optional[str]:
|
| 126 |
if not text:
|
| 127 |
return None
|
| 128 |
-
|
| 129 |
text_norm = normalize_text_for_search(text)
|
| 130 |
|
| 131 |
label_match = re.search(
|
| 132 |
-
r"(?:Invoice|Inv|Bill|Doc|Document|Tax\s*Invoice
|
| 133 |
-
text_norm,
|
| 134 |
-
re.IGNORECASE
|
| 135 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
if label_match:
|
| 138 |
start_idx = label_match.end()
|
| 139 |
-
candidate_text = text_norm[start_idx:
|
| 140 |
clean_candidates = re.sub(r"[:\-\(\)\[\]]", " ", candidate_text)
|
| 141 |
words = clean_candidates.split()
|
| 142 |
-
|
| 143 |
for word in words:
|
| 144 |
word = word.strip(".,;")
|
| 145 |
-
if word.
|
| 146 |
continue
|
| 147 |
-
if len(word) > 2 and
|
| 148 |
-
return word
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
top_text = text_norm[:600]
|
| 151 |
m = re.search(r"\b([A-Z0-9][A-Z0-9\-\/]{4,})\b", top_text)
|
| 152 |
if m:
|
| 153 |
-
inv = m.group(1)
|
| 154 |
-
if
|
| 155 |
return inv
|
| 156 |
-
|
| 157 |
-
gm = GST_LIKE_RE.search(text_norm)
|
| 158 |
-
if gm:
|
| 159 |
-
gst_val = gm.group(2) or ""
|
| 160 |
-
gst_val = gst_val.replace(" ", "").strip().upper()
|
| 161 |
-
if len(gst_val) == 15 and re.match(r"^[0-9A-Z]{15}$", gst_val):
|
| 162 |
-
return f"GST:{gst_val}"
|
| 163 |
-
|
| 164 |
return None
|
| 165 |
|
| 166 |
|
| 167 |
-
def extract_invoice_text_based(page:
|
| 168 |
text = page.get_text("text") or ""
|
| 169 |
inv = try_extract_invoice_from_text(text)
|
| 170 |
if inv:
|
| 171 |
return inv
|
| 172 |
-
|
| 173 |
for block in (page.get_text("blocks") or []):
|
| 174 |
block_text = block[4] if len(block) > 4 else ""
|
| 175 |
if block_text:
|
| 176 |
inv = try_extract_invoice_from_text(block_text)
|
| 177 |
if inv:
|
| 178 |
return inv
|
| 179 |
-
|
| 180 |
return None
|
| 181 |
|
| 182 |
|
| 183 |
-
def extract_invoice_gemini(page: fitz.Page) -> Optional[str]:
|
| 184 |
-
model = get_gemini_model()
|
| 185 |
-
if not model:
|
| 186 |
-
print(" Gemini model not available")
|
| 187 |
-
return None
|
| 188 |
-
|
| 189 |
-
try:
|
| 190 |
-
# Reduced from 2x to save memory
|
| 191 |
-
pix = page.get_pixmap(matrix=fitz.Matrix(1.5, 1.5))
|
| 192 |
-
img_bytes = pix.tobytes("png")
|
| 193 |
-
pix = None # Free memory
|
| 194 |
-
|
| 195 |
-
img = Image.open(io.BytesIO(img_bytes))
|
| 196 |
-
|
| 197 |
-
prompt = """
|
| 198 |
-
Extract the invoice number from this image. Look for:
|
| 199 |
-
- Invoice No, Invoice Number, Bill No, Bill Number
|
| 200 |
-
- Any alphanumeric code that appears to be an invoice identifier
|
| 201 |
-
- Purchase Order numbers if no invoice number is found
|
| 202 |
-
|
| 203 |
-
Return ONLY the invoice number/identifier itself, nothing else.
|
| 204 |
-
If no invoice number is found, return "NOT_FOUND".
|
| 205 |
-
"""
|
| 206 |
-
|
| 207 |
-
print(" Calling Google Gemini API...")
|
| 208 |
-
response = model.generate_content([prompt, img])
|
| 209 |
-
|
| 210 |
-
if response and response.text:
|
| 211 |
-
extracted_text = response.text.strip()
|
| 212 |
-
print(f" Gemini response: {extracted_text}")
|
| 213 |
-
|
| 214 |
-
if extracted_text and extracted_text != "NOT_FOUND":
|
| 215 |
-
invoice_no = extracted_text. replace(
|
| 216 |
-
"*", "").replace("#", "").strip()
|
| 217 |
-
if invoice_no and len(invoice_no) > 2:
|
| 218 |
-
print(f" β Gemini found invoice: {invoice_no}")
|
| 219 |
-
img.close()
|
| 220 |
-
return invoice_no
|
| 221 |
-
|
| 222 |
-
ocr_prompt = "Extract all text from this invoice image. Return the complete text content."
|
| 223 |
-
ocr_response = model.generate_content([ocr_prompt, img])
|
| 224 |
-
|
| 225 |
-
if ocr_response and ocr_response.text:
|
| 226 |
-
print(
|
| 227 |
-
f" Gemini extracted {len(ocr_response.text)} chars, trying regex...")
|
| 228 |
-
inv = try_extract_invoice_from_text(ocr_response.text)
|
| 229 |
-
if inv:
|
| 230 |
-
print(f" β Found via regex on Gemini text: {inv}")
|
| 231 |
-
img.close()
|
| 232 |
-
return inv
|
| 233 |
-
|
| 234 |
-
img.close()
|
| 235 |
-
print(" β Gemini: No invoice found")
|
| 236 |
-
return None
|
| 237 |
-
|
| 238 |
-
except Exception as e:
|
| 239 |
-
print(f" β Gemini extraction failed: {e}")
|
| 240 |
-
return None
|
| 241 |
-
|
| 242 |
-
|
| 243 |
def extract_invoice_no_from_page(page: fitz.Page, is_image_pdf: bool) -> Optional[str]:
|
|
|
|
| 244 |
text_result = extract_invoice_text_based(page)
|
| 245 |
if text_result:
|
| 246 |
-
print(f" β Found via text extraction: {text_result}")
|
| 247 |
return text_result
|
| 248 |
-
|
| 249 |
if is_image_pdf:
|
| 250 |
-
|
| 251 |
-
if gemini_result:
|
| 252 |
-
print(f" β Found via Gemini: {gemini_result}")
|
| 253 |
-
return gemini_result
|
| 254 |
-
|
| 255 |
return None
|
| 256 |
|
| 257 |
|
| 258 |
def build_pdf_from_pages(src_doc: fitz.Document, page_indices: List[int]) -> bytes:
|
| 259 |
-
"""Create a new PDF with the given pages (0-based indices)."""
|
| 260 |
out = fitz.open()
|
| 261 |
try:
|
| 262 |
for i in page_indices:
|
| 263 |
out.insert_pdf(src_doc, from_page=i, to_page=i)
|
| 264 |
-
# β Compress output
|
| 265 |
pdf_bytes = out.tobytes(garbage=4, deflate=True)
|
| 266 |
return pdf_bytes
|
| 267 |
finally:
|
| 268 |
out.close()
|
| 269 |
|
| 270 |
|
| 271 |
-
# β FIX 3: Cleanup utility
|
| 272 |
def remove_file(path: str):
|
| 273 |
try:
|
| 274 |
if os.path.exists(path):
|
| 275 |
os.remove(path)
|
| 276 |
-
print(f"π§Ή Cleaned up: {path}")
|
| 277 |
except Exception as e:
|
| 278 |
print(f"β οΈ Cleanup warning: {e}")
|
| 279 |
|
|
@@ -286,194 +728,363 @@ def remove_file(path: str):
|
|
| 286 |
async def split_invoices(
|
| 287 |
background_tasks: BackgroundTasks,
|
| 288 |
file: UploadFile = File(...),
|
| 289 |
-
|
| 290 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 291 |
):
|
| 292 |
"""
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
-
|
| 297 |
-
-
|
| 298 |
-
-
|
| 299 |
-
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 300 |
"""
|
|
|
|
|
|
|
| 301 |
if not file.filename.lower().endswith(".pdf"):
|
| 302 |
-
raise HTTPException(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 303 |
|
| 304 |
-
#
|
| 305 |
max_size_bytes = max_file_size_mb * 1024 * 1024
|
| 306 |
fd, temp_path = tempfile.mkstemp(suffix=".pdf")
|
| 307 |
os.close(fd)
|
| 308 |
|
| 309 |
doc = None
|
|
|
|
|
|
|
| 310 |
|
| 311 |
try:
|
| 312 |
-
|
| 313 |
-
print(f"π₯
|
| 314 |
-
|
|
|
|
|
|
|
|
|
|
| 315 |
|
|
|
|
| 316 |
with open(temp_path, "wb") as buffer:
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
while content := await file.read(chunk_size):
|
| 320 |
total_size += len(content)
|
| 321 |
-
|
| 322 |
if total_size > max_size_bytes:
|
| 323 |
remove_file(temp_path)
|
| 324 |
raise HTTPException(
|
| 325 |
-
status_code=413,
|
| 326 |
-
detail=f"File too large. Max: {max_file_size_mb}MB, got: {total_size/(1024*1024):.1f}MB"
|
| 327 |
-
)
|
| 328 |
-
|
| 329 |
buffer.write(content)
|
| 330 |
|
| 331 |
-
if total_size % (20 * 1024 * 1024) < chunk_size:
|
| 332 |
-
print(f" π Uploaded: {total_size/(1024*1024):.1f}MB")
|
| 333 |
-
|
| 334 |
file_size_mb = total_size / (1024 * 1024)
|
| 335 |
-
print(f"πΎ
|
| 336 |
-
|
| 337 |
-
#
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 346 |
|
| 347 |
# Detect PDF type
|
| 348 |
-
is_image_pdf,
|
| 349 |
-
|
| 350 |
if is_image_pdf and not get_gemini_model():
|
| 351 |
raise HTTPException(
|
| 352 |
-
status_code=500,
|
| 353 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 354 |
)
|
| 355 |
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
if i % 50 == 0:
|
| 360 |
-
print(f"\n--- Processing page {i+1}/{doc. page_count} ---")
|
| 361 |
-
|
| 362 |
-
page = doc. load_page(i)
|
| 363 |
-
inv = extract_invoice_no_from_page(page, is_image_pdf)
|
| 364 |
-
page_invoice_nos.append(inv)
|
| 365 |
-
page = None # Free memory
|
| 366 |
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 386 |
current_invoice = inv
|
| 387 |
-
|
| 388 |
else:
|
| 389 |
-
if inv
|
|
|
|
| 390 |
groups.append({
|
| 391 |
-
"invoice_no":
|
| 392 |
-
"pages":
|
| 393 |
})
|
|
|
|
| 394 |
current_invoice = inv
|
| 395 |
-
|
| 396 |
else:
|
| 397 |
-
|
|
|
|
| 398 |
|
| 399 |
-
|
|
|
|
| 400 |
groups.append({
|
| 401 |
-
"invoice_no":
|
| 402 |
-
"pages":
|
| 403 |
})
|
|
|
|
| 404 |
|
| 405 |
-
#
|
| 406 |
-
if len(groups)
|
| 407 |
-
groups[1]["pages"] = groups[0]["pages"] + groups[1]["pages"]
|
| 408 |
-
groups.pop(0)
|
| 409 |
-
|
| 410 |
-
if all(g["invoice_no"] is None for g in groups):
|
| 411 |
-
print("\nβ Warning: No invoices detected!")
|
| 412 |
groups = [{
|
| 413 |
"invoice_no": None,
|
| 414 |
-
"pages":
|
| 415 |
}]
|
| 416 |
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
|
|
|
| 422 |
|
| 423 |
for idx, g in enumerate(groups):
|
| 424 |
-
|
|
|
|
| 425 |
|
|
|
|
| 426 |
part_bytes = build_pdf_from_pages(doc, g["pages"])
|
| 427 |
|
| 428 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 429 |
"invoice_no": g["invoice_no"],
|
| 430 |
"pages": [p + 1 for p in g["pages"]],
|
|
|
|
| 431 |
"num_pages": len(g["pages"]),
|
| 432 |
"size_bytes": len(part_bytes),
|
| 433 |
-
"size_mb": round(len(part_bytes) / (1024 * 1024), 2)
|
| 434 |
}
|
| 435 |
|
| 436 |
-
#
|
| 437 |
-
if
|
| 438 |
-
|
| 439 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 440 |
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
f" β οΈ Response size limit reached ({MAX_RESPONSE_SIZE_MB}MB)")
|
| 444 |
-
print(f" π‘ Skipping base64 for remaining parts")
|
| 445 |
-
print(f" π‘ Use /split-invoices-stream for large files")
|
| 446 |
-
response_size_exceeded = True
|
| 447 |
-
info["pdf_base64"] = None
|
| 448 |
-
info["warning"] = f"Response too large. Use streaming endpoint."
|
| 449 |
-
else:
|
| 450 |
-
info["pdf_base64"] = base64.b64encode(
|
| 451 |
-
part_bytes).decode("ascii")
|
| 452 |
-
else:
|
| 453 |
-
info["pdf_base64"] = None
|
| 454 |
|
| 455 |
-
|
| 456 |
-
del part_bytes
|
| 457 |
-
gc.collect()
|
| 458 |
|
| 459 |
-
|
|
|
|
| 460 |
|
| 461 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 462 |
"success": True,
|
| 463 |
-
"
|
| 464 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 465 |
"source_file": {
|
| 466 |
"name": file.filename,
|
| 467 |
"size_mb": round(file_size_mb, 2),
|
| 468 |
-
"total_pages":
|
|
|
|
|
|
|
| 469 |
},
|
| 470 |
-
"
|
| 471 |
-
|
| 472 |
-
"
|
| 473 |
-
"
|
| 474 |
-
"
|
| 475 |
-
|
| 476 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 477 |
|
| 478 |
except HTTPException:
|
| 479 |
raise
|
|
@@ -489,162 +1100,169 @@ async def split_invoices(
|
|
| 489 |
gc.collect()
|
| 490 |
|
| 491 |
|
| 492 |
-
@app.post("/
|
| 493 |
-
async def
|
|
|
|
| 494 |
background_tasks: BackgroundTasks,
|
| 495 |
-
|
| 496 |
-
max_file_size_mb: int = Form(200),
|
| 497 |
):
|
| 498 |
-
"""
|
| 499 |
-
|
|
|
|
| 500 |
|
| 501 |
-
|
| 502 |
-
Each line is a separate invoice part.
|
| 503 |
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 507 |
|
| 508 |
-
if not file. filename.lower().endswith(".pdf"):
|
| 509 |
-
raise HTTPException(status_code=400, detail="Only PDF is supported")
|
| 510 |
|
| 511 |
-
|
| 512 |
-
|
| 513 |
-
|
|
|
|
| 514 |
|
| 515 |
-
|
|
|
|
| 516 |
try:
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
buffer.write(content)
|
| 527 |
except Exception as e:
|
| 528 |
-
|
| 529 |
-
raise
|
| 530 |
-
|
| 531 |
-
async def generate_parts():
|
| 532 |
-
doc = None
|
| 533 |
-
try:
|
| 534 |
-
doc = fitz.open(temp_path)
|
| 535 |
-
|
| 536 |
-
# Send status
|
| 537 |
-
yield json.dumps({
|
| 538 |
-
"type": "status",
|
| 539 |
-
"status": "processing",
|
| 540 |
-
"total_pages": doc.page_count,
|
| 541 |
-
"filename": file.filename
|
| 542 |
-
}) + "\n"
|
| 543 |
-
|
| 544 |
-
# Detect type
|
| 545 |
-
is_image_pdf, _ = is_image_based_pdf(doc)
|
| 546 |
-
|
| 547 |
-
# Extract
|
| 548 |
-
page_invoice_nos = []
|
| 549 |
-
for i in range(doc.page_count):
|
| 550 |
-
page = doc.load_page(i)
|
| 551 |
-
inv = extract_invoice_no_from_page(page, is_image_pdf)
|
| 552 |
-
page_invoice_nos.append(inv)
|
| 553 |
-
page = None
|
| 554 |
-
if i % 100 == 0:
|
| 555 |
-
gc.collect()
|
| 556 |
-
|
| 557 |
-
# Filter & group
|
| 558 |
-
clean_invs = [None if (v and v.upper().startswith(
|
| 559 |
-
"GST:")) else v for v in page_invoice_nos]
|
| 560 |
-
groups = []
|
| 561 |
-
current_group = []
|
| 562 |
-
current_inv = None
|
| 563 |
-
|
| 564 |
-
for idx, inv in enumerate(clean_invs):
|
| 565 |
-
if current_inv is None:
|
| 566 |
-
current_inv = inv
|
| 567 |
-
current_group = [idx]
|
| 568 |
-
else:
|
| 569 |
-
if inv is not None and inv != current_inv:
|
| 570 |
-
groups. append(
|
| 571 |
-
{"invoice_no": current_inv, "pages": current_group})
|
| 572 |
-
current_inv = inv
|
| 573 |
-
current_group = [idx]
|
| 574 |
-
else:
|
| 575 |
-
current_group.append(idx)
|
| 576 |
-
|
| 577 |
-
if current_group:
|
| 578 |
-
groups.append(
|
| 579 |
-
{"invoice_no": current_inv, "pages": current_group})
|
| 580 |
-
|
| 581 |
-
if len(groups) > 1 and groups[0]["invoice_no"] is None and groups[1]["invoice_no"] is not None:
|
| 582 |
-
groups[1]["pages"] = groups[0]["pages"] + groups[1]["pages"]
|
| 583 |
-
groups.pop(0)
|
| 584 |
-
|
| 585 |
-
# Stream each part
|
| 586 |
-
for idx, g in enumerate(groups):
|
| 587 |
-
part_bytes = build_pdf_from_pages(doc, g["pages"])
|
| 588 |
-
|
| 589 |
-
info = {
|
| 590 |
-
"type": "part",
|
| 591 |
-
"part_index": idx,
|
| 592 |
-
"invoice_no": g["invoice_no"],
|
| 593 |
-
"pages": [p + 1 for p in g["pages"]],
|
| 594 |
-
"num_pages": len(g["pages"]),
|
| 595 |
-
"size_bytes": len(part_bytes),
|
| 596 |
-
"pdf_base64": base64.b64encode(part_bytes).decode("ascii")
|
| 597 |
-
}
|
| 598 |
-
|
| 599 |
-
yield json.dumps(info) + "\n"
|
| 600 |
-
del part_bytes
|
| 601 |
-
gc.collect()
|
| 602 |
-
|
| 603 |
-
# Complete
|
| 604 |
-
yield json.dumps({
|
| 605 |
-
"type": "complete",
|
| 606 |
-
"total_parts": len(groups)
|
| 607 |
-
}) + "\n"
|
| 608 |
|
| 609 |
-
|
| 610 |
-
|
| 611 |
-
|
| 612 |
-
|
| 613 |
-
|
| 614 |
-
|
| 615 |
-
|
| 616 |
-
|
| 617 |
-
|
| 618 |
-
|
| 619 |
-
|
| 620 |
-
|
| 621 |
-
|
| 622 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 623 |
|
| 624 |
|
| 625 |
-
@app.get("/
|
| 626 |
-
async def
|
| 627 |
-
|
| 628 |
return {
|
| 629 |
-
"
|
| 630 |
-
"
|
| 631 |
-
"
|
| 632 |
-
"
|
| 633 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 634 |
}
|
| 635 |
|
| 636 |
|
| 637 |
if __name__ == "__main__":
|
| 638 |
import uvicorn
|
| 639 |
-
|
| 640 |
-
print(
|
| 641 |
-
print(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 642 |
|
| 643 |
uvicorn.run(
|
| 644 |
app,
|
| 645 |
-
host=
|
| 646 |
-
port=
|
| 647 |
workers=1,
|
| 648 |
-
timeout_keep_alive=
|
| 649 |
-
limit_concurrency=10
|
| 650 |
)
|
|
|
|
| 4 |
import base64
|
| 5 |
import gc
|
| 6 |
import tempfile
|
| 7 |
+
import uuid
|
| 8 |
+
import asyncio
|
| 9 |
from typing import List, Dict, Optional, Tuple
|
| 10 |
+
from collections import Counter
|
| 11 |
+
from concurrent.futures import ThreadPoolExecutor
|
| 12 |
|
| 13 |
from fastapi import FastAPI, File, UploadFile, Form, HTTPException, BackgroundTasks
|
| 14 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 15 |
+
from fastapi.responses import JSONResponse
|
| 16 |
from starlette.requests import Request
|
| 17 |
import fitz # PyMuPDF
|
| 18 |
|
| 19 |
+
# Azure Blob Storage
|
| 20 |
+
try:
|
| 21 |
+
from azure.storage.blob import (
|
| 22 |
+
BlobServiceClient,
|
| 23 |
+
generate_blob_sas,
|
| 24 |
+
BlobSasPermissions,
|
| 25 |
+
ContentSettings
|
| 26 |
+
)
|
| 27 |
+
AZURE_AVAILABLE = True
|
| 28 |
+
except ImportError:
|
| 29 |
+
AZURE_AVAILABLE = False
|
| 30 |
+
print("Warning: azure-storage-blob not installed. Run: pip install azure-storage-blob")
|
| 31 |
+
|
| 32 |
# Google Gemini - optional import
|
| 33 |
try:
|
| 34 |
import google.generativeai as genai
|
|
|
|
| 36 |
GEMINI_AVAILABLE = True
|
| 37 |
except ImportError:
|
| 38 |
GEMINI_AVAILABLE = False
|
| 39 |
+
print("Warning: google-generativeai not installed. Image-based PDFs won't be supported.")
|
| 40 |
+
|
| 41 |
+
from datetime import datetime, timedelta
|
| 42 |
|
| 43 |
+
app = FastAPI(title="Invoice Splitter API with Azure Blob Storage - Optimized")
|
| 44 |
|
| 45 |
+
# Increase request body size limit
|
| 46 |
+
Request.max_body_size = 200 * 1024 * 1024 # 200MB
|
| 47 |
|
| 48 |
app.add_middleware(
|
| 49 |
CORSMiddleware,
|
|
|
|
| 53 |
allow_headers=["*"],
|
| 54 |
)
|
| 55 |
|
| 56 |
+
# ============================================================================
|
| 57 |
+
# β CONFIGURATION FROM ENVIRONMENT VARIABLES (Hugging Face Secrets)
|
| 58 |
+
# ============================================================================
|
| 59 |
+
|
| 60 |
+
# Gemini API Key - REQUIRED for image-based PDFs
|
| 61 |
+
GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY", "")
|
| 62 |
+
|
| 63 |
+
# Azure Blob Storage Configuration - REQUIRED for blob storage
|
| 64 |
+
AZURE_STORAGE_CONNECTION_STRING = os.environ.get("AZURE_STORAGE_CONNECTION_STRING", "")
|
| 65 |
+
AZURE_STORAGE_ACCOUNT_NAME = os.environ.get("AZURE_STORAGE_ACCOUNT_NAME", "")
|
| 66 |
+
AZURE_STORAGE_ACCOUNT_KEY = os.environ.get("AZURE_STORAGE_ACCOUNT_KEY", "")
|
| 67 |
+
|
| 68 |
+
# Container name - can be configured or use default
|
| 69 |
+
AZURE_CONTAINER_NAME = os.environ.get("AZURE_CONTAINER_NAME", "invoice-splits")
|
| 70 |
+
|
| 71 |
+
# β FOLDER STRUCTURE CONFIGURATION
|
| 72 |
+
ROOT_FOLDER = os.environ.get("ROOT_FOLDER", "POD") # Root folder name
|
| 73 |
+
|
| 74 |
+
# β PERFORMANCE CONFIGURATION
|
| 75 |
+
MAX_PARALLEL_GEMINI_CALLS = int(os.environ.get("MAX_PARALLEL_GEMINI_CALLS", "5"))
|
| 76 |
+
GEMINI_IMAGE_RESOLUTION = float(os.environ.get("GEMINI_IMAGE_RESOLUTION", "1.2"))
|
| 77 |
+
USE_SMART_SAMPLING = os.environ.get("USE_SMART_SAMPLING", "false").lower() == "true"
|
| 78 |
+
|
| 79 |
+
# β SERVER CONFIGURATION
|
| 80 |
+
HOST = os.environ.get("HOST", "0.0.0.0") # Hugging Face uses 0.0.0.0
|
| 81 |
+
PORT = int(os.environ.get("PORT", "7860")) # Hugging Face default port
|
| 82 |
+
|
| 83 |
+
# ============================================================================
|
| 84 |
+
# GLOBAL VARIABLES
|
| 85 |
+
# ============================================================================
|
| 86 |
+
|
| 87 |
gemini_model = None
|
| 88 |
+
blob_service_client = None
|
| 89 |
+
|
| 90 |
+
# ============================================================================
|
| 91 |
+
# STARTUP VALIDATION
|
| 92 |
+
# ============================================================================
|
| 93 |
+
|
| 94 |
+
def validate_configuration():
|
| 95 |
+
"""Validate configuration and warn about missing credentials."""
|
| 96 |
+
warnings = []
|
| 97 |
+
errors = []
|
| 98 |
+
|
| 99 |
+
# Check Gemini API Key
|
| 100 |
+
if not GEMINI_API_KEY:
|
| 101 |
+
warnings.append("β οΈ GEMINI_API_KEY not set - image-based PDFs will not work")
|
| 102 |
+
else:
|
| 103 |
+
print(f"β
GEMINI_API_KEY configured ({len(GEMINI_API_KEY)} chars)")
|
| 104 |
+
|
| 105 |
+
# Check Azure credentials
|
| 106 |
+
if not AZURE_STORAGE_CONNECTION_STRING:
|
| 107 |
+
if not (AZURE_STORAGE_ACCOUNT_NAME and AZURE_STORAGE_ACCOUNT_KEY):
|
| 108 |
+
errors.append("β Azure credentials missing - set AZURE_STORAGE_CONNECTION_STRING or both AZURE_STORAGE_ACCOUNT_NAME and AZURE_STORAGE_ACCOUNT_KEY")
|
| 109 |
+
else:
|
| 110 |
+
print(f"β
Azure credentials configured (account: {AZURE_STORAGE_ACCOUNT_NAME})")
|
| 111 |
+
else:
|
| 112 |
+
print(f"β
Azure connection string configured")
|
| 113 |
+
|
| 114 |
+
# Print all warnings
|
| 115 |
+
for warning in warnings:
|
| 116 |
+
print(warning)
|
| 117 |
+
|
| 118 |
+
# Print all errors
|
| 119 |
+
for error in errors:
|
| 120 |
+
print(error)
|
| 121 |
+
|
| 122 |
+
if errors:
|
| 123 |
+
print("\nβ οΈ WARNING: Some required credentials are missing!")
|
| 124 |
+
print(" Set them in Hugging Face Spaces Settings > Repository secrets")
|
| 125 |
+
|
| 126 |
+
return len(errors) == 0
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
# ============================================================================
|
| 130 |
+
# AZURE BLOB STORAGE FUNCTIONS
|
| 131 |
+
# ============================================================================
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
def get_blob_service_client():
|
| 135 |
+
"""Get or create Azure Blob Service Client."""
|
| 136 |
+
global blob_service_client
|
| 137 |
+
|
| 138 |
+
if not AZURE_AVAILABLE:
|
| 139 |
+
print("β Azure SDK not available")
|
| 140 |
+
return None
|
| 141 |
+
|
| 142 |
+
if blob_service_client is None:
|
| 143 |
+
try:
|
| 144 |
+
if AZURE_STORAGE_CONNECTION_STRING:
|
| 145 |
+
blob_service_client = BlobServiceClient.from_connection_string(
|
| 146 |
+
AZURE_STORAGE_CONNECTION_STRING
|
| 147 |
+
)
|
| 148 |
+
print("β
Azure Blob Storage initialized with connection string")
|
| 149 |
+
elif AZURE_STORAGE_ACCOUNT_NAME and AZURE_STORAGE_ACCOUNT_KEY:
|
| 150 |
+
account_url = f"https://{AZURE_STORAGE_ACCOUNT_NAME}.blob.core.windows.net"
|
| 151 |
+
blob_service_client = BlobServiceClient(
|
| 152 |
+
account_url=account_url,
|
| 153 |
+
credential=AZURE_STORAGE_ACCOUNT_KEY
|
| 154 |
+
)
|
| 155 |
+
print("β
Azure Blob Storage initialized with account key")
|
| 156 |
+
else:
|
| 157 |
+
print("β οΈ WARNING: No Azure credentials configured")
|
| 158 |
+
return None
|
| 159 |
+
except Exception as e:
|
| 160 |
+
print(f"β Failed to initialize Azure Blob Storage: {e}")
|
| 161 |
+
return None
|
| 162 |
+
|
| 163 |
+
return blob_service_client
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
def ensure_container_exists(container_name: str = None):
|
| 167 |
+
"""Create container if it doesn't exist."""
|
| 168 |
+
if container_name is None:
|
| 169 |
+
container_name = AZURE_CONTAINER_NAME
|
| 170 |
+
|
| 171 |
+
try:
|
| 172 |
+
client = get_blob_service_client()
|
| 173 |
+
if client:
|
| 174 |
+
container_client = client.get_container_client(container_name)
|
| 175 |
+
if not container_client.exists():
|
| 176 |
+
container_client.create_container()
|
| 177 |
+
print(f"β
Created container: {container_name}")
|
| 178 |
+
else:
|
| 179 |
+
print(f"β
Container exists: {container_name}")
|
| 180 |
+
except Exception as e:
|
| 181 |
+
print(f"β οΈ Container check error: {e}")
|
| 182 |
+
|
| 183 |
+
|
| 184 |
+
def upload_raw_pdf_to_blob(
|
| 185 |
+
pdf_bytes: bytes,
|
| 186 |
+
filename: str,
|
| 187 |
+
batch_id: str,
|
| 188 |
+
container_name: str = None
|
| 189 |
+
) -> dict:
|
| 190 |
+
"""
|
| 191 |
+
Upload original/raw PDF to Azure Blob Storage.
|
| 192 |
+
|
| 193 |
+
Path structure: POD/{batch_id}/{filename}/Raw/{filename}
|
| 194 |
+
"""
|
| 195 |
+
if container_name is None:
|
| 196 |
+
container_name = AZURE_CONTAINER_NAME
|
| 197 |
+
|
| 198 |
+
try:
|
| 199 |
+
client = get_blob_service_client()
|
| 200 |
+
if not client:
|
| 201 |
+
raise HTTPException(
|
| 202 |
+
status_code=500,
|
| 203 |
+
detail="Azure Blob Storage not configured"
|
| 204 |
+
)
|
| 205 |
+
|
| 206 |
+
# Clean filename for folder name
|
| 207 |
+
base_filename = os.path.splitext(filename)[0]
|
| 208 |
+
safe_folder_name = re.sub(r'[<>:"/\\|?*]', '_', base_filename)
|
| 209 |
+
|
| 210 |
+
blob_name = f"{ROOT_FOLDER}/{batch_id}/{safe_folder_name}/Raw/{filename}"
|
| 211 |
+
|
| 212 |
+
# Get blob client
|
| 213 |
+
blob_client = client.get_blob_client(
|
| 214 |
+
container=container_name,
|
| 215 |
+
blob=blob_name
|
| 216 |
+
)
|
| 217 |
+
|
| 218 |
+
# Upload PDF
|
| 219 |
+
print(f"π€ Uploading raw PDF to: {blob_name}")
|
| 220 |
+
blob_client.upload_blob(
|
| 221 |
+
pdf_bytes,
|
| 222 |
+
overwrite=True,
|
| 223 |
+
content_settings=ContentSettings(content_type='application/pdf'),
|
| 224 |
+
metadata={
|
| 225 |
+
'batch_id': batch_id,
|
| 226 |
+
'file_type': 'raw',
|
| 227 |
+
'uploaded_at': datetime.now().isoformat(),
|
| 228 |
+
'original_filename': filename
|
| 229 |
+
}
|
| 230 |
+
)
|
| 231 |
+
|
| 232 |
+
# Generate SAS URL (valid for 24 hours)
|
| 233 |
+
expiry_hours = 24
|
| 234 |
+
sas_token = generate_blob_sas(
|
| 235 |
+
account_name=AZURE_STORAGE_ACCOUNT_NAME,
|
| 236 |
+
container_name=container_name,
|
| 237 |
+
blob_name=blob_name,
|
| 238 |
+
account_key=AZURE_STORAGE_ACCOUNT_KEY,
|
| 239 |
+
permission=BlobSasPermissions(read=True),
|
| 240 |
+
expiry=datetime.utcnow() + timedelta(hours=expiry_hours)
|
| 241 |
+
)
|
| 242 |
+
|
| 243 |
+
# Construct URLs
|
| 244 |
+
blob_url = blob_client.url
|
| 245 |
+
download_url = f"{blob_url}?{sas_token}"
|
| 246 |
+
expires_at = (datetime.utcnow() +
|
| 247 |
+
timedelta(hours=expiry_hours)).isoformat() + "Z"
|
| 248 |
+
|
| 249 |
+
print(f"β
Uploaded raw PDF: {blob_name}")
|
| 250 |
+
|
| 251 |
+
return {
|
| 252 |
+
"blob_name": blob_name,
|
| 253 |
+
"blob_url": blob_url,
|
| 254 |
+
"download_url": download_url,
|
| 255 |
+
"expires_at": expires_at,
|
| 256 |
+
"expires_in_hours": expiry_hours,
|
| 257 |
+
"storage": "azure_blob",
|
| 258 |
+
"folder_type": "raw",
|
| 259 |
+
"container": container_name,
|
| 260 |
+
"size_bytes": len(pdf_bytes),
|
| 261 |
+
"size_mb": round(len(pdf_bytes) / (1024 * 1024), 2)
|
| 262 |
+
}
|
| 263 |
+
|
| 264 |
+
except Exception as e:
|
| 265 |
+
print(f"β Raw PDF upload failed: {e}")
|
| 266 |
+
raise HTTPException(
|
| 267 |
+
status_code=500,
|
| 268 |
+
detail=f"Azure Blob upload failed: {str(e)}"
|
| 269 |
+
)
|
| 270 |
+
|
| 271 |
+
|
| 272 |
+
def upload_split_pdf_to_blob(
|
| 273 |
+
pdf_bytes: bytes,
|
| 274 |
+
invoice_filename: str,
|
| 275 |
+
original_filename: str,
|
| 276 |
+
batch_id: str,
|
| 277 |
+
container_name: str = None
|
| 278 |
+
) -> dict:
|
| 279 |
+
"""
|
| 280 |
+
Upload split invoice PDF to Azure Blob Storage.
|
| 281 |
+
|
| 282 |
+
Path structure: POD/{batch_id}/{original_filename}/Splitted/{invoice_filename}
|
| 283 |
+
"""
|
| 284 |
+
if container_name is None:
|
| 285 |
+
container_name = AZURE_CONTAINER_NAME
|
| 286 |
+
|
| 287 |
+
try:
|
| 288 |
+
client = get_blob_service_client()
|
| 289 |
+
if not client:
|
| 290 |
+
raise HTTPException(
|
| 291 |
+
status_code=500,
|
| 292 |
+
detail="Azure Blob Storage not configured"
|
| 293 |
+
)
|
| 294 |
+
|
| 295 |
+
# Clean original filename for folder name
|
| 296 |
+
base_filename = os.path.splitext(original_filename)[0]
|
| 297 |
+
safe_folder_name = re.sub(r'[<>:"/\\|?*]', '_', base_filename)
|
| 298 |
+
|
| 299 |
+
blob_name = f"{ROOT_FOLDER}/{batch_id}/{safe_folder_name}/Splitted/{invoice_filename}"
|
| 300 |
+
|
| 301 |
+
# Get blob client
|
| 302 |
+
blob_client = client.get_blob_client(
|
| 303 |
+
container=container_name,
|
| 304 |
+
blob=blob_name
|
| 305 |
+
)
|
| 306 |
+
|
| 307 |
+
# Upload PDF
|
| 308 |
+
blob_client.upload_blob(
|
| 309 |
+
pdf_bytes,
|
| 310 |
+
overwrite=True,
|
| 311 |
+
content_settings=ContentSettings(content_type='application/pdf'),
|
| 312 |
+
metadata={
|
| 313 |
+
'batch_id': batch_id,
|
| 314 |
+
'file_type': 'split',
|
| 315 |
+
'uploaded_at': datetime.now().isoformat(),
|
| 316 |
+
'original_filename': original_filename,
|
| 317 |
+
'invoice_filename': invoice_filename
|
| 318 |
+
}
|
| 319 |
+
)
|
| 320 |
+
|
| 321 |
+
# Generate SAS URL (valid for 24 hours)
|
| 322 |
+
expiry_hours = 24
|
| 323 |
+
sas_token = generate_blob_sas(
|
| 324 |
+
account_name=AZURE_STORAGE_ACCOUNT_NAME,
|
| 325 |
+
container_name=container_name,
|
| 326 |
+
blob_name=blob_name,
|
| 327 |
+
account_key=AZURE_STORAGE_ACCOUNT_KEY,
|
| 328 |
+
permission=BlobSasPermissions(read=True),
|
| 329 |
+
expiry=datetime.utcnow() + timedelta(hours=expiry_hours)
|
| 330 |
+
)
|
| 331 |
+
|
| 332 |
+
# Construct URLs
|
| 333 |
+
blob_url = blob_client.url
|
| 334 |
+
download_url = f"{blob_url}?{sas_token}"
|
| 335 |
+
expires_at = (datetime.utcnow() +
|
| 336 |
+
timedelta(hours=expiry_hours)).isoformat() + "Z"
|
| 337 |
+
|
| 338 |
+
return {
|
| 339 |
+
"blob_name": blob_name,
|
| 340 |
+
"blob_url": blob_url,
|
| 341 |
+
"download_url": download_url,
|
| 342 |
+
"expires_at": expires_at,
|
| 343 |
+
"expires_in_hours": expiry_hours,
|
| 344 |
+
"storage": "azure_blob",
|
| 345 |
+
"folder_type": "split",
|
| 346 |
+
"container": container_name,
|
| 347 |
+
"size_bytes": len(pdf_bytes),
|
| 348 |
+
"size_mb": round(len(pdf_bytes) / (1024 * 1024), 2)
|
| 349 |
+
}
|
| 350 |
+
|
| 351 |
+
except Exception as e:
|
| 352 |
+
print(f"β Split PDF upload failed: {e}")
|
| 353 |
+
raise HTTPException(
|
| 354 |
+
status_code=500,
|
| 355 |
+
detail=f"Azure Blob upload failed: {str(e)}"
|
| 356 |
+
)
|
| 357 |
+
|
| 358 |
+
|
| 359 |
+
async def cleanup_old_blobs(batch_id: str, container_name: str = None):
|
| 360 |
+
"""Delete all blobs for a specific batch_id."""
|
| 361 |
+
if container_name is None:
|
| 362 |
+
container_name = AZURE_CONTAINER_NAME
|
| 363 |
|
| 364 |
+
try:
|
| 365 |
+
client = get_blob_service_client()
|
| 366 |
+
if not client:
|
| 367 |
+
return
|
| 368 |
+
|
| 369 |
+
container_client = client.get_container_client(container_name)
|
| 370 |
+
|
| 371 |
+
prefix = f"{ROOT_FOLDER}/{batch_id}/"
|
| 372 |
+
blobs = container_client.list_blobs(name_starts_with=prefix)
|
| 373 |
+
|
| 374 |
+
deleted_count = 0
|
| 375 |
+
for blob in blobs:
|
| 376 |
+
blob_client = container_client.get_blob_client(blob.name)
|
| 377 |
+
blob_client.delete_blob()
|
| 378 |
+
deleted_count += 1
|
| 379 |
+
|
| 380 |
+
print(f"π§Ή Cleaned up {deleted_count} blobs for batch {batch_id}")
|
| 381 |
|
| 382 |
+
except Exception as e:
|
| 383 |
+
print(f"β οΈ Cleanup error: {e}")
|
| 384 |
+
|
| 385 |
+
|
| 386 |
+
# ============================================================================
|
| 387 |
+
# OPTIMIZED GEMINI FUNCTIONS WITH ASYNC PROCESSING
|
| 388 |
+
# ============================================================================
|
| 389 |
|
| 390 |
def get_gemini_model():
|
| 391 |
"""Get or create Gemini model instance."""
|
| 392 |
global gemini_model
|
| 393 |
|
| 394 |
if not GEMINI_AVAILABLE:
|
|
|
|
| 395 |
return None
|
| 396 |
|
| 397 |
if gemini_model is None:
|
| 398 |
if not GEMINI_API_KEY:
|
|
|
|
| 399 |
return None
|
| 400 |
|
| 401 |
try:
|
| 402 |
genai.configure(api_key=GEMINI_API_KEY)
|
| 403 |
+
# Use Gemini 2.5 Flash
|
| 404 |
+
gemini_model = genai.GenerativeModel('gemini-2.5-flash')
|
| 405 |
+
print("β
Google Gemini 2.5 Flash initialized")
|
| 406 |
except Exception as e:
|
| 407 |
+
print(f"β Failed to initialize Gemini: {e}")
|
| 408 |
return None
|
| 409 |
|
| 410 |
return gemini_model
|
| 411 |
|
| 412 |
|
| 413 |
+
def extract_invoice_gemini_sync(page: fitz.Page) -> Optional[str]:
|
| 414 |
+
"""
|
| 415 |
+
Optimized synchronous Gemini extraction for thread pool execution.
|
| 416 |
+
- Reduced image resolution for faster processing
|
| 417 |
+
- Simplified prompt for quicker responses
|
| 418 |
+
"""
|
| 419 |
+
model = get_gemini_model()
|
| 420 |
+
if not model:
|
| 421 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 422 |
|
| 423 |
+
try:
|
| 424 |
+
# Reduced resolution for faster processing
|
| 425 |
+
pix = page.get_pixmap(matrix=fitz.Matrix(
|
| 426 |
+
GEMINI_IMAGE_RESOLUTION, GEMINI_IMAGE_RESOLUTION))
|
| 427 |
+
img_bytes = pix.tobytes("png")
|
| 428 |
+
pix = None
|
| 429 |
+
img = Image.open(io.BytesIO(img_bytes))
|
| 430 |
|
| 431 |
+
# Optimized prompt for faster response
|
| 432 |
+
prompt = """Extract ONLY the invoice number from this image.
|
| 433 |
+
Look for: Invoice No, Bill No, Tax Invoice No, or Document No.
|
| 434 |
+
Return ONLY the number/code. If not found, return: NONE"""
|
| 435 |
|
| 436 |
+
response = model.generate_content([prompt, img])
|
| 437 |
+
if response and response.text:
|
| 438 |
+
extracted_text = response.text.strip()
|
| 439 |
+
if extracted_text and extracted_text not in ("NOT_FOUND", "NONE", "N/A", "NA"):
|
| 440 |
+
invoice_no = extracted_text.replace(
|
| 441 |
+
"*", "").replace("#", "").replace("Invoice No:", "").replace(":", "").strip()
|
| 442 |
+
if invoice_no and len(invoice_no) > 2:
|
| 443 |
+
img.close()
|
| 444 |
+
return invoice_no
|
| 445 |
+
|
| 446 |
+
img.close()
|
| 447 |
+
return None
|
| 448 |
+
|
| 449 |
+
except Exception as e:
|
| 450 |
+
print(f"Gemini error: {e}")
|
| 451 |
+
return None
|
| 452 |
+
|
| 453 |
+
|
| 454 |
+
async def extract_invoices_batch_async(
|
| 455 |
+
doc: fitz.Document,
|
| 456 |
+
is_image_pdf: bool,
|
| 457 |
+
batch_size: int = MAX_PARALLEL_GEMINI_CALLS
|
| 458 |
+
) -> List[Optional[str]]:
|
| 459 |
+
"""
|
| 460 |
+
π OPTIMIZED: Extract invoice numbers with parallel processing.
|
| 461 |
+
|
| 462 |
+
For text PDFs: Fast sequential processing
|
| 463 |
+
For image PDFs: Parallel Gemini API calls (5-10x faster)
|
| 464 |
+
"""
|
| 465 |
+
page_invoice_nos = []
|
| 466 |
+
|
| 467 |
+
if not is_image_pdf:
|
| 468 |
+
# Fast text-based extraction (no parallelization needed)
|
| 469 |
+
print(f" π Text-based extraction (sequential)")
|
| 470 |
+
for i in range(doc.page_count):
|
| 471 |
+
if i % 50 == 0:
|
| 472 |
+
print(f" Extracting... Page {i+1}/{doc.page_count}")
|
| 473 |
+
page = doc.load_page(i)
|
| 474 |
+
inv = extract_invoice_text_based(page)
|
| 475 |
+
page_invoice_nos.append(inv)
|
| 476 |
+
page = None
|
| 477 |
+
if i % 100 == 0:
|
| 478 |
+
gc.collect()
|
| 479 |
+
return page_invoice_nos
|
| 480 |
+
|
| 481 |
+
# Image-based PDF: Use parallel Gemini processing
|
| 482 |
+
print(f" π Image-based extraction (parallel, batch_size={batch_size})")
|
| 483 |
+
|
| 484 |
+
# Use ThreadPoolExecutor for parallel API calls
|
| 485 |
+
with ThreadPoolExecutor(max_workers=batch_size) as executor:
|
| 486 |
+
futures = []
|
| 487 |
+
|
| 488 |
+
# Submit all pages to thread pool
|
| 489 |
+
for i in range(doc.page_count):
|
| 490 |
+
page = doc.load_page(i)
|
| 491 |
+
# First try text extraction (fast)
|
| 492 |
+
text_result = extract_invoice_text_based(page)
|
| 493 |
+
if text_result:
|
| 494 |
+
futures.append((i, None, text_result))
|
| 495 |
+
else:
|
| 496 |
+
# Submit to Gemini thread pool
|
| 497 |
+
future = executor.submit(extract_invoice_gemini_sync, page)
|
| 498 |
+
futures.append((i, future, None))
|
| 499 |
+
|
| 500 |
+
# Collect results in order
|
| 501 |
+
page_invoice_nos = [None] * doc.page_count
|
| 502 |
+
completed = 0
|
| 503 |
+
|
| 504 |
+
for i, future, text_result in futures:
|
| 505 |
+
try:
|
| 506 |
+
if text_result:
|
| 507 |
+
# Already extracted from text
|
| 508 |
+
page_invoice_nos[i] = text_result
|
| 509 |
+
completed += 1
|
| 510 |
+
else:
|
| 511 |
+
# Wait for Gemini result
|
| 512 |
+
result = future.result(timeout=30)
|
| 513 |
+
page_invoice_nos[i] = result
|
| 514 |
+
completed += 1
|
| 515 |
+
|
| 516 |
+
if completed % 5 == 0:
|
| 517 |
+
print(
|
| 518 |
+
f" β Processed {completed}/{doc.page_count} pages...")
|
| 519 |
+
|
| 520 |
+
except Exception as e:
|
| 521 |
+
print(f" β οΈ Page {i+1} failed: {e}")
|
| 522 |
+
page_invoice_nos[i] = None
|
| 523 |
+
|
| 524 |
+
if completed % 20 == 0:
|
| 525 |
+
gc.collect()
|
| 526 |
+
|
| 527 |
+
print(f" β
Extraction complete: {completed}/{doc.page_count} pages")
|
| 528 |
+
return page_invoice_nos
|
| 529 |
+
|
| 530 |
+
|
| 531 |
+
def extract_invoices_smart_sampling(doc: fitz.Document, is_image_pdf: bool) -> List[Optional[str]]:
|
| 532 |
+
"""
|
| 533 |
+
β‘ FASTEST: Smart sampling strategy for large PDFs.
|
| 534 |
+
"""
|
| 535 |
+
print(f" β‘ Smart sampling mode (faster, ~95% accurate)")
|
| 536 |
+
|
| 537 |
+
page_invoice_nos = [None] * doc.page_count
|
| 538 |
+
|
| 539 |
+
# Always extract from first page
|
| 540 |
+
page = doc.load_page(0)
|
| 541 |
+
page_invoice_nos[0] = extract_invoice_no_from_page(page, is_image_pdf)
|
| 542 |
+
print(f" β Page 1: {page_invoice_nos[0]}")
|
| 543 |
+
|
| 544 |
+
# Sample every Nth page to detect changes
|
| 545 |
+
sample_interval = max(3, doc.page_count // 20)
|
| 546 |
+
print(f" Sampling interval: every {sample_interval} pages")
|
| 547 |
+
|
| 548 |
+
for i in range(sample_interval, doc.page_count, sample_interval):
|
| 549 |
+
page = doc.load_page(i)
|
| 550 |
+
inv = extract_invoice_no_from_page(page, is_image_pdf)
|
| 551 |
+
page_invoice_nos[i] = inv
|
| 552 |
+
|
| 553 |
+
if i % 10 == 0:
|
| 554 |
+
print(f" Sampling page {i+1}/{doc.page_count}...")
|
| 555 |
+
|
| 556 |
+
# If invoice changed, extract nearby pages to find exact boundary
|
| 557 |
+
prev_known_idx = i - sample_interval
|
| 558 |
+
while prev_known_idx >= 0 and page_invoice_nos[prev_known_idx] is None:
|
| 559 |
+
prev_known_idx -= 1
|
| 560 |
+
|
| 561 |
+
if prev_known_idx >= 0 and inv != page_invoice_nos[prev_known_idx]:
|
| 562 |
+
print(f" π Boundary detected near page {i+1}, refining...")
|
| 563 |
+
for offset in range(-3, 4):
|
| 564 |
+
idx = i + offset
|
| 565 |
+
if 0 <= idx < doc.page_count and page_invoice_nos[idx] is None:
|
| 566 |
+
page = doc.load_page(idx)
|
| 567 |
+
page_invoice_nos[idx] = extract_invoice_no_from_page(
|
| 568 |
+
page, is_image_pdf)
|
| 569 |
+
|
| 570 |
+
# Also check last page
|
| 571 |
+
if page_invoice_nos[-1] is None:
|
| 572 |
+
page = doc.load_page(doc.page_count - 1)
|
| 573 |
+
page_invoice_nos[-1] = extract_invoice_no_from_page(page, is_image_pdf)
|
| 574 |
+
print(f" β Last page: {page_invoice_nos[-1]}")
|
| 575 |
+
|
| 576 |
+
# Forward-fill gaps
|
| 577 |
+
last_known = page_invoice_nos[0]
|
| 578 |
+
filled = 0
|
| 579 |
+
for i in range(len(page_invoice_nos)):
|
| 580 |
+
if page_invoice_nos[i] is not None:
|
| 581 |
+
last_known = page_invoice_nos[i]
|
| 582 |
+
else:
|
| 583 |
+
page_invoice_nos[i] = last_known
|
| 584 |
+
filled += 1
|
| 585 |
+
|
| 586 |
+
print(f" β
Smart sampling complete: forward-filled {filled} pages")
|
| 587 |
+
return page_invoice_nos
|
| 588 |
+
|
| 589 |
+
|
| 590 |
+
# ============================================================================
|
| 591 |
+
# PDF PROCESSING FUNCTIONS
|
| 592 |
+
# ============================================================================
|
| 593 |
|
| 594 |
def is_image_based_pdf(doc: fitz.Document, sample_pages: int = 3) -> Tuple[bool, float]:
|
| 595 |
total_text_length = 0
|
|
|
|
| 597 |
|
| 598 |
for i in range(pages_to_check):
|
| 599 |
text = doc.load_page(i).get_text("text") or ""
|
| 600 |
+
total_text_length += len(text.strip())
|
| 601 |
|
| 602 |
avg_text_length = total_text_length / pages_to_check
|
| 603 |
is_image_based = avg_text_length < 50
|
| 604 |
|
| 605 |
+
print(f" PDF Type: {'IMAGE-BASED' if is_image_based else 'TEXT-BASED'}")
|
| 606 |
+
print(f" Avg text per page: {avg_text_length:.1f} chars")
|
|
|
|
|
|
|
|
|
|
| 607 |
return is_image_based, avg_text_length
|
| 608 |
|
| 609 |
|
|
|
|
| 616 |
return s
|
| 617 |
|
| 618 |
|
| 619 |
+
def is_valid_invoice_number(candidate: str) -> bool:
|
| 620 |
+
if not candidate or len(candidate) < 3:
|
| 621 |
+
return False
|
| 622 |
+
if len(candidate) == 15 and re.match(r'^[0-9A-Z]{15}$', candidate.upper()):
|
| 623 |
+
return False
|
| 624 |
+
if re.match(r'^\d+$', candidate):
|
| 625 |
+
return 6 <= len(candidate) <= 15
|
| 626 |
+
if re.match(r'^\d+\.\d{2,}$', candidate):
|
| 627 |
+
return False
|
| 628 |
+
has_letter = any(c.isalpha() for c in candidate)
|
| 629 |
+
has_digit = any(c.isdigit() for c in candidate)
|
| 630 |
+
return has_letter and has_digit
|
| 631 |
+
|
| 632 |
+
|
| 633 |
def try_extract_invoice_from_text(text: str) -> Optional[str]:
|
| 634 |
if not text:
|
| 635 |
return None
|
|
|
|
| 636 |
text_norm = normalize_text_for_search(text)
|
| 637 |
|
| 638 |
label_match = re.search(
|
| 639 |
+
r"(?:Invoice\s*No\.?|Inv\.?\s*No\.?|Bill\s*No\.?|Doc\s*No\.?|Document\s*No\.?|Tax\s*Invoice\s*No\.?)[\s:\-]*(\d{6,15})",
|
| 640 |
+
text_norm, re.IGNORECASE
|
|
|
|
| 641 |
)
|
| 642 |
+
if label_match:
|
| 643 |
+
invoice_num = label_match.group(1).strip()
|
| 644 |
+
if is_valid_invoice_number(invoice_num):
|
| 645 |
+
return invoice_num.upper()
|
| 646 |
|
| 647 |
+
label_match = re.search(
|
| 648 |
+
r"(?:Invoice|Inv|Bill|Doc|Document|Tax\s*Invoice)\s*(?:No|#|\.|:\s*)",
|
| 649 |
+
text_norm, re.IGNORECASE
|
| 650 |
+
)
|
| 651 |
if label_match:
|
| 652 |
start_idx = label_match.end()
|
| 653 |
+
candidate_text = text_norm[start_idx:start_idx + 60]
|
| 654 |
clean_candidates = re.sub(r"[:\-\(\)\[\]]", " ", candidate_text)
|
| 655 |
words = clean_candidates.split()
|
|
|
|
| 656 |
for word in words:
|
| 657 |
word = word.strip(".,;")
|
| 658 |
+
if word.lower() in ("order", "ref", "no", "date", "dt", "inv", "bill", "account"):
|
| 659 |
continue
|
| 660 |
+
if len(word) > 2 and is_valid_invoice_number(word):
|
| 661 |
+
return word.upper()
|
| 662 |
+
|
| 663 |
+
top_text = text_norm[:800]
|
| 664 |
+
digit_matches = re.findall(r'\b(\d{6,15})\b', top_text)
|
| 665 |
+
for match in digit_matches:
|
| 666 |
+
if is_valid_invoice_number(match):
|
| 667 |
+
if not re.match(r'^(19|20)\d{6}$', match):
|
| 668 |
+
if not re.match(r'^[6-9]\d{9}$', match):
|
| 669 |
+
return match.upper()
|
| 670 |
|
| 671 |
top_text = text_norm[:600]
|
| 672 |
m = re.search(r"\b([A-Z0-9][A-Z0-9\-\/]{4,})\b", top_text)
|
| 673 |
if m:
|
| 674 |
+
inv = m.group(1).upper()
|
| 675 |
+
if is_valid_invoice_number(inv):
|
| 676 |
return inv
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 677 |
return None
|
| 678 |
|
| 679 |
|
| 680 |
+
def extract_invoice_text_based(page: fitz.Page) -> Optional[str]:
|
| 681 |
text = page.get_text("text") or ""
|
| 682 |
inv = try_extract_invoice_from_text(text)
|
| 683 |
if inv:
|
| 684 |
return inv
|
|
|
|
| 685 |
for block in (page.get_text("blocks") or []):
|
| 686 |
block_text = block[4] if len(block) > 4 else ""
|
| 687 |
if block_text:
|
| 688 |
inv = try_extract_invoice_from_text(block_text)
|
| 689 |
if inv:
|
| 690 |
return inv
|
|
|
|
| 691 |
return None
|
| 692 |
|
| 693 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 694 |
def extract_invoice_no_from_page(page: fitz.Page, is_image_pdf: bool) -> Optional[str]:
|
| 695 |
+
"""Extract invoice number from a single page (used by smart sampling)."""
|
| 696 |
text_result = extract_invoice_text_based(page)
|
| 697 |
if text_result:
|
|
|
|
| 698 |
return text_result
|
|
|
|
| 699 |
if is_image_pdf:
|
| 700 |
+
return extract_invoice_gemini_sync(page)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 701 |
return None
|
| 702 |
|
| 703 |
|
| 704 |
def build_pdf_from_pages(src_doc: fitz.Document, page_indices: List[int]) -> bytes:
|
|
|
|
| 705 |
out = fitz.open()
|
| 706 |
try:
|
| 707 |
for i in page_indices:
|
| 708 |
out.insert_pdf(src_doc, from_page=i, to_page=i)
|
|
|
|
| 709 |
pdf_bytes = out.tobytes(garbage=4, deflate=True)
|
| 710 |
return pdf_bytes
|
| 711 |
finally:
|
| 712 |
out.close()
|
| 713 |
|
| 714 |
|
|
|
|
| 715 |
def remove_file(path: str):
|
| 716 |
try:
|
| 717 |
if os.path.exists(path):
|
| 718 |
os.remove(path)
|
|
|
|
| 719 |
except Exception as e:
|
| 720 |
print(f"β οΈ Cleanup warning: {e}")
|
| 721 |
|
|
|
|
| 728 |
async def split_invoices(
|
| 729 |
background_tasks: BackgroundTasks,
|
| 730 |
file: UploadFile = File(...),
|
| 731 |
+
|
| 732 |
+
# β REQUIRED: Batch ID
|
| 733 |
+
batch_id: str = Form(...,
|
| 734 |
+
description="Batch ID (required) - used for folder structure"),
|
| 735 |
+
|
| 736 |
+
# Blob Storage options
|
| 737 |
+
use_blob_storage: bool = Form(
|
| 738 |
+
True, description="Upload PDFs to Azure Blob Storage"),
|
| 739 |
+
blob_container: Optional[str] = Form(
|
| 740 |
+
None, description="Custom Azure container (optional)"),
|
| 741 |
+
|
| 742 |
+
# Response options
|
| 743 |
+
include_base64: bool = Form(
|
| 744 |
+
False, description="Include base64 in response"),
|
| 745 |
+
|
| 746 |
+
# Performance options
|
| 747 |
+
parallel_batch_size: int = Form(
|
| 748 |
+
MAX_PARALLEL_GEMINI_CALLS, description="Parallel Gemini API calls (1-10)"),
|
| 749 |
+
use_smart_sampling: bool = Form(
|
| 750 |
+
USE_SMART_SAMPLING, description="Use smart sampling (faster, ~95% accurate)"),
|
| 751 |
+
|
| 752 |
+
# File size limit
|
| 753 |
+
max_file_size_mb: int = Form(200, description="Maximum file size in MB"),
|
| 754 |
):
|
| 755 |
"""
|
| 756 |
+
β OPTIMIZED INVOICE SPLITTER WITH AZURE BLOB STORAGE
|
| 757 |
+
|
| 758 |
+
Performance Improvements:
|
| 759 |
+
- Parallel Gemini API calls (5-10x faster for image PDFs)
|
| 760 |
+
- Smart sampling option for large PDFs
|
| 761 |
+
- Reduced image resolution for faster processing
|
| 762 |
+
- Optimized prompts for quicker responses
|
| 763 |
+
|
| 764 |
+
Folder Structure in Blob Storage:
|
| 765 |
+
POD/
|
| 766 |
+
βββ {batch_id}/
|
| 767 |
+
βββ {filename}/
|
| 768 |
+
βββ Raw/ (original uploaded PDF)
|
| 769 |
+
βββ Splitted/ (individual split invoice PDFs)
|
| 770 |
+
|
| 771 |
+
Required Parameters:
|
| 772 |
+
- file: PDF file to upload
|
| 773 |
+
- batch_id: Batch identifier (used for folder structure)
|
| 774 |
+
|
| 775 |
+
Returns:
|
| 776 |
+
- All invoice URLs with proper folder paths
|
| 777 |
"""
|
| 778 |
+
|
| 779 |
+
# Validation
|
| 780 |
if not file.filename.lower().endswith(".pdf"):
|
| 781 |
+
raise HTTPException(
|
| 782 |
+
status_code=400, detail="Only PDF files are supported")
|
| 783 |
+
|
| 784 |
+
# Check blob storage
|
| 785 |
+
if use_blob_storage and not get_blob_service_client():
|
| 786 |
+
raise HTTPException(
|
| 787 |
+
status_code=500, detail="Azure Blob Storage not configured")
|
| 788 |
+
|
| 789 |
+
# Container
|
| 790 |
+
container_name = blob_container if blob_container else AZURE_CONTAINER_NAME
|
| 791 |
+
|
| 792 |
+
# Ensure container exists
|
| 793 |
+
if use_blob_storage:
|
| 794 |
+
ensure_container_exists(container_name)
|
| 795 |
|
| 796 |
+
# Stream upload to temp file
|
| 797 |
max_size_bytes = max_file_size_mb * 1024 * 1024
|
| 798 |
fd, temp_path = tempfile.mkstemp(suffix=".pdf")
|
| 799 |
os.close(fd)
|
| 800 |
|
| 801 |
doc = None
|
| 802 |
+
original_pdf_bytes = None
|
| 803 |
+
start_time = datetime.now()
|
| 804 |
|
| 805 |
try:
|
| 806 |
+
print(f"\n{'='*70}")
|
| 807 |
+
print(f"π₯ Processing: {file.filename}")
|
| 808 |
+
print(f" Batch ID: {batch_id}")
|
| 809 |
+
print(
|
| 810 |
+
f" Performance Mode: {'Smart Sampling' if use_smart_sampling else f'Parallel ({parallel_batch_size} workers)'}")
|
| 811 |
+
print(f"{'='*70}")
|
| 812 |
|
| 813 |
+
total_size = 0
|
| 814 |
with open(temp_path, "wb") as buffer:
|
| 815 |
+
chunk_read_size = 5 * 1024 * 1024
|
| 816 |
+
while content := await file.read(chunk_read_size):
|
|
|
|
| 817 |
total_size += len(content)
|
|
|
|
| 818 |
if total_size > max_size_bytes:
|
| 819 |
remove_file(temp_path)
|
| 820 |
raise HTTPException(
|
| 821 |
+
status_code=413, detail=f"File too large. Max: {max_file_size_mb}MB")
|
|
|
|
|
|
|
|
|
|
| 822 |
buffer.write(content)
|
| 823 |
|
|
|
|
|
|
|
|
|
|
| 824 |
file_size_mb = total_size / (1024 * 1024)
|
| 825 |
+
print(f"πΎ File size: {file_size_mb:.2f}MB")
|
| 826 |
+
|
| 827 |
+
# Read original PDF bytes
|
| 828 |
+
with open(temp_path, "rb") as f:
|
| 829 |
+
original_pdf_bytes = f.read()
|
| 830 |
+
|
| 831 |
+
# Upload original PDF to Raw folder
|
| 832 |
+
raw_pdf_info = None
|
| 833 |
+
if use_blob_storage:
|
| 834 |
+
try:
|
| 835 |
+
print(f"\nπ€ Uploading original PDF to Raw folder...")
|
| 836 |
+
raw_pdf_info = upload_raw_pdf_to_blob(
|
| 837 |
+
original_pdf_bytes,
|
| 838 |
+
file.filename,
|
| 839 |
+
batch_id,
|
| 840 |
+
container_name
|
| 841 |
+
)
|
| 842 |
+
print(f"β
Original PDF uploaded: {raw_pdf_info['blob_name']}")
|
| 843 |
+
except Exception as e:
|
| 844 |
+
print(f"β οΈ Failed to upload raw PDF: {e}")
|
| 845 |
+
|
| 846 |
+
# Open PDF for processing
|
| 847 |
+
doc = fitz.open(temp_path)
|
| 848 |
+
if doc.page_count == 0:
|
| 849 |
+
raise HTTPException(status_code=400, detail="Empty PDF")
|
| 850 |
+
|
| 851 |
+
print(f"π Total pages: {doc.page_count}")
|
| 852 |
|
| 853 |
# Detect PDF type
|
| 854 |
+
is_image_pdf, _ = is_image_based_pdf(doc)
|
|
|
|
| 855 |
if is_image_pdf and not get_gemini_model():
|
| 856 |
raise HTTPException(
|
| 857 |
+
status_code=500, detail="Image PDF detected but Gemini not configured")
|
| 858 |
+
|
| 859 |
+
# β‘ OPTIMIZED EXTRACTION
|
| 860 |
+
print(f"\nπ Extracting invoice numbers...")
|
| 861 |
+
extraction_start = datetime.now()
|
| 862 |
+
|
| 863 |
+
if use_smart_sampling and doc.page_count > 10:
|
| 864 |
+
# Smart sampling for large PDFs
|
| 865 |
+
page_invoice_nos = extract_invoices_smart_sampling(
|
| 866 |
+
doc, is_image_pdf)
|
| 867 |
+
else:
|
| 868 |
+
# Parallel extraction (async batch processing)
|
| 869 |
+
page_invoice_nos = await extract_invoices_batch_async(
|
| 870 |
+
doc,
|
| 871 |
+
is_image_pdf,
|
| 872 |
+
batch_size=parallel_batch_size
|
| 873 |
)
|
| 874 |
|
| 875 |
+
extraction_time = (datetime.now() - extraction_start).total_seconds()
|
| 876 |
+
print(f"β
Extraction completed in {extraction_time:.1f} seconds")
|
| 877 |
+
print(f" Speed: {doc.page_count / extraction_time:.1f} pages/second")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 878 |
|
| 879 |
+
# ============================================================================
|
| 880 |
+
# π§ CORRECTED GROUPING LOGIC - NO AGGRESSIVE FILTERING
|
| 881 |
+
# ============================================================================
|
| 882 |
+
|
| 883 |
+
print(f"\nπ§ Grouping invoices...")
|
| 884 |
+
|
| 885 |
+
# DEBUG: Show raw extraction results
|
| 886 |
+
print(f"\nπ DEBUG - Raw extraction results:")
|
| 887 |
+
for idx, inv in enumerate(page_invoice_nos[:min(10, len(page_invoice_nos))]):
|
| 888 |
+
print(f" Page {idx+1}: {inv if inv else '(not found)'}")
|
| 889 |
+
if len(page_invoice_nos) > 10:
|
| 890 |
+
print(f" ... (showing first 10 of {len(page_invoice_nos)} pages)")
|
| 891 |
+
|
| 892 |
+
# Step 1: Normalize extracted invoice numbers (only filter GST numbers)
|
| 893 |
+
page_invoice_nos_normalized = []
|
| 894 |
+
for v in page_invoice_nos:
|
| 895 |
+
if v and v.upper().startswith("GST"):
|
| 896 |
+
# Filter out GST numbers (not invoice numbers)
|
| 897 |
+
page_invoice_nos_normalized.append(None)
|
| 898 |
+
elif v:
|
| 899 |
+
# Normalize: uppercase, remove spaces/underscores
|
| 900 |
+
normalized = v.upper().strip().replace(" ", "").replace("_", "")
|
| 901 |
+
page_invoice_nos_normalized.append(normalized)
|
| 902 |
+
else:
|
| 903 |
+
page_invoice_nos_normalized.append(None)
|
| 904 |
+
|
| 905 |
+
# Step 2: Smart forward-fill for failed extractions
|
| 906 |
+
# Only fill None values, DON'T remove any extracted invoice numbers
|
| 907 |
+
page_invoice_nos_filled = []
|
| 908 |
+
last_known_invoice = None
|
| 909 |
+
|
| 910 |
+
for idx, inv in enumerate(page_invoice_nos_normalized):
|
| 911 |
+
if inv is not None:
|
| 912 |
+
# Valid invoice number found
|
| 913 |
+
last_known_invoice = inv
|
| 914 |
+
page_invoice_nos_filled.append(inv)
|
| 915 |
+
else:
|
| 916 |
+
# Extraction failed - use last known invoice
|
| 917 |
+
page_invoice_nos_filled.append(last_known_invoice)
|
| 918 |
+
|
| 919 |
+
# Count how many pages were forward-filled
|
| 920 |
+
filled_count = sum(1 for i in range(len(page_invoice_nos_normalized))
|
| 921 |
+
if page_invoice_nos_normalized[i] is None and page_invoice_nos_filled[i] is not None)
|
| 922 |
+
|
| 923 |
+
# Debug: Count unique invoice numbers
|
| 924 |
+
unique_invoices = set([v for v in page_invoice_nos_filled if v is not None])
|
| 925 |
+
print(f"\n π Found {len(unique_invoices)} unique invoice numbers:")
|
| 926 |
+
for inv_no in sorted(unique_invoices) if unique_invoices else []:
|
| 927 |
+
page_count = sum(1 for v in page_invoice_nos_filled if v == inv_no)
|
| 928 |
+
print(f" β’ {inv_no}: {page_count} pages")
|
| 929 |
+
|
| 930 |
+
# Step 3: Group consecutive pages by invoice number
|
| 931 |
+
groups = []
|
| 932 |
+
current_group = []
|
| 933 |
+
current_invoice = None
|
| 934 |
+
|
| 935 |
+
for idx, inv in enumerate(page_invoice_nos_filled):
|
| 936 |
+
if idx == 0:
|
| 937 |
+
# First page
|
| 938 |
current_invoice = inv
|
| 939 |
+
current_group = [idx]
|
| 940 |
else:
|
| 941 |
+
if inv != current_invoice:
|
| 942 |
+
# Invoice number changed - save current group and start new one
|
| 943 |
groups.append({
|
| 944 |
+
"invoice_no": current_invoice,
|
| 945 |
+
"pages": current_group[:]
|
| 946 |
})
|
| 947 |
+
print(f" π Group {len(groups)}: Invoice {current_invoice or 'UNKNOWN'} - Pages {current_group[0]+1}-{current_group[-1]+1} ({len(current_group)} pages)")
|
| 948 |
current_invoice = inv
|
| 949 |
+
current_group = [idx]
|
| 950 |
else:
|
| 951 |
+
# Same invoice - add to current group
|
| 952 |
+
current_group.append(idx)
|
| 953 |
|
| 954 |
+
# Don't forget the last group
|
| 955 |
+
if current_group:
|
| 956 |
groups.append({
|
| 957 |
+
"invoice_no": current_invoice,
|
| 958 |
+
"pages": current_group[:]
|
| 959 |
})
|
| 960 |
+
print(f" π Group {len(groups)}: Invoice {current_invoice or 'UNKNOWN'} - Pages {current_group[0]+1}-{current_group[-1]+1} ({len(current_group)} pages)")
|
| 961 |
|
| 962 |
+
# Handle edge case: entire PDF has no invoice numbers
|
| 963 |
+
if len(groups) == 1 and groups[0]["invoice_no"] is None:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 964 |
groups = [{
|
| 965 |
"invoice_no": None,
|
| 966 |
+
"pages": list(range(doc.page_count))
|
| 967 |
}]
|
| 968 |
|
| 969 |
+
print(f"\nβ
Created {len(groups)} invoice groups")
|
| 970 |
+
print(f" Forward-filled {filled_count} pages with missing invoice numbers")
|
| 971 |
+
|
| 972 |
+
# Build and upload split PDFs
|
| 973 |
+
print(f"\nπ¨ Building and uploading split invoices...")
|
| 974 |
+
all_parts = []
|
| 975 |
|
| 976 |
for idx, g in enumerate(groups):
|
| 977 |
+
if (idx + 1) % 20 == 0:
|
| 978 |
+
print(f" Processing {idx + 1}/{len(groups)} invoices...")
|
| 979 |
|
| 980 |
+
# Build PDF
|
| 981 |
part_bytes = build_pdf_from_pages(doc, g["pages"])
|
| 982 |
|
| 983 |
+
# Generate filename
|
| 984 |
+
invoice_no = g["invoice_no"] if g["invoice_no"] else f"NO_NUMBER_{idx + 1}"
|
| 985 |
+
safe_invoice_no = re.sub(r'[<>:"/\\|?*]', '_', invoice_no)
|
| 986 |
+
invoice_filename = f"invoice_{safe_invoice_no}.pdf"
|
| 987 |
+
|
| 988 |
+
# Prepare invoice info
|
| 989 |
+
invoice_info = {
|
| 990 |
"invoice_no": g["invoice_no"],
|
| 991 |
"pages": [p + 1 for p in g["pages"]],
|
| 992 |
+
"page_range": f"{g['pages'][0]+1}-{g['pages'][-1]+1}" if len(g['pages']) > 1 else f"{g['pages'][0]+1}",
|
| 993 |
"num_pages": len(g["pages"]),
|
| 994 |
"size_bytes": len(part_bytes),
|
| 995 |
+
"size_mb": round(len(part_bytes) / (1024 * 1024), 2),
|
| 996 |
}
|
| 997 |
|
| 998 |
+
# Upload to Splitted folder
|
| 999 |
+
if use_blob_storage:
|
| 1000 |
+
try:
|
| 1001 |
+
blob_info = upload_split_pdf_to_blob(
|
| 1002 |
+
part_bytes,
|
| 1003 |
+
invoice_filename,
|
| 1004 |
+
file.filename,
|
| 1005 |
+
batch_id,
|
| 1006 |
+
container_name
|
| 1007 |
+
)
|
| 1008 |
+
invoice_info["storage"] = blob_info
|
| 1009 |
+
invoice_info["pdf_url"] = blob_info["download_url"]
|
| 1010 |
+
invoice_info["blob_name"] = blob_info["blob_name"]
|
| 1011 |
+
invoice_info["expires_at"] = blob_info["expires_at"]
|
| 1012 |
+
except Exception as e:
|
| 1013 |
+
print(f" β οΈ Failed to upload invoice {idx+1}: {e}")
|
| 1014 |
+
invoice_info["upload_error"] = str(e)
|
| 1015 |
+
|
| 1016 |
+
# Include base64 if requested
|
| 1017 |
+
if include_base64:
|
| 1018 |
+
invoice_info["pdf_base64"] = base64.b64encode(
|
| 1019 |
+
part_bytes).decode("ascii")
|
| 1020 |
+
|
| 1021 |
+
all_parts.append(invoice_info)
|
| 1022 |
+
del part_bytes
|
| 1023 |
|
| 1024 |
+
if idx % 50 == 0:
|
| 1025 |
+
gc.collect()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1026 |
|
| 1027 |
+
print(f"β
Processed all {len(all_parts)} invoices")
|
|
|
|
|
|
|
| 1028 |
|
| 1029 |
+
# β SAVE VALUES BEFORE CLOSING DOCUMENT
|
| 1030 |
+
total_pages_count = doc.page_count
|
| 1031 |
|
| 1032 |
+
# Close document
|
| 1033 |
+
doc.close()
|
| 1034 |
+
doc = None
|
| 1035 |
+
remove_file(temp_path)
|
| 1036 |
+
gc.collect()
|
| 1037 |
+
|
| 1038 |
+
# Calculate total processing time
|
| 1039 |
+
total_time = (datetime.now() - start_time).total_seconds()
|
| 1040 |
+
|
| 1041 |
+
# Return response
|
| 1042 |
+
response_data = {
|
| 1043 |
"success": True,
|
| 1044 |
+
"batch_id": batch_id,
|
| 1045 |
+
"folder_structure": {
|
| 1046 |
+
"root": ROOT_FOLDER,
|
| 1047 |
+
"path": f"{ROOT_FOLDER}/{batch_id}/{os.path.splitext(file.filename)[0]}",
|
| 1048 |
+
"raw_folder": f"{ROOT_FOLDER}/{batch_id}/{os.path.splitext(file.filename)[0]}/Raw",
|
| 1049 |
+
"split_folder": f"{ROOT_FOLDER}/{batch_id}/{os.path.splitext(file.filename)[0]}/Splitted"
|
| 1050 |
+
},
|
| 1051 |
"source_file": {
|
| 1052 |
"name": file.filename,
|
| 1053 |
"size_mb": round(file_size_mb, 2),
|
| 1054 |
+
"total_pages": total_pages_count,
|
| 1055 |
+
"pdf_type": "image-based" if is_image_pdf else "text-based",
|
| 1056 |
+
"raw_pdf": raw_pdf_info
|
| 1057 |
},
|
| 1058 |
+
"summary": {
|
| 1059 |
+
"total_invoices": len(all_parts),
|
| 1060 |
+
"unique_invoice_numbers": len(unique_invoices),
|
| 1061 |
+
"extraction_method": "gemini" if is_image_pdf else "text",
|
| 1062 |
+
"pages_forward_filled": filled_count,
|
| 1063 |
+
"storage_type": "azure_blob" if use_blob_storage else "base64"
|
| 1064 |
+
},
|
| 1065 |
+
"performance": {
|
| 1066 |
+
"total_time_seconds": round(total_time, 2),
|
| 1067 |
+
"extraction_time_seconds": round(extraction_time, 2),
|
| 1068 |
+
"pages_per_second": round(total_pages_count / extraction_time, 2) if extraction_time > 0 else 0,
|
| 1069 |
+
"parallel_batch_size": parallel_batch_size,
|
| 1070 |
+
"smart_sampling_used": use_smart_sampling and total_pages_count > 10
|
| 1071 |
+
},
|
| 1072 |
+
"invoices": all_parts
|
| 1073 |
+
}
|
| 1074 |
+
|
| 1075 |
+
print(f"\n{'='*70}")
|
| 1076 |
+
print(f"β
SUCCESS!")
|
| 1077 |
+
print(f" Batch ID: {batch_id}")
|
| 1078 |
+
print(
|
| 1079 |
+
f" Raw PDF: {raw_pdf_info['blob_name'] if raw_pdf_info else 'Not uploaded'}")
|
| 1080 |
+
print(f" Split invoices: {len(all_parts)}")
|
| 1081 |
+
print(f" Unique invoice numbers: {len(unique_invoices)}")
|
| 1082 |
+
print(f" Total time: {total_time:.1f}s")
|
| 1083 |
+
print(
|
| 1084 |
+
f" Extraction time: {extraction_time:.1f}s ({total_pages_count / extraction_time:.1f} pages/sec)")
|
| 1085 |
+
print(f"{'='*70}\n")
|
| 1086 |
+
|
| 1087 |
+
return JSONResponse(response_data)
|
| 1088 |
|
| 1089 |
except HTTPException:
|
| 1090 |
raise
|
|
|
|
| 1100 |
gc.collect()
|
| 1101 |
|
| 1102 |
|
| 1103 |
+
@app.post("/cleanup-batch/{batch_id}")
|
| 1104 |
+
async def cleanup_batch(
|
| 1105 |
+
batch_id: str,
|
| 1106 |
background_tasks: BackgroundTasks,
|
| 1107 |
+
container_name: Optional[str] = Form(None)
|
|
|
|
| 1108 |
):
|
| 1109 |
+
"""Delete all blobs for a specific batch (entire POD/{batch_id}/ folder)."""
|
| 1110 |
+
if container_name is None:
|
| 1111 |
+
container_name = AZURE_CONTAINER_NAME
|
| 1112 |
|
| 1113 |
+
background_tasks.add_task(cleanup_old_blobs, batch_id, container_name)
|
|
|
|
| 1114 |
|
| 1115 |
+
return JSONResponse({
|
| 1116 |
+
"success": True,
|
| 1117 |
+
"message": f"Cleanup started for batch {batch_id}",
|
| 1118 |
+
"batch_id": batch_id,
|
| 1119 |
+
"folder_path": f"{ROOT_FOLDER}/{batch_id}/",
|
| 1120 |
+
"container": container_name
|
| 1121 |
+
})
|
| 1122 |
|
|
|
|
|
|
|
| 1123 |
|
| 1124 |
+
@app.get("/health")
|
| 1125 |
+
async def health_check():
|
| 1126 |
+
"""Health check endpoint."""
|
| 1127 |
+
gemini_status = "configured" if get_gemini_model() else "not configured"
|
| 1128 |
|
| 1129 |
+
blob_status = "not configured"
|
| 1130 |
+
blob_details = None
|
| 1131 |
try:
|
| 1132 |
+
client = get_blob_service_client()
|
| 1133 |
+
if client:
|
| 1134 |
+
blob_status = "configured"
|
| 1135 |
+
blob_details = {
|
| 1136 |
+
"account_name": AZURE_STORAGE_ACCOUNT_NAME,
|
| 1137 |
+
"container": AZURE_CONTAINER_NAME,
|
| 1138 |
+
"root_folder": ROOT_FOLDER,
|
| 1139 |
+
"available": True
|
| 1140 |
+
}
|
|
|
|
| 1141 |
except Exception as e:
|
| 1142 |
+
blob_status = f"error: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1143 |
|
| 1144 |
+
return {
|
| 1145 |
+
"status": "healthy",
|
| 1146 |
+
"timestamp": datetime.now().isoformat(),
|
| 1147 |
+
"services": {
|
| 1148 |
+
"gemini": {
|
| 1149 |
+
"status": gemini_status,
|
| 1150 |
+
"available": GEMINI_AVAILABLE,
|
| 1151 |
+
"model": "gemini-2.5-flash",
|
| 1152 |
+
"api_key_set": bool(GEMINI_API_KEY)
|
| 1153 |
+
},
|
| 1154 |
+
"azure_blob_storage": {
|
| 1155 |
+
"status": blob_status,
|
| 1156 |
+
"available": AZURE_AVAILABLE,
|
| 1157 |
+
"details": blob_details,
|
| 1158 |
+
"credentials_set": bool(AZURE_STORAGE_CONNECTION_STRING or (AZURE_STORAGE_ACCOUNT_NAME and AZURE_STORAGE_ACCOUNT_KEY))
|
| 1159 |
+
}
|
| 1160 |
+
},
|
| 1161 |
+
"performance": {
|
| 1162 |
+
"max_parallel_gemini_calls": MAX_PARALLEL_GEMINI_CALLS,
|
| 1163 |
+
"gemini_image_resolution": GEMINI_IMAGE_RESOLUTION,
|
| 1164 |
+
"smart_sampling_default": USE_SMART_SAMPLING
|
| 1165 |
+
},
|
| 1166 |
+
"environment": {
|
| 1167 |
+
"host": HOST,
|
| 1168 |
+
"port": PORT
|
| 1169 |
+
}
|
| 1170 |
+
}
|
| 1171 |
|
| 1172 |
|
| 1173 |
+
@app.get("/")
|
| 1174 |
+
async def root():
|
| 1175 |
+
"""Root endpoint."""
|
| 1176 |
return {
|
| 1177 |
+
"name": "Invoice Splitter API",
|
| 1178 |
+
"version": "6.0.0 - Fixed Grouping Logic",
|
| 1179 |
+
"description": "Split PDF invoices with Azure Blob Storage - Splits on invoice number change",
|
| 1180 |
+
"features": {
|
| 1181 |
+
"parallel_processing": f"Up to {MAX_PARALLEL_GEMINI_CALLS} concurrent Gemini API calls",
|
| 1182 |
+
"smart_sampling": "Optional fast mode for large PDFs (~5-10x faster)",
|
| 1183 |
+
"optimized_prompts": "Faster Gemini responses",
|
| 1184 |
+
"reduced_resolution": f"Image processing at {GEMINI_IMAGE_RESOLUTION}x for speed",
|
| 1185 |
+
"no_aggressive_filtering": "Keeps all extracted invoice numbers (fixed bug)"
|
| 1186 |
+
},
|
| 1187 |
+
"folder_structure": {
|
| 1188 |
+
"format": "POD/{batch_id}/{filename}/Raw|Splitted/",
|
| 1189 |
+
"raw_folder": "Contains original uploaded PDF",
|
| 1190 |
+
"split_folder": "Contains individual split invoice PDFs"
|
| 1191 |
+
},
|
| 1192 |
+
"endpoints": {
|
| 1193 |
+
"split_invoices": "/split-invoices",
|
| 1194 |
+
"cleanup_batch": "/cleanup-batch/{batch_id}",
|
| 1195 |
+
"health": "/health"
|
| 1196 |
+
},
|
| 1197 |
+
"configuration": {
|
| 1198 |
+
"gemini_configured": bool(GEMINI_API_KEY),
|
| 1199 |
+
"azure_configured": bool(AZURE_STORAGE_CONNECTION_STRING or (AZURE_STORAGE_ACCOUNT_NAME and AZURE_STORAGE_ACCOUNT_KEY)),
|
| 1200 |
+
"environment_ready": validate_configuration()
|
| 1201 |
+
}
|
| 1202 |
}
|
| 1203 |
|
| 1204 |
|
| 1205 |
if __name__ == "__main__":
|
| 1206 |
import uvicorn
|
| 1207 |
+
|
| 1208 |
+
print("\n" + "="*70)
|
| 1209 |
+
print("π Invoice Splitter API - v6.0 FIXED (Hugging Face)")
|
| 1210 |
+
print("="*70)
|
| 1211 |
+
|
| 1212 |
+
# Validate configuration
|
| 1213 |
+
config_valid = validate_configuration()
|
| 1214 |
+
|
| 1215 |
+
print(f"\nβ‘ Performance Features:")
|
| 1216 |
+
print(
|
| 1217 |
+
f" β’ Parallel Gemini API calls: {MAX_PARALLEL_GEMINI_CALLS} workers")
|
| 1218 |
+
print(f" β’ Image resolution: {GEMINI_IMAGE_RESOLUTION}x (optimized)")
|
| 1219 |
+
print(
|
| 1220 |
+
f" β’ Smart sampling: {'Enabled' if USE_SMART_SAMPLING else 'Disabled'} (optional)")
|
| 1221 |
+
print(f" β’ Expected speed: 5-10x faster for image PDFs")
|
| 1222 |
+
|
| 1223 |
+
print(f"\nπ§ Bug Fixes:")
|
| 1224 |
+
print(f" β’ β
Removed aggressive frequency filtering")
|
| 1225 |
+
print(f" β’ β
Splits on every invoice number change")
|
| 1226 |
+
print(f" β’ β
Keeps all extracted invoice numbers")
|
| 1227 |
+
print(f" β’ β
Added detailed debug logging")
|
| 1228 |
+
|
| 1229 |
+
print(f"\nπ Folder Structure:")
|
| 1230 |
+
print(f" {ROOT_FOLDER}/{{batch_id}}/{{filename}}/")
|
| 1231 |
+
print(f" βββ Raw/ (original PDF)")
|
| 1232 |
+
print(f" βββ Splitted/ (split invoices)")
|
| 1233 |
+
print(f"\nπ¦ Azure Configuration:")
|
| 1234 |
+
print(f" Account: {AZURE_STORAGE_ACCOUNT_NAME or 'Not set'}")
|
| 1235 |
+
print(f" Container: {AZURE_CONTAINER_NAME}")
|
| 1236 |
+
|
| 1237 |
+
if get_blob_service_client():
|
| 1238 |
+
print(f" β
Azure Blob Storage: Connected")
|
| 1239 |
+
else:
|
| 1240 |
+
print(f" β οΈ Azure Blob Storage: Not configured")
|
| 1241 |
+
|
| 1242 |
+
if get_gemini_model():
|
| 1243 |
+
print(f" β
Gemini AI: Connected (gemini-2.5-flash)")
|
| 1244 |
+
else:
|
| 1245 |
+
print(f" β οΈ Gemini AI: Not configured")
|
| 1246 |
+
|
| 1247 |
+
print(f"\nπ Server Configuration:")
|
| 1248 |
+
print(f" Host: {HOST}")
|
| 1249 |
+
print(f" Port: {PORT}")
|
| 1250 |
+
|
| 1251 |
+
if not config_valid:
|
| 1252 |
+
print(f"\nβ οΈ WARNING: Some credentials are missing!")
|
| 1253 |
+
print(f" For Hugging Face deployment:")
|
| 1254 |
+
print(f" 1. Go to your Space Settings > Repository secrets")
|
| 1255 |
+
print(f" 2. Add the following secrets:")
|
| 1256 |
+
print(f" - GEMINI_API_KEY")
|
| 1257 |
+
print(f" - AZURE_STORAGE_CONNECTION_STRING (or)")
|
| 1258 |
+
print(f" - AZURE_STORAGE_ACCOUNT_NAME + AZURE_STORAGE_ACCOUNT_KEY")
|
| 1259 |
+
|
| 1260 |
+
print("\n" + "="*70 + "\n")
|
| 1261 |
|
| 1262 |
uvicorn.run(
|
| 1263 |
app,
|
| 1264 |
+
host=HOST,
|
| 1265 |
+
port=PORT,
|
| 1266 |
workers=1,
|
| 1267 |
+
timeout_keep_alive=600
|
|
|
|
| 1268 |
)
|