-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimages.go
165 lines (139 loc) · 3.61 KB
/
images.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
package goai
import (
"bytes"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"strconv"
"github.com/spf13/viper"
)
func (goai Client) ImageGen(prompt, imageFile string, n int) ImageResponse {
var oaiRequest interface{}
oaiResponse := ImageResponse{}
// Check if we are editing an image or generating a new one
if imageFile != "" {
// Create the JSON Request Body
oaiRequest = &ImageEditRequest{
N: n,
ResponseFormat: "url",
Prompt: prompt,
User: goai.User,
Size: viper.GetString("openAI_image_size"),
}
goai.UploadImage(oaiRequest, &oaiResponse, goai.Endpoint+"images/edits", imageFile)
} else { // Generate a new image
oaiRequest = &ImageRequest{
N: n,
ResponseFormat: "url",
Prompt: prompt,
User: goai.User,
Size: viper.GetString("openAI_image_size"),
Model: viper.GetString("openAI_image_model"),
}
goai.PostJson(oaiRequest, &oaiResponse, goai.Endpoint+"images/generations")
}
if goai.Verbose {
fmt.Println(oaiRequest)
}
return oaiResponse
}
func (goai Client) UploadImage(requestJson, responseJson interface{}, endpoint, filePath string) error {
// Get the absolute path of the file
fullPath, err := filepath.Abs(filePath)
if err != nil {
return err
}
// https://platform.openAI_com/docs/api-reference/images/create-edit#images/create-edit-image
// The image to edit. Must be a valid PNG file, less than 4MB, and square.
// If mask is not provided, image must have transparency, which will be used as the mask.
//
// Open the PNG image file
file, err := os.Open(fullPath)
if err != nil {
return err
}
defer file.Close()
// Create a new multipart writer
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
// Add the PNG file to the request
part, err := writer.CreateFormFile("image", filePath)
if err != nil {
return err
}
_, err = io.Copy(part, file)
if err != nil {
return err
}
oaiImageEditJson := requestJson.(*ImageEditRequest)
// Add the JSON payload to the request
part, err = writer.CreateFormField("prompt")
if err != nil {
return err
}
part.Write([]byte(oaiImageEditJson.Prompt))
part, err = writer.CreateFormField("n")
if err != nil {
return err
}
part.Write([]byte(strconv.Itoa(oaiImageEditJson.N)))
part, err = writer.CreateFormField("size")
if err != nil {
return err
}
part.Write([]byte(oaiImageEditJson.Size))
part, err = writer.CreateFormField("user")
if err != nil {
return err
}
part.Write([]byte(oaiImageEditJson.User))
// Close the multipart writer
err = writer.Close()
if err != nil {
return err
}
// Create a new HTTP request
req, err := http.NewRequest("POST", endpoint, body)
if err != nil {
return err
}
req.Header.Set("Content-Type", writer.FormDataContentType())
req.Header.Set("Authorization", "Bearer "+goai.API_KEY)
if goai.Verbose {
// trace()
fmt.Println("Request Body: ", req.Body)
fmt.Println("Request JSON: ", oaiImageEditJson)
}
// Send the request
fmt.Println("⏳ Uploading File: " + fullPath)
resp, err := goai.HTTPClient.Do(req)
if err != nil {
return err
}
// Read the JSON Response Body
// jsonString, err := io.ReadAll(resp.Body)
// if err != nil {
// return err
// }
// Check for API Errors
jsonString, err := httpCatchErr(resp)
if err != nil {
return err
}
// Unmarshal the JSON Response Body
err = json.Unmarshal([]byte(jsonString), &responseJson)
if err != nil {
return err
}
if goai.Verbose {
// trace()
fmt.Println(string(jsonString))
}
// Close the HTTP Response Body
defer resp.Body.Close()
return nil
}