Spaces:
No application file
No application file
Update app.py
Browse files
app.py
CHANGED
|
@@ -495,17 +495,31 @@ def display_pdf_preview(file_path, model_name):
|
|
| 495 |
"""Display PDF preview inline"""
|
| 496 |
import os
|
| 497 |
|
| 498 |
-
# Extract just the filename from the Windows path
|
| 499 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 500 |
|
| 501 |
# Try multiple possible locations
|
| 502 |
possible_paths = [
|
| 503 |
os.path.join("pdfs", pdf_filename), # /pdfs/filename.pdf
|
| 504 |
os.path.join(".", "pdfs", pdf_filename), # ./pdfs/filename.pdf
|
| 505 |
-
pdf_filename, #
|
| 506 |
-
file_path # Original path (unlikely to work)
|
| 507 |
]
|
| 508 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 509 |
pdf_found = False
|
| 510 |
actual_path = None
|
| 511 |
|
|
@@ -514,13 +528,13 @@ def display_pdf_preview(file_path, model_name):
|
|
| 514 |
if os.path.exists(test_path):
|
| 515 |
pdf_found = True
|
| 516 |
actual_path = test_path
|
|
|
|
| 517 |
break
|
| 518 |
|
| 519 |
-
if pdf_found:
|
| 520 |
try:
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
pdf_data = f.read()
|
| 524 |
|
| 525 |
file_size_mb = len(pdf_data) / (1024 * 1024)
|
| 526 |
|
|
@@ -531,7 +545,7 @@ def display_pdf_preview(file_path, model_name):
|
|
| 531 |
st.download_button(
|
| 532 |
"📥 Download PDF",
|
| 533 |
data=pdf_data,
|
| 534 |
-
file_name=
|
| 535 |
mime="application/pdf",
|
| 536 |
use_container_width=True,
|
| 537 |
type="primary"
|
|
@@ -573,16 +587,33 @@ def display_pdf_preview(file_path, model_name):
|
|
| 573 |
st.info(f"Attempted to read from: {actual_path}")
|
| 574 |
else:
|
| 575 |
st.error("❌ PDF file not found.")
|
| 576 |
-
st.info(f"
|
| 577 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 578 |
|
| 579 |
# Debug information
|
| 580 |
-
with st.expander("
|
| 581 |
-
st.write(f"**Original path
|
| 582 |
st.write(f"**Extracted filename:** {pdf_filename}")
|
| 583 |
-
st.write(f"**
|
| 584 |
-
for path in possible_paths:
|
| 585 |
-
st.write(f"- {path}")
|
| 586 |
|
| 587 |
def get_high_accuracy_models(limit=8):
|
| 588 |
"""Get models with highest extraction quality"""
|
|
|
|
| 495 |
"""Display PDF preview inline"""
|
| 496 |
import os
|
| 497 |
|
| 498 |
+
# Extract just the filename from the Windows path, ignoring subdirectories
|
| 499 |
+
# Convert backslashes to forward slashes first
|
| 500 |
+
normalized_path = file_path.replace('\\', '/')
|
| 501 |
+
pdf_filename = os.path.basename(normalized_path)
|
| 502 |
+
|
| 503 |
+
# Clean up the filename (remove any extra spaces or special characters if needed)
|
| 504 |
+
pdf_filename = pdf_filename.strip()
|
| 505 |
+
|
| 506 |
+
# Debug info
|
| 507 |
+
st.write(f"🔍 Looking for PDF: {pdf_filename}")
|
| 508 |
|
| 509 |
# Try multiple possible locations
|
| 510 |
possible_paths = [
|
| 511 |
os.path.join("pdfs", pdf_filename), # /pdfs/filename.pdf
|
| 512 |
os.path.join(".", "pdfs", pdf_filename), # ./pdfs/filename.pdf
|
| 513 |
+
f"pdfs/{pdf_filename}", # Direct path string
|
|
|
|
| 514 |
]
|
| 515 |
|
| 516 |
+
# Also try without spaces in filename (in case of naming issues)
|
| 517 |
+
pdf_filename_no_spaces = pdf_filename.replace(' ', '_')
|
| 518 |
+
possible_paths.extend([
|
| 519 |
+
os.path.join("pdfs", pdf_filename_no_spaces),
|
| 520 |
+
f"pdfs/{pdf_filename_no_spaces}"
|
| 521 |
+
])
|
| 522 |
+
|
| 523 |
pdf_found = False
|
| 524 |
actual_path = None
|
| 525 |
|
|
|
|
| 528 |
if os.path.exists(test_path):
|
| 529 |
pdf_found = True
|
| 530 |
actual_path = test_path
|
| 531 |
+
st.success(f"✅ Found PDF at: {test_path}")
|
| 532 |
break
|
| 533 |
|
| 534 |
+
if pdf_found and actual_path is not None:
|
| 535 |
try:
|
| 536 |
+
with open(actual_path, "rb") as f:
|
| 537 |
+
pdf_data = f.read()
|
|
|
|
| 538 |
|
| 539 |
file_size_mb = len(pdf_data) / (1024 * 1024)
|
| 540 |
|
|
|
|
| 545 |
st.download_button(
|
| 546 |
"📥 Download PDF",
|
| 547 |
data=pdf_data,
|
| 548 |
+
file_name=pdf_filename,
|
| 549 |
mime="application/pdf",
|
| 550 |
use_container_width=True,
|
| 551 |
type="primary"
|
|
|
|
| 587 |
st.info(f"Attempted to read from: {actual_path}")
|
| 588 |
else:
|
| 589 |
st.error("❌ PDF file not found.")
|
| 590 |
+
st.info(f"Expected filename: {pdf_filename}")
|
| 591 |
+
|
| 592 |
+
# List files in pdfs directory to help debug
|
| 593 |
+
try:
|
| 594 |
+
pdfs_dir = "pdfs"
|
| 595 |
+
if os.path.exists(pdfs_dir):
|
| 596 |
+
files_in_pdfs = os.listdir(pdfs_dir)
|
| 597 |
+
# Find similar filenames
|
| 598 |
+
similar_files = [f for f in files_in_pdfs if pdf_filename.lower()[:10] in f.lower()]
|
| 599 |
+
|
| 600 |
+
if similar_files:
|
| 601 |
+
st.warning("🔍 Similar files found in /pdfs folder:")
|
| 602 |
+
for f in similar_files[:5]: # Show max 5 similar files
|
| 603 |
+
st.write(f"- {f}")
|
| 604 |
+
else:
|
| 605 |
+
st.info("No similar files found. The PDF might not have been uploaded.")
|
| 606 |
+
else:
|
| 607 |
+
st.error("The /pdfs directory doesn't exist!")
|
| 608 |
+
|
| 609 |
+
except Exception as e:
|
| 610 |
+
st.error(f"Error listing PDF directory: {e}")
|
| 611 |
|
| 612 |
# Debug information
|
| 613 |
+
with st.expander("🔧 Debug Information"):
|
| 614 |
+
st.write(f"**Original path:** {file_path}")
|
| 615 |
st.write(f"**Extracted filename:** {pdf_filename}")
|
| 616 |
+
st.write(f"**Model:** {model_name}")
|
|
|
|
|
|
|
| 617 |
|
| 618 |
def get_high_accuracy_models(limit=8):
|
| 619 |
"""Get models with highest extraction quality"""
|