Skip to content
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

refactor(data): implement BinaryFetcher interface for improved code structure #841

Merged
merged 4 commits into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cmd/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,12 @@ func main() {
defer artifactPrivateServiceClientConn.Close()
}

minioxClientWrapper, err := miniox.NewMinioClient(ctx, &config.Config.Minio, logger)
if err != nil {
logger.Fatal("failed to create miniox client wrapper", zap.Error(err))
}

binaryFetcher := external.NewArtifactBinaryFetcher(artifactPrivateServiceClient, minioxClientWrapper)
service := service.NewService(
service.ServiceConfig{
Repository: repo,
Expand All @@ -281,6 +287,7 @@ func main() {
Memory: ms,
WorkerUID: workerUID,
RetentionHandler: nil,
BinaryFetcher: binaryFetcher,
},
)

Expand Down Expand Up @@ -448,6 +455,7 @@ func main() {
WorkerUID: workerUID,
ArtifactPublicServiceClient: artifactPublicServiceClient,
ArtifactPrivateServiceClient: artifactPrivateServiceClient,
BinaryFetcher: binaryFetcher,
},
)

Expand Down
8 changes: 7 additions & 1 deletion pkg/component/ai/openai/v0/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package openai
import (
"bufio"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -462,7 +463,12 @@ func (e *execution) worker(ctx context.Context, client *httpclient.Client, job *

results := []imageGenerationsOutputResult{}
for _, d := range resp.Data {
img, err := data.NewImageFromURL(fmt.Sprintf("data:image/webp;base64,%s", d.Image))
b, err := base64.StdEncoding.DecodeString(d.Image)
if err != nil {
job.Error.Error(ctx, err)
return
}
img, err := data.NewImageFromBytes(b, data.PNG, "")
if err != nil {
job.Error.Error(ctx, err)
return
Expand Down
3 changes: 2 additions & 1 deletion pkg/component/base/executionwrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ func NewOutputWriter(ow OutputWriter, schema string) *outputWriter {
}
}
func (ow *outputWriter) WriteData(ctx context.Context, output any) (err error) {
outputMap, err := data.Marshal(output)
marshaler := data.NewMarshaler()
outputMap, err := marshaler.Marshal(output)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"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/external"
)

func TestDetectActivity(t *testing.T) {
Expand Down Expand Up @@ -124,7 +125,9 @@ func TestDetectActivity(t *testing.T) {
jsonValue, err := data.NewJSONValue(segmentsMap)
c.Assert(err, qt.IsNil)

c.Assert(data.Unmarshal(jsonValue, &expectedSegmentsStruct), qt.IsNil)
binaryFetcher := external.NewBinaryFetcher()
unmarshaler := data.NewUnmarshaler(binaryFetcher)
c.Assert(unmarshaler.Unmarshal(context.Background(), jsonValue, &expectedSegmentsStruct), qt.IsNil)
expectedSegments := expectedSegmentsStruct.Segments

c.Assert(capturedOutput.Segments, qt.HasLen, len(expectedSegments))
Expand Down
5 changes: 4 additions & 1 deletion pkg/component/operator/audio/v0/task_segment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"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/external"
)

func TestSegment(t *testing.T) {
Expand Down Expand Up @@ -67,7 +68,9 @@ func TestSegment(t *testing.T) {
jsonValue, err := data.NewJSONValue(segmentsMap)
c.Assert(err, qt.IsNil)

c.Assert(data.Unmarshal(jsonValue, &segmentsStruct), qt.IsNil)
binaryFetcher := external.NewBinaryFetcher()
unmarshaler := data.NewUnmarshaler(binaryFetcher)
c.Assert(unmarshaler.Unmarshal(context.Background(), jsonValue, &segmentsStruct), qt.IsNil)
segments := segmentsStruct.Segments

ir.ReadDataMock.Set(func(ctx context.Context, input any) error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package document

import (
"context"
"encoding/base64"

"github.com/instill-ai/pipeline-backend/pkg/component/base"
"github.com/instill-ai/pipeline-backend/pkg/component/operator/document/v0/transformer"
Expand Down Expand Up @@ -40,7 +41,8 @@ func (e *execution) convertDocumentToMarkdown(ctx context.Context, job *base.Job
Images: func() []format.Image {
images := make([]format.Image, len(transformerOutputStruct.Images))
for i, image := range transformerOutputStruct.Images {
images[i], _ = data.NewImageFromURL(image)
b, _ := base64.StdEncoding.DecodeString(image)
images[i], _ = data.NewImageFromBytes(b, data.PNG, "")
// TODO: handle error
}
return images
Expand All @@ -49,7 +51,8 @@ func (e *execution) convertDocumentToMarkdown(ctx context.Context, job *base.Job
AllPageImages: func() []format.Image {
images := make([]format.Image, len(transformerOutputStruct.AllPageImages))
for i, image := range transformerOutputStruct.AllPageImages {
images[i], _ = data.NewImageFromURL(image)
b, _ := base64.StdEncoding.DecodeString(image)
images[i], _ = data.NewImageFromBytes(b, data.PNG, "")
// TODO: handle error
}
return images
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package document

import (
"context"
"encoding/base64"
"fmt"
"os"
"testing"

Expand Down Expand Up @@ -109,15 +107,13 @@ func TestConvertDocumentToMarkdown(t *testing.T) {
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.Set(func(ctx context.Context, input any) error {
switch input := input.(type) {
case *ConvertDocumentToMarkdownInput:
*input = ConvertDocumentToMarkdownInput{
Document: func() format.Document {
doc, err := data.NewDocumentFromURL(base64DataURI)
doc, err := data.NewDocumentFromBytes(fileContent, mimeTypeByExtension(test.filepath), "")
if err != nil {
return nil
}
Expand Down
6 changes: 1 addition & 5 deletions pkg/component/operator/document/v0/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package document

import (
"context"
"encoding/base64"
"fmt"
"os"
"testing"

Expand Down Expand Up @@ -65,8 +63,6 @@ func TestConvertToText(t *testing.T) {
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))

execution, err := component.CreateExecution(base.ComponentExecution{
Component: component,
Task: "TASK_CONVERT_TO_TEXT",
Expand All @@ -79,7 +75,7 @@ func TestConvertToText(t *testing.T) {
case *ConvertToTextInput:
*input = ConvertToTextInput{
Document: func() format.Document {
doc, err := data.NewDocumentFromURL(base64DataURI)
doc, err := data.NewDocumentFromBytes(fileContent, mimeTypeByExtension(test.filepath), "")
if err != nil {
return nil
}
Expand Down
9 changes: 6 additions & 3 deletions pkg/component/operator/document/v0/convert_to_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package document

import (
"context"
"encoding/base64"

"github.com/instill-ai/pipeline-backend/pkg/component/base"
"github.com/instill-ai/pipeline-backend/pkg/component/operator/document/v0/transformer"
Expand All @@ -22,8 +23,8 @@ func (e *execution) convertDocumentToImages(ctx context.Context, job *base.Job)
}

transformerInputStruct := transformer.ConvertDocumentToImagesTransformerInput{
Document: dataURI.String(),
Filename: inputStruct.Filename,
Document: dataURI.String(),
Filename: inputStruct.Filename,
Resolution: inputStruct.Resolution,
}

Expand All @@ -35,7 +36,9 @@ func (e *execution) convertDocumentToImages(ctx context.Context, job *base.Job)
Images: func() []format.Image {
images := make([]format.Image, len(transformerOutputStruct.Images))
for i, image := range transformerOutputStruct.Images {
images[i], _ = data.NewImageFromURL(image)
b, _ := base64.StdEncoding.DecodeString(image)
images[i], _ = data.NewImageFromBytes(b, data.PNG, "")
// TODO: handle error
}
return images
}(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package document

import (
"context"
"encoding/base64"
"fmt"
"os"
"testing"

Expand Down Expand Up @@ -44,16 +42,14 @@ func Test_ConvertDocumentToImages(t *testing.T) {
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)
doc, err := data.NewDocumentFromBytes(fileContent, mimeTypeByExtension(test.filepath), "")
if err != nil {
return nil
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/data/audio.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package data

import (
"context"
"encoding/json"
"fmt"
"os"
Expand All @@ -11,6 +12,7 @@ import (

"github.com/instill-ai/pipeline-backend/pkg/data/format"
"github.com/instill-ai/pipeline-backend/pkg/data/path"
"github.com/instill-ai/pipeline-backend/pkg/external"
)

type audioData struct {
Expand Down Expand Up @@ -50,8 +52,8 @@ func NewAudioFromBytes(b []byte, contentType, filename string) (*audioData, erro
return createAudioData(b, contentType, filename)
}

func NewAudioFromURL(url string) (*audioData, error) {
b, contentType, filename, err := convertURLToBytes(url)
func NewAudioFromURL(ctx context.Context, binaryFetcher external.BinaryFetcher, url string) (video *audioData, err error) {
b, contentType, filename, err := binaryFetcher.FetchFromURL(ctx, url)
if err != nil {
return nil, err
}
Expand Down
7 changes: 6 additions & 1 deletion pkg/data/audio_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package data

import (
"context"
"os"
"testing"

"github.com/google/go-cmp/cmp/cmpopts"

qt "github.com/frankban/quicktest"

"github.com/instill-ai/pipeline-backend/pkg/external"
)

func TestNewAudioFromBytes(t *testing.T) {
Expand Down Expand Up @@ -54,7 +57,9 @@ func TestNewAudioFromBytes(t *testing.T) {
func TestNewAudioFromURL(t *testing.T) {
t.Parallel()
c := qt.New(t)
ctx := context.Background()

binaryFetcher := external.NewBinaryFetcher()
testCases := []struct {
name string
url string
Expand All @@ -66,7 +71,7 @@ func TestNewAudioFromURL(t *testing.T) {

for _, tc := range testCases {
c.Run(tc.name, func(c *qt.C) {
audio, err := NewAudioFromURL(tc.url)
audio, err := NewAudioFromURL(ctx, binaryFetcher, tc.url)

if tc.name == "Valid audio URL" {
c.Assert(err, qt.IsNil)
Expand Down
22 changes: 16 additions & 6 deletions pkg/data/document.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package data

import (
"context"
"encoding/base64"
"fmt"
"strings"

"github.com/instill-ai/pipeline-backend/pkg/component/operator/document/v0/transformer"
"github.com/instill-ai/pipeline-backend/pkg/data/format"
"github.com/instill-ai/pipeline-backend/pkg/data/path"
"github.com/instill-ai/pipeline-backend/pkg/external"
)

type documentData struct {
Expand Down Expand Up @@ -38,12 +41,11 @@ func NewDocumentFromBytes(b []byte, contentType, filename string) (*documentData
return createDocumentData(b, contentType, filename)
}

func NewDocumentFromURL(url string) (*documentData, error) {
b, contentType, filename, err := convertURLToBytes(url)
func NewDocumentFromURL(ctx context.Context, binaryFetcher external.BinaryFetcher, url string) (*documentData, error) {
b, contentType, filename, err := binaryFetcher.FetchFromURL(ctx, url)
if err != nil {
return nil, err
}

return createDocumentData(b, contentType, filename)
}

Expand Down Expand Up @@ -143,7 +145,12 @@ func (d *documentData) PDF() (val format.Document, err error) {
return nil, err
}

return NewDocumentFromURL(fmt.Sprintf("data:application/pdf;base64,%s", s))
b, err := base64.StdEncoding.DecodeString(s)
if err != nil {
return nil, err
}

return NewDocumentFromBytes(b, PDF, d.filename)
}

func (d *documentData) Images() (mp Array, err error) {
Expand All @@ -168,8 +175,11 @@ func (d *documentData) Images() (mp Array, err error) {
images := make([]format.Value, len(res.Images))

for idx := range res.Images {
// img := strings.Split(res.Images[idx], ",")[1]
images[idx], err = NewImageFromURL(res.Images[idx])
b, err := base64.StdEncoding.DecodeString(res.Images[idx])
if err != nil {
return nil, err
}
images[idx], err = NewImageFromBytes(b, PNG, d.filename)
if err != nil {
return nil, fmt.Errorf("NewImageFromBytes: %w", err)
}
Expand Down
8 changes: 6 additions & 2 deletions pkg/data/document_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package data

import (
"context"
"os"
"testing"

qt "github.com/frankban/quicktest"

"github.com/instill-ai/pipeline-backend/pkg/external"
)

func TestNewDocumentFromBytes(t *testing.T) {
Expand Down Expand Up @@ -48,7 +51,8 @@ func TestNewDocumentFromBytes(t *testing.T) {
func TestNewDocumentFromURL(t *testing.T) {
t.Parallel()
c := qt.New(t)

ctx := context.Background()
binaryFetcher := external.NewBinaryFetcher()
testCases := []struct {
name string
url string
Expand All @@ -61,7 +65,7 @@ func TestNewDocumentFromURL(t *testing.T) {

for _, tc := range testCases {
c.Run(tc.name, func(c *qt.C) {
document, err := NewDocumentFromURL(tc.url)
document, err := NewDocumentFromURL(ctx, binaryFetcher, tc.url)

if tc.name == "Valid PDF URL" || tc.name == "Valid TXT URL" || tc.name == "Valid DOCX URL" {
c.Assert(err, qt.IsNil)
Expand Down
Loading
Loading