Update app.py
Browse files
app.py
CHANGED
|
@@ -190,7 +190,7 @@ def run_yolo_detection_and_count(
|
|
| 190 |
|
| 191 |
|
| 192 |
# ============================================================================
|
| 193 |
-
# --- MAIN DOCUMENT PROCESSING FUNCTION ---
|
| 194 |
# ============================================================================
|
| 195 |
|
| 196 |
def run_single_pdf_preprocessing(pdf_path: str, temp_output_dir: str) -> Tuple[int, int, int, str, List[str]]:
|
|
@@ -227,6 +227,9 @@ def run_single_pdf_preprocessing(pdf_path: str, temp_output_dir: str) -> Tuple[i
|
|
| 227 |
mat = fitz.Matrix(SCALE_FACTOR, SCALE_FACTOR)
|
| 228 |
all_equation_images = [] # Stores file paths (strings) for Gradio gallery
|
| 229 |
|
|
|
|
|
|
|
|
|
|
| 230 |
for page_num_0_based in range(doc.page_count):
|
| 231 |
fitz_page = doc.load_page(page_num_0_based)
|
| 232 |
page_num = page_num_0_based + 1
|
|
@@ -257,7 +260,11 @@ def run_single_pdf_preprocessing(pdf_path: str, temp_output_dir: str) -> Tuple[i
|
|
| 257 |
|
| 258 |
# Save to a temporary file path
|
| 259 |
img_bytes = eq_pix.tobytes("png")
|
| 260 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
output_path = os.path.join(temp_output_dir, filename)
|
| 262 |
|
| 263 |
with open(output_path, 'wb') as f:
|
|
@@ -270,7 +277,7 @@ def run_single_pdf_preprocessing(pdf_path: str, temp_output_dir: str) -> Tuple[i
|
|
| 270 |
|
| 271 |
doc.close()
|
| 272 |
|
| 273 |
-
# Final Report Generation
|
| 274 |
report = (
|
| 275 |
f"✅ **YOLO Counting Complete!**\n\n"
|
| 276 |
f"**1) Total Pages Detected in PDF:** **{total_pages}**\n"
|
|
@@ -283,22 +290,18 @@ def run_single_pdf_preprocessing(pdf_path: str, temp_output_dir: str) -> Tuple[i
|
|
| 283 |
|
| 284 |
|
| 285 |
# ============================================================================
|
| 286 |
-
# --- GRADIO INTERFACE FUNCTION
|
| 287 |
# ============================================================================
|
| 288 |
|
| 289 |
def gradio_process_pdf(pdf_file) -> Tuple[str, str, str, str, List[str]]:
|
| 290 |
"""
|
| 291 |
Gradio wrapper function to handle file upload, manage temporary directory, and return file paths.
|
| 292 |
-
|
| 293 |
-
The cleanup block is REMOVED to allow Gradio's front-end to access the images
|
| 294 |
-
before the files are deleted. Gradio will eventually handle the cleanup.
|
| 295 |
"""
|
| 296 |
if pdf_file is None:
|
| 297 |
return "N/A", "N/A", "N/A", "Please upload a PDF file.", []
|
| 298 |
|
| 299 |
pdf_path = pdf_file.name
|
| 300 |
-
# Create temp directory
|
| 301 |
-
temp_output_dir = tempfile.mkdtemp()
|
| 302 |
|
| 303 |
try:
|
| 304 |
# Run the core logic, passing the temp directory
|
|
|
|
| 190 |
|
| 191 |
|
| 192 |
# ============================================================================
|
| 193 |
+
# --- MAIN DOCUMENT PROCESSING FUNCTION (Fixed for unique filenames) ---
|
| 194 |
# ============================================================================
|
| 195 |
|
| 196 |
def run_single_pdf_preprocessing(pdf_path: str, temp_output_dir: str) -> Tuple[int, int, int, str, List[str]]:
|
|
|
|
| 227 |
mat = fitz.Matrix(SCALE_FACTOR, SCALE_FACTOR)
|
| 228 |
all_equation_images = [] # Stores file paths (strings) for Gradio gallery
|
| 229 |
|
| 230 |
+
# NEW LOCAL COUNTER: Tracks total equations processed for unique filename creation
|
| 231 |
+
equation_save_count = 0
|
| 232 |
+
|
| 233 |
for page_num_0_based in range(doc.page_count):
|
| 234 |
fitz_page = doc.load_page(page_num_0_based)
|
| 235 |
page_num = page_num_0_based + 1
|
|
|
|
| 260 |
|
| 261 |
# Save to a temporary file path
|
| 262 |
img_bytes = eq_pix.tobytes("png")
|
| 263 |
+
|
| 264 |
+
# FIX APPLIED: Increment and use local counter for unique filename
|
| 265 |
+
equation_save_count += 1
|
| 266 |
+
filename = f"eq_{equation_save_count}_p{page_num}.png"
|
| 267 |
+
|
| 268 |
output_path = os.path.join(temp_output_dir, filename)
|
| 269 |
|
| 270 |
with open(output_path, 'wb') as f:
|
|
|
|
| 277 |
|
| 278 |
doc.close()
|
| 279 |
|
| 280 |
+
# Final Report Generation (GLOBAL_EQUATION_COUNT is correct here)
|
| 281 |
report = (
|
| 282 |
f"✅ **YOLO Counting Complete!**\n\n"
|
| 283 |
f"**1) Total Pages Detected in PDF:** **{total_pages}**\n"
|
|
|
|
| 290 |
|
| 291 |
|
| 292 |
# ============================================================================
|
| 293 |
+
# --- GRADIO INTERFACE FUNCTION ---
|
| 294 |
# ============================================================================
|
| 295 |
|
| 296 |
def gradio_process_pdf(pdf_file) -> Tuple[str, str, str, str, List[str]]:
|
| 297 |
"""
|
| 298 |
Gradio wrapper function to handle file upload, manage temporary directory, and return file paths.
|
|
|
|
|
|
|
|
|
|
| 299 |
"""
|
| 300 |
if pdf_file is None:
|
| 301 |
return "N/A", "N/A", "N/A", "Please upload a PDF file.", []
|
| 302 |
|
| 303 |
pdf_path = pdf_file.name
|
| 304 |
+
temp_output_dir = tempfile.mkdtemp() # Create temp directory
|
|
|
|
| 305 |
|
| 306 |
try:
|
| 307 |
# Run the core logic, passing the temp directory
|