/
analyzeImages.go
235 lines (206 loc) · 6.27 KB
/
analyzeImages.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package run
import (
"bufio"
"fmt"
"os"
"os/signal"
"path"
"strings"
"syscall"
"time"
"github.com/google/generative-ai-go/genai"
"github.com/google/uuid"
"github.com/kubetrail/gini/pkg/flags"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"google.golang.org/api/option"
)
func AnalyzeImages(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
ctx, stop := signal.NotifyContext(ctx, syscall.SIGINT, syscall.SIGTERM)
defer stop()
_ = viper.BindPFlag(flags.ApiKey, cmd.Flag(flags.ApiKey))
_ = viper.BindPFlag(flags.Model, cmd.Flag(flags.Model))
_ = viper.BindPFlag(flags.TopP, cmd.Flag(flags.TopP))
_ = viper.BindPFlag(flags.TopK, cmd.Flag(flags.TopK))
_ = viper.BindPFlag(flags.Temperature, cmd.Flag(flags.Temperature))
_ = viper.BindPFlag(flags.CandidateCount, cmd.Flag(flags.CandidateCount))
_ = viper.BindPFlag(flags.MaxOutputTokens, cmd.Flag(flags.MaxOutputTokens))
_ = viper.BindPFlag(flags.AutoSave, cmd.Flag(flags.AutoSave))
_ = viper.BindPFlag(flags.Render, cmd.Flag(flags.Render))
_ = viper.BindPFlag(flags.AllowHarmProbability, cmd.Flag(flags.AllowHarmProbability))
_ = viper.BindPFlag(flags.File, cmd.Flag(flags.File))
_ = viper.BindPFlag(flags.Format, cmd.Flag(flags.Format))
_ = viper.BindEnv(flags.ApiKey, flags.ApiKeyEnv)
apiKey := viper.GetString(flags.ApiKey)
modelName := viper.GetString(flags.Model)
topP := float32(viper.GetFloat64(flags.TopP))
topK := viper.GetInt32(flags.TopK)
temperature := float32(viper.GetFloat64(flags.Temperature))
candidateCount := viper.GetInt32(flags.CandidateCount)
maxOutputTokens := viper.GetInt32(flags.MaxOutputTokens)
autoSave := viper.GetBool(flags.AutoSave)
render := viper.GetString(flags.Render)
allowHarmProbability := viper.GetString(flags.AllowHarmProbability)
files := viper.GetStringSlice(flags.File)
formats := viper.GetStringSlice(flags.Format)
if len(apiKey) == 0 || len(modelName) == 0 {
return fmt.Errorf("api-key or model cannot be empty")
}
fileName := fmt.Sprintf("history-%s.txt", uuid.New().String())
var fileWriter *bufio.Writer
if autoSave {
f, err := os.Create(fileName)
if err != nil {
return fmt.Errorf("failed to create history file: %w", err)
}
defer f.Close()
fileWriter = bufio.NewWriter(f)
if _, err := fileWriter.WriteString(
fmt.Sprintf("Command: %s\nTimestamp: %s\n",
strings.Join(os.Args, " "),
time.Now().String(),
),
); err != nil {
return fmt.Errorf("failed to write to history file: %w", err)
}
}
client, err := genai.NewClient(ctx, option.WithAPIKey(apiKey))
if err != nil {
return fmt.Errorf("failed to create new genai client: %w", err)
}
defer client.Close()
model := client.GenerativeModel(modelName)
if topP >= 0 {
model.SetTopP(topP)
}
if topK >= 0 {
model.SetTopK(topK)
}
if temperature >= 0 {
model.SetTemperature(temperature)
}
if candidateCount >= 0 {
model.SetCandidateCount(candidateCount)
}
if maxOutputTokens >= 0 {
model.SetMaxOutputTokens(maxOutputTokens)
}
parts := make([]genai.Part, len(files)+1)
if formats == nil {
formats = make([]string, len(files))
for i := range formats {
formats[i] = flags.FormatJpeg
}
} else {
if len(formats) > len(files) {
return fmt.Errorf("cannot provide more formats than number of images")
}
for i := len(formats); i < len(files); i++ {
formats = append(formats, flags.FormatJpeg)
}
}
for i, file := range files {
b, err := os.ReadFile(file)
if err != nil {
return fmt.Errorf("failed to read image file: %w", err)
}
if len(b) > flags.MaxBlobBufferSizeBytes {
return fmt.Errorf("%s file size needs to be less than %d bytes",
file, flags.MaxBlobBufferSizeBytes)
}
if path.Base(formats[i]) == formats[i] {
formats[i] = "image/" + formats[i]
}
parts[i] = genai.Blob{
MIMEType: formats[i],
Data: b,
}
}
var prompt string
if len(args) > 0 {
prompt = strings.Join(args, " ")
} else {
_, _ = fmt.Fprintf(cmd.OutOrStdout(), ">>> ")
scanner := bufio.NewScanner(os.Stdin)
var lines []string
var hold bool
InnerLoop:
for {
scanner.Scan()
line := scanner.Text()
select {
case <-ctx.Done():
break InnerLoop
default:
if len(line) == 0 && !hold {
break InnerLoop
}
if strings.TrimSpace(line) == startHold {
hold = true
continue InnerLoop
}
if strings.TrimSpace(line) == endHold && hold {
break InnerLoop
}
lines = append(lines, line)
}
}
if err := scanner.Err(); err != nil {
return fmt.Errorf("error reading input: %w", err)
}
if len(lines) == 0 {
return nil
}
prompt = strings.Join(lines, "\n")
}
parts[len(files)] = genai.Text(prompt)
send := func(msg string) (*genai.GenerateContentResponse, error) {
res, err := model.GenerateContent(ctx, parts...)
if err != nil {
return nil, fmt.Errorf("failure at backend: %w", err)
}
return res, nil
}
s := "...sending prompt... please wait"
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "%s\r", s)
res, err := send(prompt)
if err != nil {
return err
}
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "%s\r", strings.Repeat(" ", len(s)+2))
if allowHarmProbability != flags.HarmProbabilityUnspecified {
var harmProbability genai.HarmProbability
switch allowHarmProbability {
case flags.HarmProbabilityNegligible:
harmProbability = genai.HarmProbabilityNegligible
case flags.HarmProbabilityLow:
harmProbability = genai.HarmProbabilityLow
case flags.HarmProbabilityMedium:
harmProbability = genai.HarmProbabilityMedium
case flags.HarmProbabilityHigh:
harmProbability = genai.HarmProbabilityHigh
default:
return fmt.Errorf("invalid harm probability:%s", allowHarmProbability)
}
if res != nil && res.PromptFeedback != nil {
for _, rating := range res.PromptFeedback.SafetyRatings {
if rating.Probability > harmProbability {
return fmt.Errorf("output harm probability threshold crossed")
}
}
}
}
if err := printResponse(res, cmd.OutOrStdout(), render, autoSave, fileWriter); err != nil {
return fmt.Errorf("failed to write response: %w", err)
}
if autoSave {
if err := fileWriter.Flush(); err != nil {
return fmt.Errorf("failed to write to history file: %w", err)
}
if _, err := fmt.Fprintln(cmd.OutOrStdout(), fmt.Sprintf("history saved to %s", fileName)); err != nil {
return fmt.Errorf("failed to write to output: %w", err)
}
}
return nil
}