forked from openai/openai-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage.py
More file actions
242 lines (211 loc) · 6.12 KB
/
Copy pathimage.py
File metadata and controls
242 lines (211 loc) · 6.12 KB
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
# WARNING: This interface is considered experimental and may changed in the future without warning.
from typing import Any, List
import openai
from openai import api_requestor, util
from openai.api_resources.abstract import APIResource
class Image(APIResource):
OBJECT_NAME = "images"
@classmethod
def _get_url(cls, action):
return cls.class_url() + f"/{action}"
@classmethod
def create(
cls,
api_key=None,
api_base=None,
api_type=None,
api_version=None,
organization=None,
**params,
):
requestor = api_requestor.APIRequestor(
api_key,
api_base=api_base or openai.api_base,
api_type=api_type,
api_version=api_version,
organization=organization,
)
_, api_version = cls._get_api_type_and_version(api_type, api_version)
response, _, api_key = requestor.request(
"post", cls._get_url("generations"), params
)
return util.convert_to_openai_object(
response, api_key, api_version, organization
)
@classmethod
async def acreate(
cls,
api_key=None,
api_base=None,
api_type=None,
api_version=None,
organization=None,
**params,
):
requestor = api_requestor.APIRequestor(
api_key,
api_base=api_base or openai.api_base,
api_type=api_type,
api_version=api_version,
organization=organization,
)
_, api_version = cls._get_api_type_and_version(api_type, api_version)
response, _, api_key = await requestor.arequest(
"post", cls._get_url("generations"), params
)
return util.convert_to_openai_object(
response, api_key, api_version, organization
)
@classmethod
def _prepare_create_variation(
cls,
image,
api_key=None,
api_base=None,
api_type=None,
api_version=None,
organization=None,
**params,
):
requestor = api_requestor.APIRequestor(
api_key,
api_base=api_base or openai.api_base,
api_type=api_type,
api_version=api_version,
organization=organization,
)
_, api_version = cls._get_api_type_and_version(api_type, api_version)
url = cls._get_url("variations")
files: List[Any] = []
for key, value in params.items():
files.append((key, (None, value)))
files.append(("image", ("image", image, "application/octet-stream")))
return requestor, url, files
@classmethod
def create_variation(
cls,
image,
api_key=None,
api_base=None,
api_type=None,
api_version=None,
organization=None,
**params,
):
requestor, url, files = cls._prepare_create_variation(
image,
api_key,
api_base,
api_type,
api_version,
organization,
**params,
)
response, _, api_key = requestor.request("post", url, files=files)
return util.convert_to_openai_object(
response, api_key, api_version, organization
)
@classmethod
async def acreate_variation(
cls,
image,
api_key=None,
api_base=None,
api_type=None,
api_version=None,
organization=None,
**params,
):
requestor, url, files = cls._prepare_create_variation(
image,
api_key,
api_base,
api_type,
api_version,
organization,
**params,
)
response, _, api_key = await requestor.arequest("post", url, files=files)
return util.convert_to_openai_object(
response, api_key, api_version, organization
)
@classmethod
def _prepare_create_edit(
cls,
image,
mask=None,
api_key=None,
api_base=None,
api_type=None,
api_version=None,
organization=None,
**params,
):
requestor = api_requestor.APIRequestor(
api_key,
api_base=api_base or openai.api_base,
api_type=api_type,
api_version=api_version,
organization=organization,
)
_, api_version = cls._get_api_type_and_version(api_type, api_version)
url = cls._get_url("edits")
files: List[Any] = []
for key, value in params.items():
files.append((key, (None, value)))
files.append(("image", ("image", image, "application/octet-stream")))
if mask is not None:
files.append(("mask", ("mask", mask, "application/octet-stream")))
return requestor, url, files
@classmethod
def create_edit(
cls,
image,
mask=None,
api_key=None,
api_base=None,
api_type=None,
api_version=None,
organization=None,
**params,
):
requestor, url, files = cls._prepare_create_edit(
image,
mask,
api_key,
api_base,
api_type,
api_version,
organization,
**params,
)
response, _, api_key = requestor.request("post", url, files=files)
return util.convert_to_openai_object(
response, api_key, api_version, organization
)
@classmethod
async def acreate_edit(
cls,
image,
mask=None,
api_key=None,
api_base=None,
api_type=None,
api_version=None,
organization=None,
**params,
):
requestor, url, files = cls._prepare_create_edit(
image,
mask,
api_key,
api_base,
api_type,
api_version,
organization,
**params,
)
response, _, api_key = await requestor.arequest("post", url, files=files)
return util.convert_to_openai_object(
response, api_key, api_version, organization
)