redxican commited on
Commit
97f6d6b
·
verified ·
1 Parent(s): 91400f8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -16
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
- pdf_filename = os.path.basename(file_path)
 
 
 
 
 
 
 
 
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, # Just filename in root
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
- if actual_path:
522
- with open(actual_path, "rb") as f:
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=f"{model_name}_spec_sheet.pdf",
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"Looking for: {pdf_filename}")
577
- st.info("The PDF may not have been uploaded yet or the filename may be different.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
578
 
579
  # Debug information
580
- with st.expander("🔍 Debug Information"):
581
- st.write(f"**Original path in database:** {file_path}")
582
  st.write(f"**Extracted filename:** {pdf_filename}")
583
- st.write(f"**Searched locations:**")
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"""