Spaces:
No application file
No application file
Update app.py
Browse files
app.py
CHANGED
|
@@ -495,45 +495,21 @@ def display_pdf_preview(file_path, model_name):
|
|
| 495 |
"""Display PDF preview inline"""
|
| 496 |
import os
|
| 497 |
|
| 498 |
-
# Extract just the filename from the
|
| 499 |
-
#
|
| 500 |
normalized_path = file_path.replace('\\', '/')
|
| 501 |
-
pdf_filename =
|
| 502 |
-
|
| 503 |
-
#
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
# Debug info
|
| 507 |
-
st.write(f"
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
|
| 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 |
-
|
| 526 |
-
# Try each possible path
|
| 527 |
-
for test_path in possible_paths:
|
| 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(
|
| 537 |
pdf_data = f.read()
|
| 538 |
|
| 539 |
file_size_mb = len(pdf_data) / (1024 * 1024)
|
|
@@ -563,10 +539,7 @@ def display_pdf_preview(file_path, model_name):
|
|
| 563 |
st.markdown("---")
|
| 564 |
|
| 565 |
if file_size_mb < 10:
|
| 566 |
-
# Convert to base64
|
| 567 |
base64_pdf = base64.b64encode(pdf_data).decode('utf-8')
|
| 568 |
-
|
| 569 |
-
# Create HTML for inline PDF display
|
| 570 |
pdf_display = f"""
|
| 571 |
<iframe
|
| 572 |
src="data:application/pdf;base64,{base64_pdf}"
|
|
@@ -576,44 +549,26 @@ def display_pdf_preview(file_path, model_name):
|
|
| 576 |
style="border: 2px solid #4CAF50; border-radius: 8px;">
|
| 577 |
</iframe>
|
| 578 |
"""
|
| 579 |
-
|
| 580 |
st.markdown(pdf_display, unsafe_allow_html=True)
|
| 581 |
-
|
| 582 |
else:
|
| 583 |
st.warning("PDF file is too large for inline viewing. Please use the download button.")
|
| 584 |
|
| 585 |
except Exception as e:
|
| 586 |
st.error(f"Error reading PDF: {str(e)}")
|
| 587 |
-
st.info(f"Attempted to read from: {actual_path}")
|
| 588 |
else:
|
| 589 |
st.error("❌ PDF file not found.")
|
| 590 |
-
st.info(f"
|
| 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 |
-
#
|
| 613 |
-
|
| 614 |
-
|
| 615 |
-
|
| 616 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 617 |
|
| 618 |
def get_high_accuracy_models(limit=8):
|
| 619 |
"""Get models with highest extraction quality"""
|
|
|
|
| 495 |
"""Display PDF preview inline"""
|
| 496 |
import os
|
| 497 |
|
| 498 |
+
# Extract just the filename from the database path
|
| 499 |
+
# Handle both forward and backslashes
|
| 500 |
normalized_path = file_path.replace('\\', '/')
|
| 501 |
+
pdf_filename = normalized_path.split('/')[-1] # Get last part after final slash
|
| 502 |
+
|
| 503 |
+
# The PDFs are in the 'pdfs' subdirectory
|
| 504 |
+
pdf_path = os.path.join('pdfs', pdf_filename)
|
| 505 |
+
|
| 506 |
+
# Debug info - remove these lines once working
|
| 507 |
+
# st.write(f"Debug - Looking for: {pdf_path}")
|
| 508 |
+
# st.write(f"Debug - File exists: {os.path.exists(pdf_path)}")
|
| 509 |
+
|
| 510 |
+
if os.path.exists(pdf_path):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 511 |
try:
|
| 512 |
+
with open(pdf_path, "rb") as f:
|
| 513 |
pdf_data = f.read()
|
| 514 |
|
| 515 |
file_size_mb = len(pdf_data) / (1024 * 1024)
|
|
|
|
| 539 |
st.markdown("---")
|
| 540 |
|
| 541 |
if file_size_mb < 10:
|
|
|
|
| 542 |
base64_pdf = base64.b64encode(pdf_data).decode('utf-8')
|
|
|
|
|
|
|
| 543 |
pdf_display = f"""
|
| 544 |
<iframe
|
| 545 |
src="data:application/pdf;base64,{base64_pdf}"
|
|
|
|
| 549 |
style="border: 2px solid #4CAF50; border-radius: 8px;">
|
| 550 |
</iframe>
|
| 551 |
"""
|
|
|
|
| 552 |
st.markdown(pdf_display, unsafe_allow_html=True)
|
|
|
|
| 553 |
else:
|
| 554 |
st.warning("PDF file is too large for inline viewing. Please use the download button.")
|
| 555 |
|
| 556 |
except Exception as e:
|
| 557 |
st.error(f"Error reading PDF: {str(e)}")
|
|
|
|
| 558 |
else:
|
| 559 |
st.error("❌ PDF file not found.")
|
| 560 |
+
st.info(f"Looking for: {pdf_filename} in /pdfs folder")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 561 |
|
| 562 |
+
# If you're still having issues, uncomment these debug lines:
|
| 563 |
+
# st.write("Current directory:", os.getcwd())
|
| 564 |
+
# st.write("Contents of current directory:", os.listdir('.'))
|
| 565 |
+
# if os.path.exists('pdfs'):
|
| 566 |
+
# pdf_list = os.listdir('pdfs')
|
| 567 |
+
# st.write(f"Number of PDFs in /pdfs: {len(pdf_list)}")
|
| 568 |
+
# # Show first few PDFs that might match
|
| 569 |
+
# matching = [p for p in pdf_list if model_name in p]
|
| 570 |
+
# if matching:
|
| 571 |
+
# st.write("Possible matches:", matching[:5])
|
| 572 |
|
| 573 |
def get_high_accuracy_models(limit=8):
|
| 574 |
"""Get models with highest extraction quality"""
|