Fix startup and Gemini provider handling
This commit is contained in:
+15
-4
@@ -6,25 +6,35 @@ from pathlib import Path
|
||||
|
||||
from config import UPLOAD_DIR
|
||||
|
||||
# Maximum pages we will extract from a single PDF upload.
|
||||
# Beyond this limit pages are silently dropped to prevent runaway queue growth.
|
||||
MAX_PDF_PAGES = 200
|
||||
|
||||
|
||||
def pdf_to_images(pdf_bytes: bytes, original_name: str) -> list[dict]:
|
||||
"""
|
||||
Convert a PDF to individual page images.
|
||||
Returns list of dicts: [{"image_path": "relative/path.png", "page_number": 1}, ...]
|
||||
|
||||
Each page is rendered at 250 DPI — sufficient for Arabic OCR while keeping
|
||||
file sizes manageable. For scanned/image-only PDFs PyMuPDF simply renders
|
||||
the embedded raster; no additional image extraction step is needed.
|
||||
|
||||
Returns list of dicts: [{"image_path": "relative/path.png", "page_number": 1, ...}, ...]
|
||||
"""
|
||||
import fitz # PyMuPDF
|
||||
|
||||
doc = fitz.open(stream=pdf_bytes, filetype="pdf")
|
||||
total_pages = len(doc)
|
||||
group_id = uuid.uuid4().hex
|
||||
today = date.today().isoformat()
|
||||
dest_dir = Path(UPLOAD_DIR) / today
|
||||
dest_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
pages = []
|
||||
for page_num in range(len(doc)):
|
||||
for page_num in range(min(total_pages, MAX_PDF_PAGES)):
|
||||
page = doc[page_num]
|
||||
# Render at 200 DPI for good OCR quality
|
||||
pix = page.get_pixmap(dpi=200)
|
||||
# 250 DPI gives sharper Arabic text while staying under ~2 MB per PNG
|
||||
pix = page.get_pixmap(dpi=250)
|
||||
|
||||
filename = f"{group_id}_p{page_num + 1}.png"
|
||||
dest = dest_dir / filename
|
||||
@@ -34,6 +44,7 @@ def pdf_to_images(pdf_bytes: bytes, original_name: str) -> list[dict]:
|
||||
"image_path": str(Path(today) / filename),
|
||||
"page_number": page_num + 1,
|
||||
"pdf_group_id": group_id,
|
||||
"total_pages": total_pages,
|
||||
})
|
||||
|
||||
doc.close()
|
||||
|
||||
Reference in New Issue
Block a user