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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
+
from flask import Flask, request, jsonify
|
4 |
+
|
5 |
+
import openai
|
6 |
+
|
7 |
+
|
8 |
+
|
9 |
+
app = Flask(__name__)
|
10 |
+
|
11 |
+
|
12 |
+
|
13 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
14 |
+
|
15 |
+
|
16 |
+
|
17 |
+
global requests_list
|
18 |
+
|
19 |
+
requests_list = []
|
20 |
+
|
21 |
+
|
22 |
+
|
23 |
+
@app.route("/")
|
24 |
+
|
25 |
+
def home():
|
26 |
+
|
27 |
+
return jsonify(requests_list)
|
28 |
+
|
29 |
+
|
30 |
+
|
31 |
+
@app.route("/api/completions", methods=["POST"])
|
32 |
+
|
33 |
+
def completions():
|
34 |
+
|
35 |
+
payload = request.json
|
36 |
+
|
37 |
+
requests_list.append(payload)
|
38 |
+
|
39 |
+
response = openai.Completion.create(
|
40 |
+
|
41 |
+
model=payload["model"],
|
42 |
+
|
43 |
+
prompt=payload["prompt"],
|
44 |
+
|
45 |
+
max_tokens=payload.get("max_tokens", 100),
|
46 |
+
|
47 |
+
temperature=payload.get("temperature", 0.8),
|
48 |
+
|
49 |
+
)
|
50 |
+
|
51 |
+
return jsonify(response.choices[0].text.strip())
|
52 |
+
|
53 |
+
|
54 |
+
|
55 |
+
@app.route("/api/chat/completions", methods=["POST"])
|
56 |
+
|
57 |
+
def chat():
|
58 |
+
|
59 |
+
payload = request.json
|
60 |
+
|
61 |
+
requests_list.append(payload)
|
62 |
+
|
63 |
+
response = openai.ChatCompletion.create(
|
64 |
+
|
65 |
+
model=payload["model"],
|
66 |
+
|
67 |
+
messages=payload["messages"]
|
68 |
+
|
69 |
+
)
|
70 |
+
|
71 |
+
return jsonify(response)
|
72 |
+
|
73 |
+
|
74 |
+
|
75 |
+
@app.route("/api/edits", methods=["POST"])
|
76 |
+
|
77 |
+
def edits():
|
78 |
+
|
79 |
+
payload = request.json
|
80 |
+
|
81 |
+
requests_list.append(payload)
|
82 |
+
|
83 |
+
response = openai.Edit.create(
|
84 |
+
|
85 |
+
model=payload["model"],
|
86 |
+
|
87 |
+
input=payload["input"],
|
88 |
+
|
89 |
+
instruction=payload["instruction"]
|
90 |
+
|
91 |
+
)
|
92 |
+
|
93 |
+
return jsonify(response.choices[0].text)
|
94 |
+
|
95 |
+
|
96 |
+
|
97 |
+
@app.route('/api/audio/transcriptions', methods=['POST'])
|
98 |
+
|
99 |
+
def transcriptions():
|
100 |
+
|
101 |
+
audio_file = request.files['file']
|
102 |
+
|
103 |
+
request_info = {
|
104 |
+
|
105 |
+
'filename': audio_file.filename,
|
106 |
+
|
107 |
+
'content_type': audio_file.content_type,
|
108 |
+
|
109 |
+
'headers': dict(request.headers)
|
110 |
+
|
111 |
+
}
|
112 |
+
|
113 |
+
requests_list.append(request_info)
|
114 |
+
|
115 |
+
|
116 |
+
|
117 |
+
# save to a temporary file
|
118 |
+
|
119 |
+
tmp_file_name = 'temp_audio.wav'
|
120 |
+
|
121 |
+
audio_file.save(tmp_file_name)
|
122 |
+
|
123 |
+
|
124 |
+
|
125 |
+
# use the saved file for transcription
|
126 |
+
|
127 |
+
with open(tmp_file_name, 'rb') as f:
|
128 |
+
|
129 |
+
response = openai.Audio.transcribe("whisper-1", f)
|
130 |
+
|
131 |
+
|
132 |
+
|
133 |
+
# delete the temporary file
|
134 |
+
|
135 |
+
os.remove(tmp_file_name)
|
136 |
+
|
137 |
+
|
138 |
+
|
139 |
+
return jsonify(response)
|
140 |
+
|
141 |
+
|
142 |
+
|
143 |
+
@app.route("/api/images", methods=["POST"])
|
144 |
+
|
145 |
+
def images():
|
146 |
+
|
147 |
+
payload = request.json
|
148 |
+
|
149 |
+
requests_list.append(payload)
|
150 |
+
|
151 |
+
response = openai.Image.create(
|
152 |
+
|
153 |
+
prompt=payload["prompt"],
|
154 |
+
|
155 |
+
n=payload.get("n", 1),
|
156 |
+
|
157 |
+
size=payload.get("size", "512x512")
|
158 |
+
|
159 |
+
)
|
160 |
+
|
161 |
+
return jsonify(response)
|
162 |
+
|
163 |
+
|
164 |
+
|
165 |
+
@app.route("/api/images/edits", methods=["POST"])
|
166 |
+
|
167 |
+
def image_edits():
|
168 |
+
|
169 |
+
image_file = request.files["image"]
|
170 |
+
|
171 |
+
prompt = request.form["prompt"]
|
172 |
+
|
173 |
+
n = int(request.form.get("n", 1))
|
174 |
+
|
175 |
+
size = request.form.get("size", "1024x1024")
|
176 |
+
|
177 |
+
response_format = request.form.get("response_format", "url")
|
178 |
+
|
179 |
+
|
180 |
+
|
181 |
+
request_info = {
|
182 |
+
|
183 |
+
'filename': image_file.filename,
|
184 |
+
|
185 |
+
'content_type': image_file.content_type,
|
186 |
+
|
187 |
+
'headers': dict(request.headers),
|
188 |
+
|
189 |
+
'prompt': prompt,
|
190 |
+
|
191 |
+
'n': n,
|
192 |
+
|
193 |
+
'size': size,
|
194 |
+
|
195 |
+
'response_format': response_format
|
196 |
+
|
197 |
+
}
|
198 |
+
|
199 |
+
requests_list.append(request_info)
|
200 |
+
|
201 |
+
|
202 |
+
|
203 |
+
response = openai.Image.create_edit(
|
204 |
+
|
205 |
+
image=image_file,
|
206 |
+
|
207 |
+
prompt=prompt,
|
208 |
+
|
209 |
+
n=n,
|
210 |
+
|
211 |
+
size=size,
|
212 |
+
|
213 |
+
response_format=response_format
|
214 |
+
|
215 |
+
)
|
216 |
+
|
217 |
+
|
218 |
+
|
219 |
+
return jsonify(response)
|
220 |
+
|
221 |
+
|
222 |
+
|
223 |
+
@app.route("/api/images/variations", methods=["POST"])
|
224 |
+
|
225 |
+
def image_variations():
|
226 |
+
|
227 |
+
image_file = request.files["image"]
|
228 |
+
|
229 |
+
n = request.form.get("n", 1)
|
230 |
+
|
231 |
+
size = request.form.get("size", "1024x1024")
|
232 |
+
|
233 |
+
|
234 |
+
|
235 |
+
request_info = {
|
236 |
+
|
237 |
+
'filename': image_file.filename,
|
238 |
+
|
239 |
+
'content_type': image_file.content_type,
|
240 |
+
|
241 |
+
'headers': dict(request.headers),
|
242 |
+
|
243 |
+
'n': n,
|
244 |
+
|
245 |
+
'size': size
|
246 |
+
|
247 |
+
}
|
248 |
+
|
249 |
+
requests_list.append(request_info)
|
250 |
+
|
251 |
+
|
252 |
+
|
253 |
+
response = openai.Image.create_variation(
|
254 |
+
|
255 |
+
image=image_file,
|
256 |
+
|
257 |
+
n=int(n),
|
258 |
+
|
259 |
+
size=size
|
260 |
+
|
261 |
+
)
|
262 |
+
|
263 |
+
return jsonify(response)
|
264 |
+
|
265 |
+
|
266 |
+
|
267 |
+
@app.route("/api/moderations", methods=["POST"])
|
268 |
+
|
269 |
+
def moderations():
|
270 |
+
|
271 |
+
payload = request.json
|
272 |
+
|
273 |
+
requests_list.append(payload)
|
274 |
+
|
275 |
+
response = openai.Moderation.create(
|
276 |
+
|
277 |
+
input=payload["input"]
|
278 |
+
|
279 |
+
)
|
280 |
+
|
281 |
+
return jsonify(response.results)
|
282 |
+
|
283 |
+
|
284 |
+
|
285 |
+
if __name__ == "__main__":
|
286 |
+
|
287 |
+
port = int(os.environ.get("PORT", 5000))
|
288 |
+
|
289 |
+
app.run(host="0.0.0.0", port=port)
|