redxican commited on
Commit
548143f
·
verified ·
1 Parent(s): 0f40775

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -34
app.py CHANGED
@@ -492,63 +492,110 @@ def export_bookmarked_models():
492
  return df.to_csv(index=False)
493
 
494
  def display_pdf_preview(file_path, model_name):
495
- """Display PDF using Google Docs Viewer - most reliable method"""
496
  import os
 
 
497
 
498
  # Extract filename
499
  normalized_path = file_path.replace('\\', '/')
500
  pdf_filename = normalized_path.split('/')[-1]
501
 
502
- # Create direct URL
503
- pdf_url = f"https://huggingface.co/spaces/TurboAir/TurboAirViewer/resolve/main/pdfs/{pdf_filename}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
504
 
505
  # Control buttons
506
  col1, col2, col3 = st.columns([2, 1, 1])
507
 
508
  with col1:
509
- st.markdown(f'<a href="{pdf_url}" download="{pdf_filename}"><button style="width: 100%; padding: 0.5rem; background-color: #FF4B4B; color: white; border: none; border-radius: 5px; cursor: pointer;">📥 Download PDF</button></a>', unsafe_allow_html=True)
 
 
 
 
 
 
 
510
 
511
  with col2:
512
- st.success("✅ PDF ready")
513
 
514
  with col3:
515
  if st.button("❌ Close Preview", use_container_width=True):
516
  st.session_state[f'show_pdf_{model_name}'] = False
517
  st.rerun()
518
 
519
- # Display PDF - NO GREEN BORDER
520
  st.markdown("---")
521
 
522
- # Method 1: Direct embed (try this first)
523
- pdf_viewer = f"""
524
- <embed
525
- src="{pdf_url}#view=FitH&toolbar=0"
526
- width="100%"
527
- height="900px"
528
- type="application/pdf">
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
529
  """
530
 
531
- # Method 2: If embed doesn't work, use iframe
532
- # pdf_viewer = f"""
533
- # <iframe
534
- # src="{pdf_url}#view=FitH&toolbar=0"
535
- # width="100%"
536
- # height="900px"
537
- # frameborder="0">
538
- # </iframe>
539
- # """
540
-
541
- # Method 3: If neither works, use Google Docs
542
- # pdf_viewer = f"""
543
- # <iframe
544
- # src="https://docs.google.com/viewer?url={pdf_url}&embedded=true"
545
- # width="100%"
546
- # height="900px"
547
- # frameborder="0">
548
- # </iframe>
549
- # """
550
-
551
- st.markdown(pdf_viewer, unsafe_allow_html=True)
552
 
553
  def get_high_accuracy_models(limit=8):
554
  """Get models with highest extraction quality"""
 
492
  return df.to_csv(index=False)
493
 
494
  def display_pdf_preview(file_path, model_name):
495
+ """Display PDF - works on Hugging Face with Git LFS"""
496
  import os
497
+ import requests
498
+ import base64
499
 
500
  # Extract filename
501
  normalized_path = file_path.replace('\\', '/')
502
  pdf_filename = normalized_path.split('/')[-1]
503
 
504
+ # Check if running locally or on Hugging Face
505
+ is_local = not os.environ.get('SPACE_ID')
506
+
507
+ if is_local:
508
+ # Local file system access
509
+ pdf_path = os.path.join('pdfs', pdf_filename)
510
+ if os.path.exists(pdf_path):
511
+ with open(pdf_path, 'rb') as f:
512
+ pdf_data = f.read()
513
+ file_size_mb = len(pdf_data) / (1024 * 1024)
514
+ else:
515
+ st.error(f"PDF not found locally: {pdf_path}")
516
+ return
517
+ else:
518
+ # Hugging Face - need to download from URL
519
+ pdf_url = f"https://huggingface.co/spaces/TurboAir/TurboAirViewer/resolve/main/pdfs/{pdf_filename}"
520
+
521
+ try:
522
+ with st.spinner("Loading PDF..."):
523
+ response = requests.get(pdf_url, timeout=30)
524
+ response.raise_for_status()
525
+ pdf_data = response.content
526
+ file_size_mb = len(pdf_data) / (1024 * 1024)
527
+
528
+ # Verify we got actual PDF data, not Git LFS pointer
529
+ if len(pdf_data) < 1000:
530
+ st.error("PDF file is too small - might be Git LFS pointer")
531
+ # Use external viewer as fallback
532
+ st.markdown(f"[📄 Open PDF in new tab]({pdf_url})")
533
+ return
534
+
535
+ except Exception as e:
536
+ st.error(f"Error loading PDF: {str(e)}")
537
+ # Fallback to external link
538
+ st.markdown(f"[📄 Open PDF in new tab]({pdf_url})")
539
+ return
540
 
541
  # Control buttons
542
  col1, col2, col3 = st.columns([2, 1, 1])
543
 
544
  with col1:
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"
552
+ )
553
 
554
  with col2:
555
+ st.success(f"✅ PDF ready ({file_size_mb:.1f} MB)")
556
 
557
  with col3:
558
  if st.button("❌ Close Preview", use_container_width=True):
559
  st.session_state[f'show_pdf_{model_name}'] = False
560
  st.rerun()
561
 
 
562
  st.markdown("---")
563
 
564
+ # Display PDF using base64 encoding (works everywhere)
565
+ base64_pdf = base64.b64encode(pdf_data).decode('utf-8')
566
+
567
+ # Try multiple display methods
568
+ pdf_display = f"""
569
+ <style>
570
+ .pdf-viewer {{
571
+ width: 100%;
572
+ height: 900px;
573
+ border: none;
574
+ }}
575
+ </style>
576
+
577
+ <!-- Method 1: Object with embed fallback -->
578
+ <object data="data:application/pdf;base64,{base64_pdf}"
579
+ type="application/pdf"
580
+ class="pdf-viewer">
581
+ <embed src="data:application/pdf;base64,{base64_pdf}"
582
+ type="application/pdf"
583
+ class="pdf-viewer" />
584
+ <p>PDF preview not available. Please use the download button above.</p>
585
+ </object>
586
  """
587
 
588
+ st.markdown(pdf_display, unsafe_allow_html=True)
589
+
590
+ # If base64 doesn't work, show alternative
591
+ st.markdown("---")
592
+ st.info("💡 If PDF doesn't display above, you can:")
593
+ col1, col2 = st.columns(2)
594
+ with col1:
595
+ st.markdown("• Use the Download button above")
596
+ with col2:
597
+ if not is_local:
598
+ st.markdown(f"• [Open in new tab ↗]({pdf_url})")
 
 
 
 
 
 
 
 
 
 
599
 
600
  def get_high_accuracy_models(limit=8):
601
  """Get models with highest extraction quality"""