-
Notifications
You must be signed in to change notification settings - Fork 0
/
discover.go
202 lines (168 loc) · 5.18 KB
/
discover.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package main
import (
"cmp"
"context"
"embed"
"slices"
"strings"
"github.com/kkoreilly/osusu/osusu"
"github.com/kkoreilly/osusu/otextencoding"
"github.com/nlpodyssey/cybertron/pkg/client"
"github.com/nlpodyssey/cybertron/pkg/models/bert"
"github.com/nlpodyssey/spago/mat"
"goki.dev/colors"
"goki.dev/gi/v2/gi"
"goki.dev/gi/v2/giv"
"goki.dev/girl/styles"
"goki.dev/goosi/events"
"goki.dev/grows/jsons"
"goki.dev/grr"
)
//go:embed recipes.json
var recipesFS embed.FS
var recipes []*osusu.Recipe
//go:embed textEncodingVectors.json
var textEncodingVectorsFS embed.FS
var textEncodingVectors map[string][]float32
func configDiscover(rf *gi.Frame, mf *gi.Frame) {
if rf.HasChildren() {
rf.DeleteChildren(true)
}
updt := rf.UpdateStart()
rf.Style(func(s *styles.Style) {
s.Wrap = true
})
if recipes == nil {
err := jsons.OpenFS(&recipes, recipesFS, "recipes.json")
if err != nil {
gi.ErrorDialog(rf, err, "Error opening recipes")
return
}
for _, recipe := range recipes {
grr.Log(recipe.Init())
}
err = jsons.OpenFS(&textEncodingVectors, textEncodingVectorsFS, "textEncodingVectors.json")
if err != nil {
gi.ErrorDialog(rf, err, "Error opening recipe text encoding vectors")
return
}
otextencoding.Model = client.NewClientForTextEncoding("localhost:8081", client.Options{})
}
var meals []*osusu.Meal
err := osusu.DB.Find(&meals).Error
if err != nil {
gi.ErrorDialog(rf, err)
}
mealEntries := map[uint][]osusu.Entry{}
mealVectors := make([]mat.Matrix, len(meals))
for i, meal := range meals {
entries := []osusu.Entry{}
err := osusu.DB.Find(&entries, "meal_id = ? AND user_id = ?", meal.ID, curUser.ID).Error
if err != nil {
gi.ErrorDialog(rf, err)
}
mealEntries[meal.ID] = entries
res, err := otextencoding.Model.Encode(context.TODO(), meal.Text(), int(bert.MeanPooling))
if err != nil {
gi.ErrorDialog(rf, err, "Error text encoding meal")
continue
}
mealVectors[i] = res.Vector
}
for _, recipe := range recipes {
// first we get the base score index
// TODO(kai/osusu): cache this step
recipe.ComputeBaseScoreIndex()
// then we get the raw text encoding score
// TODO(kai/osusu): cache this step
recipeVector := textEncodingVectors[recipe.URL]
recipeMat := mat.NewDense[float32](mat.WithBacking(recipeVector))
recipe.TextEncodingScores = map[uint]float32{}
for i, meal := range meals {
mealVector := mealVectors[i]
score := mealVector.DotUnitary(recipeMat)
recipe.TextEncodingScores[meal.ID] = score.Item().F32()
}
// then we get the weighted score
// this step can not be cached
weightedScores := make([]*osusu.Score, len(meals))
for i, meal := range meals {
textEncodingScore := recipe.TextEncodingScores[meal.ID]
entries := mealEntries[meal.ID]
score := meal.Score(entries)
score.ComputeTotal(curOptions)
osusu.MulScore(score, textEncodingScore)
weightedScores[i] = score
}
recipe.EncodingScoreIndex = *osusu.AverageScore(weightedScores)
}
// now we can compute the percentile scores
osusu.ComputeNormScores(recipes)
// and then the total scores
for _, recipe := range recipes {
recipe.BaseScore.ComputeTotal(curOptions)
recipe.EncodingScore.ComputeTotal(curOptions)
// encoding score is three times more important than base score
recipe.Score = *osusu.AverageScore([]*osusu.Score{&recipe.BaseScore, &recipe.EncodingScore, &recipe.EncodingScore, &recipe.EncodingScore})
}
slices.SortFunc(recipes, func(a, b *osusu.Recipe) int {
return cmp.Compare(b.Score.Total, a.Score.Total)
})
for _, recipe := range recipes {
recipe := recipe
if rf.NumChildren() > 100 {
break
}
grr.Log(recipe.CategoryFlag.SetString(strings.Join(recipe.Category, "|")))
grr.Log(recipe.CuisineFlag.SetString(strings.Join(recipe.Cuisine, "|")))
if !bitFlagsOverlap(recipe.CategoryFlag, curOptions.Categories) ||
!bitFlagsOverlap(recipe.CuisineFlag, curOptions.Cuisines) {
continue
}
rc := gi.NewFrame(rf)
cardStyles(rc)
img := gi.NewImage(rc)
go func() {
if i := getImageFromURL(recipe.Image); i != nil {
img.SetImage(i)
img.Update()
}
}()
gi.NewLabel(rc).SetType(gi.LabelHeadlineSmall).SetText(recipe.Name)
castr := friendlyBitFlagString(recipe.CategoryFlag)
custr := friendlyBitFlagString(recipe.CuisineFlag)
text := castr
if castr != "" && custr != "" {
text += " • "
}
text += custr
gi.NewLabel(rc).SetText(text).Style(func(s *styles.Style) {
s.Color = colors.Scheme.OnSurfaceVariant
})
scoreGrid(rc, &recipe.Score, true)
rc.OnClick(func(e events.Event) {
addRecipe(rf, recipe, rc, mf)
})
}
rf.Update()
rf.UpdateEndLayout(updt)
}
func addRecipe(rf *gi.Frame, recipe *osusu.Recipe, rc *gi.Frame, mf *gi.Frame) {
d := gi.NewBody().AddTitle("Add recipe")
giv.NewStructView(d).SetStruct(recipe).SetReadOnly(true)
d.AddBottomBar(func(pw gi.Widget) {
d.AddCancel(pw)
d.AddOk(pw).SetText("Add").OnClick(func(e events.Event) {
meal := &osusu.Meal{
Name: recipe.Name,
Description: recipe.Description,
Image: recipe.Image,
Category: recipe.CategoryFlag,
Cuisine: recipe.CuisineFlag,
}
meal.Source.SetFlag(true, osusu.Cooking)
newMeal(rf, mf, meal)
})
})
d.NewFullDialog(rc).Run()
}