- 
                Notifications
    You must be signed in to change notification settings 
- Fork 778
          uv workspace layout, broke out PyMuPDF
          #1021
        
          New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            3 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              Large diffs are not rendered by default.
      
      Oops, something went wrong.
      
    
  Large diffs are not rendered by default.
      
      Oops, something went wrong.
      
    
  
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # paper-qa-pymupdf | ||
|  | ||
| [](https://github.com/Future-House/paper-qa/tree/main/packages/paper-qa-pymupdf) | ||
| [](https://badge.fury.io/py/paper-qa-pymupdf) | ||
| [](https://github.com/Future-House/paper-qa) | ||
|  | ||
|  | ||
|  | ||
| PDF reading code backed by | ||
| [PyMuPDF](https://github.com/pymupdf/PyMuPDF). | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| [build-system] | ||
| build-backend = "setuptools.build_meta" | ||
| requires = ["setuptools>=64", "setuptools_scm>=8"] | ||
|  | ||
| [project] | ||
| authors = [ | ||
| {email = "hello@futurehouse.org", name = "FutureHouse technical staff"}, | ||
| ] | ||
| classifiers = [ | ||
| "Intended Audience :: Developers", | ||
| "License :: OSI Approved :: GNU Affero General Public License v3", | ||
| "Operating System :: OS Independent", | ||
| "Programming Language :: Python :: 3 :: Only", | ||
| "Programming Language :: Python :: 3.11", | ||
| "Programming Language :: Python :: 3.12", | ||
| "Programming Language :: Python :: 3.13", | ||
| "Programming Language :: Python", | ||
| "Topic :: Scientific/Engineering :: Artificial Intelligence", | ||
| ] | ||
| dependencies = [ | ||
| "PyMuPDF>=1.24.12", # For pymupdf.set_messages addition | ||
| "paper-qa", | ||
| ] | ||
| description = "PaperQA readers implemented using PyMuPDF" | ||
| dynamic = ["version"] | ||
| license = {file = "LICENSE"} | ||
| maintainers = [ | ||
| {email = "jamesbraza@gmail.com", name = "James Braza"}, | ||
| {email = "michael.skarlinski@gmail.com", name = "Michael Skarlinski"}, | ||
| {email = "white.d.andrew@gmail.com", name = "Andrew White"}, | ||
| ] | ||
| name = "paper-qa-pymupdf" | ||
| readme = "README.md" | ||
| requires-python = ">=3.11" | ||
|  | ||
| [tool.ruff] | ||
| extend = "../../pyproject.toml" | ||
|  | ||
| [tool.setuptools.packages.find] | ||
| where = ["src"] | ||
|  | ||
| [tool.setuptools_scm] | ||
| root = "../.." | ||
| version_file = "src/paperqa_pymupdf/version.py" | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| from .reader import BLOCK_TEXT_INDEX, parse_pdf_to_pages, setup_pymupdf_python_logging | ||
|  | ||
| __all__ = [ | ||
| "BLOCK_TEXT_INDEX", | ||
| "parse_pdf_to_pages", | ||
| "setup_pymupdf_python_logging", | ||
| ] | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import os | ||
|  | ||
| import pymupdf | ||
| from paperqa.types import ParsedMetadata, ParsedText | ||
| from paperqa.utils import ImpossibleParsingError | ||
| from paperqa.version import __version__ as pqa_version | ||
|  | ||
|  | ||
| def setup_pymupdf_python_logging() -> None: | ||
| """ | ||
| Configure PyMuPDF to use Python logging. | ||
|  | ||
| SEE: https://pymupdf.readthedocs.io/en/latest/app3.html#diagnostics | ||
| """ | ||
| pymupdf.set_messages(pylogging=True) | ||
|  | ||
|  | ||
| BLOCK_TEXT_INDEX = 4 | ||
|  | ||
|  | ||
| def parse_pdf_to_pages( | ||
| path: str | os.PathLike, | ||
| page_size_limit: int | None = None, | ||
| use_block_parsing: bool = False, | ||
| **_, | ||
| ) -> ParsedText: | ||
|  | ||
| with pymupdf.open(path) as file: | ||
| pages: dict[str, str] = {} | ||
| total_length = 0 | ||
|  | ||
| for i in range(file.page_count): | ||
| try: | ||
| page = file.load_page(i) | ||
| except pymupdf.mupdf.FzErrorFormat as exc: | ||
| raise ImpossibleParsingError( | ||
| f"Page loading via {pymupdf.__name__} failed on page {i} of" | ||
| f" {file.page_count} for the PDF at path {path}, likely this PDF" | ||
| " file is corrupt." | ||
| ) from exc | ||
|  | ||
| if use_block_parsing: | ||
| # NOTE: this block-based parsing appears to be better, but until | ||
| # fully validated on 1+ benchmarks, it's considered experimental | ||
|  | ||
| # Extract text blocks from the page | ||
| # Note: sort=False is important to preserve the order of text blocks | ||
| # as they appear in the PDF | ||
| blocks = page.get_text("blocks", sort=False) | ||
|  | ||
| # Concatenate text blocks into a single string | ||
| text = "\n".join( | ||
| block[BLOCK_TEXT_INDEX] | ||
| for block in blocks | ||
| if len(block) > BLOCK_TEXT_INDEX | ||
| ) | ||
| else: | ||
| text = page.get_text("text", sort=True) | ||
|  | ||
| if page_size_limit and len(text) > page_size_limit: | ||
| raise ImpossibleParsingError( | ||
| f"The text in page {i} of {file.page_count} was {len(text)} chars" | ||
| f" long, which exceeds the {page_size_limit} char limit for the PDF" | ||
| f" at path {path}." | ||
| ) | ||
| pages[str(i + 1)] = text | ||
| total_length += len(text) | ||
|  | ||
| metadata = ParsedMetadata( | ||
| parsing_libraries=[f"pymupdf ({pymupdf.__version__})"], | ||
| paperqa_version=pqa_version, | ||
| total_parsed_text_length=total_length, | ||
| parse_type="pdf", | ||
| ) | ||
| return ParsedText(content=pages, metadata=metadata) | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| from pathlib import Path | ||
|  | ||
| from paperqa_pymupdf import parse_pdf_to_pages | ||
|  | ||
| REPO_ROOT = Path(__file__).parents[3] | ||
| STUB_DATA_DIR = REPO_ROOT / "tests" / "stub_data" | ||
|  | ||
|  | ||
| def test_parse_pdf_to_pages() -> None: | ||
| filepath = STUB_DATA_DIR / "pasa.pdf" | ||
| parsed_text = parse_pdf_to_pages(filepath, use_block_parsing=True) | ||
| assert isinstance(parsed_text.content, dict) | ||
| assert "1" in parsed_text.content, "Parsed text should contain page 1" | ||
| assert ( | ||
| "Abstract\n\nWe introduce PaSa, an advanced Paper Search" | ||
| "\nagent powered by large language models." | ||
| ) in parsed_text.content["1"] | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
            File renamed without changes.
          
    
            File renamed without changes.
          
    
            File renamed without changes.
          
    
            File renamed without changes.
          
    
            File renamed without changes.
          
    
            File renamed without changes.
          
    
            File renamed without changes.
          
    
            File renamed without changes.
          
    
            File renamed without changes.
          
    
            File renamed without changes.
          
    
            File renamed without changes.
          
    
            File renamed without changes.
          
    
            File renamed without changes.
          
    
            File renamed without changes.
          
    
            File renamed without changes.
          
    
            File renamed without changes.
          
    
            File renamed without changes.
          
    
            File renamed without changes.
          
    
            File renamed without changes.
          
    
            File renamed without changes.
          
    
            File renamed without changes.
          
    
            File renamed without changes.
          
    
            File renamed without changes.
          
    
            File renamed without changes.
          
    
            File renamed without changes.
          
    
            File renamed without changes.
          
    
            File renamed without changes.
          
    
            File renamed without changes.
          
    
            File renamed without changes.
          
    
            File renamed without changes.
          
    
            File renamed without changes.
          
    
            File renamed without changes.
          
    
            File renamed without changes.
          
    
            File renamed without changes.
          
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
            File renamed without changes.
          
    
            File renamed without changes.
          
    
            File renamed without changes.
          
    
            File renamed without changes.
          
    
            File renamed without changes.
          
    
            File renamed without changes.
          
    
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.