compression-theory-viz / utils /pdf_utils.py
rufimelo's picture
feat: add streamlit visualization app with prompts, trajectories, and outputs
d14dfc9
Raw
History Blame Contribute Delete
1.09 kB
import os
from pathlib import Path
from PyPDF2 import PdfReader
def extract_text_from_pdf(pdf_path: str) -> str:
"""
Extract all text from a PDF file.
Args:
pdf_path: Path to the PDF file
Returns:
Extracted text as a single string
"""
if not os.path.exists(pdf_path):
raise FileNotFoundError(f"PDF not found: {pdf_path}")
text = []
try:
with open(pdf_path, "rb") as file:
pdf_reader = PdfReader(file)
for page in pdf_reader.pages:
text.append(page.extract_text())
except Exception as e:
raise ValueError(f"Error reading PDF {pdf_path}: {e}")
return "\n".join(text)
def list_papers(papers_dir: str = "papers") -> list:
"""
List all PDF files in the papers directory.
Args:
papers_dir: Directory containing test papers
Returns:
List of absolute paths to PDF files
"""
papers_path = Path(papers_dir)
if not papers_path.exists():
return []
return [str(f) for f in papers_path.glob("*.pdf") if f.is_file()]