Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -277,30 +277,36 @@ async def v1_images_generations(request: Request, authorization: str = Header(No
|
|
| 277 |
images = images[:n]
|
| 278 |
|
| 279 |
openai_data = []
|
| 280 |
-
for img in images:
|
| 281 |
-
# If it looks like a data URL already
|
| 282 |
-
if isinstance(img, str) and img.startswith("data:image"):
|
| 283 |
-
# Already a data URL; extract base64 if needed
|
| 284 |
-
b64_part = img.split("base64,", 1)[-1]
|
| 285 |
-
if response_format == "b64_json":
|
| 286 |
-
openai_data.append({"b64_json": b64_part})
|
| 287 |
-
else:
|
| 288 |
-
openai_data.append({"url": img})
|
| 289 |
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 298 |
|
| 299 |
-
result = {
|
| 300 |
-
"created": int(time.time()),
|
| 301 |
-
"data": openai_data,
|
| 302 |
-
}
|
| 303 |
-
return JSONResponse(result, headers={"Access-Control-Allow-Origin": "*"})
|
| 304 |
|
| 305 |
|
| 306 |
|
|
|
|
| 277 |
images = images[:n]
|
| 278 |
|
| 279 |
openai_data = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 280 |
|
| 281 |
+
async def fetch_and_b64(url: str) -> str:
|
| 282 |
+
async with httpx.AsyncClient(timeout=60) as client:
|
| 283 |
+
r = await client.get(url)
|
| 284 |
+
r.raise_for_status()
|
| 285 |
+
return base64.b64encode(r.content).decode("utf-8")
|
| 286 |
+
|
| 287 |
+
for img in images:
|
| 288 |
+
# Case 1: Already base64
|
| 289 |
+
if isinstance(img, str) and img.startswith("data:image"):
|
| 290 |
+
b64_part = img.split("base64,", 1)[1]
|
| 291 |
+
openai_data.append({"b64_json": b64_part})
|
| 292 |
+
continue
|
| 293 |
+
|
| 294 |
+
# Case 2: Looks like base64 (no URL)
|
| 295 |
+
if isinstance(img, str) and not img.startswith("http"):
|
| 296 |
+
openai_data.append({"b64_json": img})
|
| 297 |
+
continue
|
| 298 |
+
|
| 299 |
+
# Case 3: It is a URL → fetch + convert to base64
|
| 300 |
+
if isinstance(img, str) and img.startswith("http"):
|
| 301 |
+
try:
|
| 302 |
+
b64 = await fetch_and_b64(img)
|
| 303 |
+
openai_data.append({"b64_json": b64})
|
| 304 |
+
except Exception as e:
|
| 305 |
+
raise HTTPException(
|
| 306 |
+
status_code=502,
|
| 307 |
+
detail=f"Failed to fetch image from URL returned by Bytez: {str(e)}"
|
| 308 |
+
)
|
| 309 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 310 |
|
| 311 |
|
| 312 |
|