This repository was archived by the owner on Jul 22, 2025. It is now read-only.
  
  
  
            
  
    
      generated from discourse/discourse-plugin-skeleton
    
        
        - 
                Notifications
    You must be signed in to change notification settings 
- Fork 41
FEATURE: Native PDF support #1127
          
     Merged
      
      
    
  
     Merged
                    Changes from 5 commits
      Commits
    
    
            Show all changes
          
          
            7 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      c41974e
              
                FEATURE: Native PDF support
              
              
                SamSaffron 14901fb
              
                This means that our simple pdf eval passes at last
              
              
                SamSaffron 0036a10
              
                fix spec
              
              
                SamSaffron fbf89ce
              
                skip test in CI
              
              
                SamSaffron 7d222ec
              
                test file support
              
              
                SamSaffron 001b179
              
                Update lib/utils/image_to_text.rb
              
              
                SamSaffron 9960ac9
              
                address pr comments
              
              
                SamSaffron 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
    
  
  
    
              
  
    
      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
    
  
  
    
              
  
    
      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
    
  
  
    
              
  
    
      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
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| # frozen_string_literal: true | ||
|  | ||
| class DiscourseAi::Utils::PdfToText | ||
| class Reader | ||
| def initialize(upload:, user: nil, llm_model: nil) | ||
| @extractor = | ||
| DiscourseAi::Utils::PdfToText.new(upload: upload, user: user, llm_model: llm_model) | ||
| @enumerator = create_enumerator | ||
| @buffer = +"" | ||
| end | ||
|  | ||
| def read(length) | ||
| return @buffer.slice!(0, length) if !@buffer.empty? | ||
|  | ||
| begin | ||
| @buffer << @enumerator.next | ||
| rescue StopIteration | ||
| return nil | ||
| end | ||
|  | ||
| @buffer.slice!(0, length) | ||
| end | ||
|  | ||
| private | ||
|  | ||
| def create_enumerator | ||
| Enumerator.new { |yielder| @extractor.extract_text { |chunk| yielder.yield(chunk || "") } } | ||
| end | ||
| end | ||
|  | ||
| attr_reader :upload | ||
|  | ||
| def self.as_fake_file(upload:, user: nil, llm_model: nil) | ||
| Reader.new(upload: upload, user: user, llm_model: llm_model) | ||
| end | ||
|  | ||
| def initialize(upload:, user: nil, llm_model: nil) | ||
| @upload = upload | ||
| @user = user | ||
| @llm_model = llm_model | ||
| end | ||
|  | ||
| def extract_text | ||
| pdf_path = | ||
| if upload.local? | ||
| Discourse.store.path_for(upload) | ||
| else | ||
| Discourse.store.download_safe(upload, max_file_size_kb: MAX_PDF_SIZE)&.path | ||
|         
                  SamSaffron marked this conversation as resolved.
              Show resolved
            Hide resolved There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn't seem like we are cleaning up this file after downloading it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we clean up anywhere though? this is a bit tricky, no clean pattern for this. | ||
| end | ||
|  | ||
| raise Discourse::InvalidParameters.new("Failed to download PDF") if pdf_path.nil? | ||
|  | ||
| require "pdf/reader" | ||
|  | ||
| page_number = 0 | ||
| PDF::Reader.open(pdf_path) do |reader| | ||
| reader.pages.each do |page| | ||
| page_number += 1 | ||
| llm_decorate(page_number: page_number, text: page.text, pdf_path: pdf_path) do |chunk| | ||
| yield chunk | ||
| end | ||
| end | ||
| end | ||
| end | ||
|  | ||
| def llm_decorate(page_number:, text:, pdf_path:) | ||
| raise "Must be called with block" if !block_given? | ||
| if !@llm_model | ||
| yield text | ||
| return | ||
| end | ||
|  | ||
| begin | ||
| temp_dir = Dir.mktmpdir("discourse-pdf-#{SecureRandom.hex(8)}") | ||
| output_path = File.join(temp_dir, "page-#{page_number}.png") | ||
|  | ||
| # Extract specific page using ImageMagick | ||
| # image magick uses 0 based page numbers | ||
| command = [ | ||
| "magick", | ||
| "-density", | ||
| "300", | ||
| "#{pdf_path}[#{page_number - 1}]", | ||
| "-background", | ||
| "white", | ||
| "-auto-orient", | ||
| "-quality", | ||
| "85", | ||
| output_path, | ||
| ] | ||
|  | ||
| Discourse::Utils.execute_command( | ||
| *command, | ||
| failure_message: "Failed to convert PDF page #{page_number} to image", | ||
| timeout: 30, | ||
|         
                  SamSaffron marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| ) | ||
|  | ||
| # TODO - we are creating leftover uploads, they will be cleaned up | ||
| # but maybe we should just keep them around? | ||
| upload = | ||
| UploadCreator.new(File.open(output_path), "page-#{page_number}.png").create_for(@user&.id) | ||
|  | ||
| DiscourseAi::Utils::ImageToText | ||
| .new(upload: upload, llm_model: @llm_model, user: @user, guidance_text: text) | ||
| .extract_text { |chunk| yield chunk } | ||
| ensure | ||
| FileUtils.rm_rf(temp_dir) if temp_dir | ||
| end | ||
| end | ||
| end | ||
  
    
      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
    
  
  
    
              
            Binary file not shown.
          
    
  
    
      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
    
  
  
    
              
      
      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.