-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
115 lines (93 loc) · 2.44 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"context"
"encoding/base64"
"fmt"
"log"
"os"
"github.com/weaviate/weaviate-go-client/v4/weaviate"
"github.com/weaviate/weaviate-go-client/v4/weaviate/graphql"
"github.com/weaviate/weaviate/entities/models"
"github.com/rchang0501/media-search-engine/internal"
)
func addModelToSchema(client *weaviate.Client, classObj *models.Class) {
// add the schema
err := client.Schema().ClassCreator().WithClass(classObj).Do(context.Background())
if err != nil {
panic(err)
}
}
func addObjectsToModel(client *weaviate.Client) {
objs := []*models.Object{}
for _, imgPath := range internal.Images {
base64String := internal.ConvertImageToBase64(imgPath)
imgName := internal.ExtractImageName(imgPath)
// Create a new object
obj := &models.Object{
Class: "Picture",
Properties: map[string]interface{}{
"image": base64String,
"text": fmt.Sprintf("This is a picture of a %s", imgName),
},
}
objs = append(objs, obj)
}
// batch write items
batchRes, err := client.Batch().ObjectsBatcher().WithObjects(objs...).Do(context.Background())
if err != nil {
panic(err)
}
for _, res := range batchRes {
if res.Result.Errors != nil {
panic(res.Result.Errors.Error)
}
}
}
func retrieveData(client *weaviate.Client, imagePath string) {
// Convert image data to base64
base64Image := internal.ConvertImageToBase64(imagePath)
fields := []graphql.Field{
{Name: "image"},
{Name: "text"},
}
nearText := client.GraphQL().
NearImageArgBuilder().WithImage(base64Image)
// Perform GraphQL query
resImage, err := client.GraphQL().Get().
WithClassName("Picture").
WithFields(fields...).
WithNearImage(nearText).
WithLimit(1).
Do(context.Background())
if err != nil {
log.Fatal(err)
}
resStr := fmt.Sprintf("%+v", resImage)
resultImage := internal.ExtractImageFromJSON(resStr)
fmt.Println("Result image:", resultImage)
resultData, err := base64.StdEncoding.DecodeString(resultImage)
if err != nil {
log.Fatal(err)
}
resultImagePath := "imgs/result.jpg"
err = os.WriteFile(resultImagePath, resultData, 0644)
if err != nil {
log.Fatal(err)
}
fmt.Println("Result image written to", resultImagePath)
}
func main() {
cfg := weaviate.Config{
Host: "localhost:8080",
Scheme: "http",
}
client, err := weaviate.NewClient(cfg)
if err != nil {
panic(err)
}
schema, err := client.Schema().Getter().Do(context.Background())
if err != nil {
panic(err)
}
fmt.Printf("schema: %+v", schema)
}