-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract_forms_server.py
More file actions
97 lines (81 loc) · 3.19 KB
/
Copy pathextract_forms_server.py
File metadata and controls
97 lines (81 loc) · 3.19 KB
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
from fastapi.middleware.cors import CORSMiddleware
import os
import asyncio
import requests
from io import BytesIO
from PyPDF2 import PdfReader, PdfWriter
from fastapi import FastAPI, HTTPException
from utils.s3_utils import list_s3_pdfs, fetch_pdf
from extract_forms.pdf_processing import extract_form_pages
from utils.mongo_utils import is_form_complete, mark_form_complete, get_forms
app = FastAPI()
origins = [
"http://localhost:8080",
"http://192.168.1.5:8080",
"https://tenderbharat.vercel.app",
"http://localhost:3000",
"https://www.bidindia.site",
"https://www.bidindia.co.in",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
async def process_single_tender(tender_id: str):
print(f"\n===============================")
print(f"▶ START tender: {tender_id}")
print(f"===============================")
report = {
"tender_id": tender_id,
"processed_docs": 0,
"skipped_docs": 0,
"scanned_pages": 0,
"regular_pages": 0,
"total_page_errors": 0,
"errors": [],
"forms": {}
}
s3_prefix = f"tender-documents/{tender_id}/"
print(f"📂 Fetching S3 PDFs from prefix: {s3_prefix}")
pdf_keys = await list_s3_pdfs(s3_prefix)
print(f"📄 Found {len(pdf_keys)} PDFs")
for pdf_key in pdf_keys:
document_name = os.path.basename(pdf_key)
print(f"📄 Document: {document_name}")
if await asyncio.to_thread(is_form_complete, tender_id, document_name):
print(f"⏩ Already processed, skipping")
report["skipped_docs"] += 1
continue
try:
pdf_bytes = await fetch_pdf(pdf_key)
form_pages, scanned_count, regular_count, page_errors = await extract_form_pages(pdf_bytes, document_name)
report["scanned_pages"] += scanned_count
report["regular_pages"] += regular_count
report["total_page_errors"] += page_errors
if page_errors > 3:
print(f"❌ Too many errors ({page_errors}), aborting PDF: {document_name}")
report["errors"].append(f"{document_name} aborted due to {page_errors} page errors")
continue
await asyncio.to_thread(mark_form_complete, tender_id, document_name, form_pages)
report["processed_docs"] += 1
if page_errors > 0:
report["errors"].append(f"{document_name} had {page_errors} page errors")
except Exception as e:
print(f"❌ Error processing {document_name}: {e}")
report["errors"].append(f"{document_name}: {str(e)}")
forms_data = await asyncio.to_thread(get_forms, tender_id)
report["forms"] = forms_data
print(f"\n✅ Finished tender {tender_id}")
print(f"📊 Report: {report}")
return report
@app.post("/process/{tender_id}")
async def route_process(tender_id: str):
print(f"\n🌐 API CALL → /process/{tender_id}")
try:
return await process_single_tender(tender_id)
except Exception as e:
print(f"❌ API ERROR: {e}")
raise HTTPException(status_code=500, detail=str(e))