-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathimage.go
172 lines (145 loc) · 4.8 KB
/
image.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
package openaigo
import (
"bytes"
"fmt"
"io"
"mime/multipart"
)
const (
Size256 string = "256x256"
Size512 string = "512x512"
Size1024 string = "1024x1024"
)
type ImageGenerationRequestBody struct {
Prompt string `json:"prompt"`
N int `json:"n,omitempty"`
Size string `json:"size,omitempty"`
ResponseFormat string `json:"response_format,omitempty"`
User string `json:"user,omitempty"`
}
type ImageResponse struct {
Created int64 `json:"created"`
Data []ImageData `json:"data"`
}
type ImageData struct {
Base64 string `json:"b64_json"`
URL string `json:"url"`
}
type ImageGenerationResponse ImageResponse
type ImageEditRequestBody struct {
// image Required
// The image to use as the basis for the variation(s). Must be a valid PNG file, less than 4MB, and square.
// User MUST close it if it's like ReadCloser.
Image io.Reader
// n integer Optional Defaults to 1
// The number of images to generate. Must be between 1 and 10.
N int
// size string Optional Defaults to 1024x1024
// The size of the generated images. Must be one of 256x256, 512x512, or 1024x1024.
Size string
// response_format string Optional Defaults to url
// The format in which the generated images are returned. Must be one of url or b64_json.
ResponseFormat string
// user string Optional
// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
// Learn more: https://beta.openai.com/docs/guides/safety-best-practices/end-user-ids
User string
// mask string Optional
// An additional image whose fully transparent areas (e.g. where alpha is zero) indicate where image should be edited.
// Must be a valid PNG file, less than 4MB, and have the same dimensions as image.
// User MUST close it if it's like ReadCloser.
Mask io.Reader
// prompt string Required
// A text description of the desired image(s). The maximum length is 1000 characters.
Prompt string
}
func (body ImageEditRequestBody) ToMultipartFormData() (buf *bytes.Buffer, contenttype string, err error) {
if body.Image == nil {
return nil, "", fmt.Errorf("body.Image must not be nil")
}
buf = bytes.NewBuffer(nil)
w := multipart.NewWriter(buf)
imgw, err := w.CreateFormFile("image", "image.png")
if err != nil {
return nil, "", fmt.Errorf("failed to create FormFile: %v", err)
}
if _, err := io.Copy(imgw, body.Image); err != nil {
return nil, "", fmt.Errorf("failed to copy io.Reader to buffer: %v", err)
}
if body.Mask != nil {
maskw, err := w.CreateFormFile("mask", "mask.png")
if err != nil {
return nil, "", err
}
if _, err := io.Copy(maskw, body.Mask); err != nil {
return nil, "", err
}
}
// prompt is required for image edit.
w.WriteField("prompt", body.Prompt)
if body.N > 1 {
w.WriteField("n", fmt.Sprintf("%d", body.N))
}
if body.Size != "" {
w.WriteField("size", body.Size)
}
if body.ResponseFormat != "" {
w.WriteField("response_format", body.ResponseFormat)
}
if body.User != "" {
w.WriteField("user", body.User)
}
if err = w.Close(); err != nil {
return nil, "", err
}
return buf, w.FormDataContentType(), err
}
type ImageEditResponse ImageResponse
type ImageVariationRequestBody struct {
// image Required
// The image to use as the basis for the variation(s). Must be a valid PNG file, less than 4MB, and square.
// User MUST close it if it's like ReadCloser.
Image io.Reader
// n integer Optional Defaults to 1
// The number of images to generate. Must be between 1 and 10.
N int
// size string Optional Defaults to 1024x1024
// The size of the generated images. Must be one of 256x256, 512x512, or 1024x1024.
Size string
// response_format string Optional Defaults to url
// The format in which the generated images are returned. Must be one of url or b64_json.
ResponseFormat string
// user string Optional
// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
// Learn more: https://beta.openai.com/docs/guides/safety-best-practices/end-user-ids
User string
}
func (body ImageVariationRequestBody) ToMultipartFormData() (buf *bytes.Buffer, contenttype string, err error) {
if body.Image == nil {
return nil, "", fmt.Errorf("body.Image must not be nil")
}
buf = bytes.NewBuffer(nil)
w := multipart.NewWriter(buf)
defer w.Close()
imgw, err := w.CreateFormFile("image", "image.png")
if err != nil {
return nil, "", err
}
if _, err := io.Copy(imgw, body.Image); err != nil {
return nil, "", err
}
if body.N > 1 {
w.WriteField("n", fmt.Sprintf("%d", body.N))
}
if body.Size != "" {
w.WriteField("size", body.Size)
}
if body.ResponseFormat != "" {
w.WriteField("response_format", body.ResponseFormat)
}
if body.User != "" {
w.WriteField("user", body.User)
}
return buf, w.FormDataContentType(), err
}
type ImageVariationResponse ImageResponse