Spaces:
Running
Running
Updated Space.
Browse files
app.py
CHANGED
@@ -221,10 +221,10 @@ def safe_eval(expression):
|
|
221 |
raise NameError(f"Use of {name} is not allowed")
|
222 |
return eval(code, {"__builtins__": {}}, allowed_names)
|
223 |
|
224 |
-
def get_api_key(call=
|
225 |
-
if call ==
|
226 |
key = api_key
|
227 |
-
elif call ==
|
228 |
key = oai_api_key
|
229 |
else:
|
230 |
key = api_key
|
@@ -249,7 +249,7 @@ def moderate(messages):
|
|
249 |
f"{base_url}/moderations",
|
250 |
headers={
|
251 |
"Content-Type": "application/json",
|
252 |
-
"Authorization": f"Bearer {get_api_key(call=
|
253 |
},
|
254 |
json={"input": encodeChat(messages)}
|
255 |
)
|
@@ -262,7 +262,7 @@ def moderate(messages):
|
|
262 |
"https://api.openai.com/v1/moderations",
|
263 |
headers={
|
264 |
"Content-Type": "application/json",
|
265 |
-
"Authorization": f"Bearer {get_api_key(call=
|
266 |
},
|
267 |
json={"input": encodeChat(messages)}
|
268 |
)
|
@@ -284,7 +284,7 @@ def moderate(messages):
|
|
284 |
async def streamChat(params):
|
285 |
async with aiohttp.ClientSession() as session:
|
286 |
try:
|
287 |
-
async with session.post(f"{base_url}/chat/completions", headers={"Authorization": f"Bearer {get_api_key(call=
|
288 |
r.raise_for_status()
|
289 |
async for line in r.content:
|
290 |
if line:
|
@@ -300,7 +300,7 @@ async def streamChat(params):
|
|
300 |
continue
|
301 |
except aiohttp.ClientError:
|
302 |
try:
|
303 |
-
async with session.post("https://api.openai.com/v1/chat/completions", headers={"Authorization": f"Bearer {get_api_key(call=
|
304 |
r.raise_for_status()
|
305 |
async for line in r.content:
|
306 |
if line:
|
@@ -323,7 +323,7 @@ def imagine(prompt):
|
|
323 |
f"{base_url}/images/generations",
|
324 |
headers={
|
325 |
"Content-Type": "application/json",
|
326 |
-
"Authorization": f"Bearer {get_api_key(call=
|
327 |
},
|
328 |
json={
|
329 |
"model": "dall-e-3",
|
@@ -340,7 +340,7 @@ def imagine(prompt):
|
|
340 |
"https://api.openai.com/v1/images/generations",
|
341 |
headers={
|
342 |
"Content-Type": "application/json",
|
343 |
-
"Authorization": f"Bearer {get_api_key(call=
|
344 |
},
|
345 |
json={
|
346 |
"model": "dall-e-3",
|
|
|
221 |
raise NameError(f"Use of {name} is not allowed")
|
222 |
return eval(code, {"__builtins__": {}}, allowed_names)
|
223 |
|
224 |
+
def get_api_key(call='api_key'):
|
225 |
+
if call == 'api_key':
|
226 |
key = api_key
|
227 |
+
elif call == 'oai_api_key':
|
228 |
key = oai_api_key
|
229 |
else:
|
230 |
key = api_key
|
|
|
249 |
f"{base_url}/moderations",
|
250 |
headers={
|
251 |
"Content-Type": "application/json",
|
252 |
+
"Authorization": f"Bearer {get_api_key(call='api_key')}"
|
253 |
},
|
254 |
json={"input": encodeChat(messages)}
|
255 |
)
|
|
|
262 |
"https://api.openai.com/v1/moderations",
|
263 |
headers={
|
264 |
"Content-Type": "application/json",
|
265 |
+
"Authorization": f"Bearer {get_api_key(call='oai_api_key')}"
|
266 |
},
|
267 |
json={"input": encodeChat(messages)}
|
268 |
)
|
|
|
284 |
async def streamChat(params):
|
285 |
async with aiohttp.ClientSession() as session:
|
286 |
try:
|
287 |
+
async with session.post(f"{base_url}/chat/completions", headers={"Authorization": f"Bearer {get_api_key(call='api_key')}", "Content-Type": "application/json"}, json=params) as r:
|
288 |
r.raise_for_status()
|
289 |
async for line in r.content:
|
290 |
if line:
|
|
|
300 |
continue
|
301 |
except aiohttp.ClientError:
|
302 |
try:
|
303 |
+
async with session.post("https://api.openai.com/v1/chat/completions", headers={"Authorization": f"Bearer {get_api_key(call='oai_api_key')}", "Content-Type": "application/json"}, json=params) as r:
|
304 |
r.raise_for_status()
|
305 |
async for line in r.content:
|
306 |
if line:
|
|
|
323 |
f"{base_url}/images/generations",
|
324 |
headers={
|
325 |
"Content-Type": "application/json",
|
326 |
+
"Authorization": f"Bearer {get_api_key(call='api_key')}"
|
327 |
},
|
328 |
json={
|
329 |
"model": "dall-e-3",
|
|
|
340 |
"https://api.openai.com/v1/images/generations",
|
341 |
headers={
|
342 |
"Content-Type": "application/json",
|
343 |
+
"Authorization": f"Bearer {get_api_key(call='oai_api_key')}"
|
344 |
},
|
345 |
json={
|
346 |
"model": "dall-e-3",
|