-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: convert pdf to image concurrently (#818)
Because - it is slow when pdf to image This commit - make converter concurrently
- Loading branch information
1 parent
495c43e
commit f6456e3
Showing
5 changed files
with
187 additions
and
25 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
pkg/component/operator/document/v0/convert_to_images_test.go
This file contains 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,79 @@ | ||
package document | ||
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
qt "github.com/frankban/quicktest" | ||
|
||
"github.com/instill-ai/pipeline-backend/pkg/component/base" | ||
"github.com/instill-ai/pipeline-backend/pkg/component/internal/mock" | ||
"github.com/instill-ai/pipeline-backend/pkg/data" | ||
"github.com/instill-ai/pipeline-backend/pkg/data/format" | ||
) | ||
|
||
func Test_ConvertDocumentToImages(t *testing.T) { | ||
c := qt.New(t) | ||
|
||
test := struct { | ||
name string | ||
filepath string | ||
expectedLen int | ||
}{ | ||
name: "Convert PDF to Images", | ||
filepath: "testdata/test.pdf", | ||
expectedLen: 1, | ||
} | ||
|
||
bc := base.Component{} | ||
ctx := context.Background() | ||
|
||
component := Init(bc) | ||
c.Assert(component, qt.IsNotNil) | ||
|
||
execution, err := component.CreateExecution(base.ComponentExecution{ | ||
Component: component, | ||
Task: taskConvertToImages, | ||
}) | ||
c.Assert(err, qt.IsNil) | ||
c.Assert(execution, qt.IsNotNil) | ||
|
||
fileContent, err := os.ReadFile(test.filepath) | ||
c.Assert(err, qt.IsNil) | ||
|
||
base64DataURI := fmt.Sprintf("data:%s;base64,%s", mimeTypeByExtension(test.filepath), base64.StdEncoding.EncodeToString(fileContent)) | ||
|
||
ir, ow, eh, job := mock.GenerateMockJob(c) | ||
|
||
ir.ReadDataMock.Times(1).Set(func(ctx context.Context, input any) error { | ||
switch input := input.(type) { | ||
case *ConvertDocumentToImagesInput: | ||
*input = ConvertDocumentToImagesInput{ | ||
Document: func() format.Document { | ||
doc, err := data.NewDocumentFromURL(base64DataURI) | ||
if err != nil { | ||
return nil | ||
} | ||
return doc | ||
}(), | ||
} | ||
} | ||
return nil | ||
}) | ||
|
||
ow.WriteDataMock.Times(1).Set(func(ctx context.Context, output any) error { | ||
switch output := output.(type) { | ||
case *ConvertDocumentToImagesOutput: | ||
mock.Equal(len(output.Images), test.expectedLen) | ||
} | ||
return nil | ||
}) | ||
|
||
eh.ErrorMock.Optional() | ||
|
||
err = execution.Execute(ctx, []*base.Job{job}) | ||
c.Assert(err, qt.IsNil) | ||
} |
This file contains 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
23 changes: 23 additions & 0 deletions
23
pkg/component/operator/document/v0/transformer/execution/get_page_numbers.py
This file contains 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,23 @@ | ||
from io import BytesIO | ||
import json | ||
import sys | ||
import base64 | ||
import pdfplumber | ||
|
||
if __name__ == "__main__": | ||
try: | ||
json_str = sys.stdin.buffer.read().decode('utf-8') | ||
params = json.loads(json_str) | ||
pdf_string = params["PDF"] | ||
decoded_bytes = base64.b64decode(pdf_string) | ||
pdf_file_obj = BytesIO(decoded_bytes) | ||
|
||
pdf = pdfplumber.open(pdf_file_obj) | ||
|
||
output = { | ||
"page_numbers": len(pdf.pages) | ||
} | ||
|
||
print(json.dumps(output)) | ||
except Exception as e: | ||
print(json.dumps({"error": str(e)})) |
This file contains 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 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