/
image_variation.go
114 lines (92 loc) · 3.16 KB
/
image_variation.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
package openai
import (
"os"
"github.com/goloop/g"
)
// Check if ImageVariationRequest implements Requester interface.
var _ Requester = (*ImageVariationRequest)(nil)
// ImageVariationRequest represents a request to the OpenAI Image Variation API.
// The image field is required and must be a valid PNG file, less than 4MB,
// and square. The optional fields include n (number of images to generate,
// default is 1), size (the size of the generated images, default is 1024x1024),
// response_format (the format in which the images are returned, default is url),
// and user (a unique identifier representing your end-user).
type ImageVariationRequest struct {
Image *os.File `json:"image"` // Base64-encoded PNG file
N int `json:"n,omitempty"` // Number of images to generate
Size string `json:"size,omitempty"` // Size of the generated images
ResponseFormat string `json:"response_format,omitempty"` // Format of the returned images
User string `json:"user,omitempty"` // Unique identifier of the end-user
}
type ImageVariationData struct {
// The URL where the generated image can be found.
URL string `json:"url"`
// B64 is the base64 encoded image data.
Base64 string `json:"b64_json,omitempty"`
}
// ImageVariationResponse represents a response from the OpenAI Image
// Variation API. The created field represents the timestamp of creation,
// and the data field includes the generated image data.
type ImageVariationResponse struct {
Created int64 `json:"created"` // timestamp of creation
Data []ImageVariationData `json:"data"` // generated image data
// Is the number of parallel tasks to use when saving images.
parallelTasks int
}
func (r *ImageVariationResponse) Save(path string) error {
if len(r.Data) == 0 {
return nil
}
if r.Data[0].URL != "" {
items := make([]string, len(r.Data))
for i, data := range r.Data {
items[i] = data.URL
}
return saveByURL(path, g.Value(r.parallelTasks, parallelTasks), items)
}
if r.Data[0].Base64 != "" {
items := make([]string, len(r.Data))
for i, data := range r.Data {
items[i] = data.Base64
}
return saveByBase64(
path,
g.Value(r.parallelTasks, parallelTasks),
items,
)
}
return nil
}
// OpenImageFile reads an image from a file and assigns the *os.File
// value to the Image field of the request.
func (r *ImageVariationRequest) OpenImageFile(path string) error {
file, err := os.Open(path)
if err != nil {
return err
}
// Assign the file to the Image field.
r.Image = file
return nil
}
func (r *ImageVariationRequest) Error() error {
if r.Image == nil {
return ErrImageRequired
}
if !g.In(r.ResponseFormat, validImageResponseFormats...) {
return ErrInvalidResponseFormat
}
if !g.In(r.Size, validImageSizes...) {
return ErrInvalidSize
}
return nil
}
// CloseImageFile closes the Image file descriptor associated with the request.
func (r *ImageVariationRequest) CloseImageFile() {
if r.Image != nil {
r.Image.Close()
}
}
// Flush closes the files descriptors associated with the request.
func (r *ImageVariationRequest) Flush() {
r.CloseImageFile()
}