-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimageCmd.go
65 lines (52 loc) · 1.78 KB
/
imageCmd.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
package cmd
import (
"context"
"fmt"
"os"
"strings"
"github.com/google/generative-ai-go/genai"
"github.com/spf13/cobra"
"google.golang.org/api/option"
)
var (
imageFilePath string
imageFileFormat string
)
var imageCmd = &cobra.Command{
Use: "image [your question] --path [image path] --format [image format]",
Example: "gencli image 'What this image is about?' --path cat.png --format png",
Short: "Know details about an image (Please put your question in quotes)",
Long: "Ask a question about an image and get a response. You need to provide the path of the image and the format of the image. The supported formats are jpg, jpeg, png, and gif.",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
res := imageFunc(args)
fmt.Println(res)
},
}
func imageFunc(args []string) string {
userArgs := strings.Join(args[0:], " ")
ctx := context.Background()
client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
CheckNilError(err)
defer client.Close()
model := client.GenerativeModel("gemini-1.5-flash")
imgData, err := os.ReadFile(imageFilePath)
CheckNilError(err)
// Supports multiple image inputs
prompt := []genai.Part{
genai.ImageData(imageFileFormat, imgData),
genai.Text(userArgs),
}
resp, err := model.GenerateContent(ctx, prompt...)
CheckNilError(err)
finalResponse := resp.Candidates[0].Content.Parts[0]
return fmt.Sprint(finalResponse)
}
func init() {
imageCmd.Flags().StringVarP(&imageFilePath, "path", "p", "", "Enter the image path")
imageCmd.Flags().StringVarP(&imageFileFormat, "format", "f", "", "Enter the image format (jpeg, png, etc.)")
errPathF := imageCmd.MarkFlagRequired("path")
errFormatF := imageCmd.MarkFlagRequired("format")
CheckNilError(errPathF)
CheckNilError(errFormatF)
}