mirror of
https://github.com/icereed/paperless-gpt.git
synced 2025-03-12 12:58:02 -05:00
* feat(ocr): implement OCR provider interface and add Google Document AI and LLM providers * chore(deps): reorder dependencies in go.mod for better readability * chore: update version numbers and adjust Docker configuration for Google Document AI integration * feat(logging): add structured logging to Google Document AI and LLM providers * chore: add placeholder file to maintain directory structure in web-app/dist * chore(docker): remove Google Application Credentials configuration from docker-compose
50 lines
1.5 KiB
Go
50 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
// ProcessDocumentOCR processes a document through OCR and returns the combined text
|
|
func (app *App) ProcessDocumentOCR(ctx context.Context, documentID int) (string, error) {
|
|
docLogger := documentLogger(documentID)
|
|
docLogger.Info("Starting OCR processing")
|
|
|
|
imagePaths, err := app.Client.DownloadDocumentAsImages(ctx, documentID, limitOcrPages)
|
|
defer func() {
|
|
for _, imagePath := range imagePaths {
|
|
if err := os.Remove(imagePath); err != nil {
|
|
docLogger.WithError(err).WithField("image_path", imagePath).Warn("Failed to remove temporary image file")
|
|
}
|
|
}
|
|
}()
|
|
if err != nil {
|
|
return "", fmt.Errorf("error downloading document images for document %d: %w", documentID, err)
|
|
}
|
|
|
|
docLogger.WithField("page_count", len(imagePaths)).Debug("Downloaded document images")
|
|
|
|
var ocrTexts []string
|
|
for i, imagePath := range imagePaths {
|
|
pageLogger := docLogger.WithField("page", i+1)
|
|
pageLogger.Debug("Processing page")
|
|
|
|
imageContent, err := os.ReadFile(imagePath)
|
|
if err != nil {
|
|
return "", fmt.Errorf("error reading image file for document %d, page %d: %w", documentID, i+1, err)
|
|
}
|
|
|
|
ocrText, err := app.ocrProvider.ProcessImage(ctx, imageContent)
|
|
if err != nil {
|
|
return "", fmt.Errorf("error performing OCR for document %d, page %d: %w", documentID, i+1, err)
|
|
}
|
|
pageLogger.Debug("OCR completed for page")
|
|
|
|
ocrTexts = append(ocrTexts, ocrText)
|
|
}
|
|
|
|
docLogger.Info("OCR processing completed successfully")
|
|
return strings.Join(ocrTexts, "\n\n"), nil
|
|
}
|