forked from emacs-openai/openai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenai-image.el
259 lines (224 loc) · 9.79 KB
/
openai-image.el
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
;;; openai-image.el --- Create image with OpenAI -*- lexical-binding: t; -*-
;; Copyright (C) 2023-2024 Shen, Jen-Chieh
;; This file is not part of GNU Emacs.
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; Create image with OpenAI.
;;
;; See https://beta.openai.com/docs/api-reference/images/create
;;
;;; Code:
(require 'openai)
;;
;;; API
(cl-defun openai-image ( prompt callback
&key
(base-url openai-base-url)
(parameters openai-parameters)
(content-type "application/json")
(key openai-key)
org-id
n
size
response-format
(user openai-user))
"Create an image given a PROMPT.
Arguments PROMPT and CALLBACK are required for this type of request. PROMPT is
either the question or instruction to OpenAI. CALLBACK is the execution after
request is made.
Arguments BASE-URL, PARAMETERS, CONTENT-TYPE, KEY, ORG-ID and USER are global
options; however, you can overwrite the value by passing it in.
The rest of the arugments are optional, please see OpenAI API reference page
for more information. Arguments here refer to N, SIZE, and RESPONSE-FORMAT."
(openai-request (concat base-url "/images/generations")
:type "POST"
:params parameters
:headers (openai--headers content-type key org-id)
:data (openai--json-encode
`(("prompt" . ,prompt)
("n" . ,n)
("size" . ,size)
("response_format" . ,response-format)
("user" . ,user)))
:parser 'json-read
:complete (cl-function
(lambda (&key data &allow-other-keys)
(funcall callback data)))))
(cl-defun openai-image-edit ( image prompt callback
&key
(base-url openai-base-url)
(parameters openai-parameters)
content-type
(key openai-key)
org-id
mask
n
size
response-format
(user openai-user))
"Create an edited or extended image given an original IMAGE and a PROMPT.
Arguments IMAGE, PROMPT and CALLBACK are required for this type of request.
PROMPT is a text description of the desired image(s). IMAGE is the image file
to edit. CALLBACK is the execution after request is made.
Arguments BASE-URL, PARAMETERS, CONTENT-TYPE, KEY, ORG-ID and USER are global
options; however, you can overwrite the value by passing it in.
The rest of the arguments are optional, please see OpenAI API reference page
for more information. Arguments here refer to MASK, N, SIZE, and
RESPONSE-FORMAT."
(openai-request (concat base-url "/images/edits")
:type "POST"
:params parameters
:headers (openai--headers content-type key org-id)
:data (openai--json-encode
`(("image" . ,image)
("prompt" . ,prompt)
("mask" . ,mask)
("n" . ,n)
("size" . ,size)
("response_format" . ,response-format)
("user" . ,user)))
:parser 'json-read
:complete (cl-function
(lambda (&key data &allow-other-keys)
(funcall callback data)))))
(cl-defun openai-image-variation ( image callback
&key
(base-url openai-base-url)
(parameters openai-parameters)
content-type
(key openai-key)
org-id
mask
n
size
response-format
(user openai-user))
"Create a variation of a given IMAGE.
Argument CALLBACK is function with data pass in, and the argument IMAGE must
be a valid PNG file, less than 4MB, and square.
Arguments BASE-URL, PARAMETERS, CONTENT-TYPE, KEY, ORG-ID and USER are global
options; however, you can overwrite the value by passing it in.
The rest of the arugments are optional, please see OpenAI API reference page
for more information. Arguments here refer to MASK, N, SIZE, and
RESPONSE-FORMAT."
(openai-request (concat base-url "/images/variations")
:type "POST"
:params parameters
:headers (openai--headers content-type key org-id)
:data (openai--json-encode
`(("image" . ,image)
("mask" . ,mask)
("n" . ,n)
("size" . ,size)
("response_format" . ,response-format)
("user" . ,user)))
:parser 'json-read
:complete (cl-function
(lambda (&key data &allow-other-keys)
(funcall callback data)))))
;;
;;; Util
(defun openai--select-png-files (candidate)
"Return t if CANDIDATE is either directory or a PNG file."
(or (string-suffix-p ".png" candidate t) ; only support png
(file-directory-p candidate))) ; allow navigation
;;
;;; Application
(defcustom openai-image-n 1
"The number of images to generate. Must be between 1 and 10."
:type 'integer
:group 'openai)
(defcustom openai-image-size "1024x1024"
"The size of the generated images.
Must be one of `256x256', `512x512', or `1024x1024'."
:type 'string
:group 'openai)
(defcustom openai-image-response-format "url"
"The format in which the generated images are returned.
Must be one of `url' or `b64_json'."
:type 'string
:group 'openai)
(defvar openai-image-entries nil
"Async images entries.")
(tblui-define
openai-image
"OpenAI Image" "Display image information from OpenAI."
(lambda () openai-image-entries)
[("URL" 200 nil)]
nil)
;;;###autoload
(defun openai-image-prompt (prompt)
"Use PROMPT to ask for image, and display result in a buffer."
(interactive (list (read-string "Describe image: ")))
(setq openai-image-entries nil)
(openai-image prompt
(lambda (data)
(let ((id 0))
(let-alist data
(mapc (lambda (images)
(dolist (image images)
(push (list (number-to-string id)
(vector (cdr image)))
openai-image-entries)
(cl-incf id)))
.data)))
(openai-image-goto-ui)
:size openai-image-size
:n openai-image-n
:response-format openai-image-response-format)))
;;;###autoload
(defun openai-image-edit-prompt ()
"Use prompt to ask for image, and display result in a buffer."
(interactive)
(setq openai-image-entries nil)
(let ((image (read-file-name "Select image file: " nil nil t nil
#'openai--select-png-files))
(prompt (read-string "Describe image: ")))
(openai-image-edit image prompt
(lambda (data)
(let ((id 0))
(let-alist data
(mapc (lambda (images)
(dolist (image images)
(push (list (number-to-string id)
(vector (cdr image)))
openai-image-entries)
(cl-incf id)))
.data)))
(openai-image-goto-ui))
:size openai-image-size
:n openai-image-n
:response-format openai-image-response-format)))
;;;###autoload
(defun openai-image-variation-prompt (image)
"Prompt to select an IMAGE file, and display result in a buffer."
(interactive (list (read-file-name "Select image file: " nil nil t nil
#'openai--select-png-files)))
(setq openai-image-entries nil)
(openai-image-variation image
(lambda (data)
(let ((id 0))
(let-alist data
(mapc (lambda (images)
(dolist (image images)
(push (list (number-to-string id)
(vector (cdr image)))
openai-image-entries)
(cl-incf id)))
.data)))
(openai-image-goto-ui))
:size openai-image-size
:n openai-image-n
:response-format openai-image-response-format))
(provide 'openai-image)
;;; openai-image.el ends here