fix(openai-image-gen): remove deprecated response_format, use URL download
This commit is contained in:
parent
b3b6d421cc
commit
2acfeb1096
@ -1,6 +1,5 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
import argparse
|
import argparse
|
||||||
import base64
|
|
||||||
import datetime as dt
|
import datetime as dt
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
@ -96,8 +95,8 @@ def request_images(
|
|||||||
if model != "dall-e-2":
|
if model != "dall-e-2":
|
||||||
args["quality"] = quality
|
args["quality"] = quality
|
||||||
|
|
||||||
if model.startswith("dall-e"):
|
# Note: response_format no longer supported by OpenAI Images API
|
||||||
args["response_format"] = "b64_json"
|
# dall-e models now return URLs by default
|
||||||
|
|
||||||
if model.startswith("gpt-image"):
|
if model.startswith("gpt-image"):
|
||||||
if background:
|
if background:
|
||||||
@ -212,12 +211,19 @@ def main() -> int:
|
|||||||
args.output_format,
|
args.output_format,
|
||||||
args.style,
|
args.style,
|
||||||
)
|
)
|
||||||
b64 = res.get("data", [{}])[0].get("b64_json")
|
# OpenAI Images API now returns URLs by default
|
||||||
if not b64:
|
image_url = res.get("data", [{}])[0].get("url")
|
||||||
|
if not image_url:
|
||||||
raise RuntimeError(f"Unexpected response: {json.dumps(res)[:400]}")
|
raise RuntimeError(f"Unexpected response: {json.dumps(res)[:400]}")
|
||||||
image_bytes = base64.b64decode(b64)
|
|
||||||
|
# Download image from URL
|
||||||
filename = f"{idx:03d}-{slugify(prompt)[:40]}.{file_ext}"
|
filename = f"{idx:03d}-{slugify(prompt)[:40]}.{file_ext}"
|
||||||
(out_dir / filename).write_bytes(image_bytes)
|
filepath = out_dir / filename
|
||||||
|
try:
|
||||||
|
urllib.request.urlretrieve(image_url, filepath)
|
||||||
|
except urllib.error.URLError as e:
|
||||||
|
raise RuntimeError(f"Failed to download image from {image_url}: {e}") from e
|
||||||
|
|
||||||
items.append({"prompt": prompt, "file": filename})
|
items.append({"prompt": prompt, "file": filename})
|
||||||
|
|
||||||
(out_dir / "prompts.json").write_text(json.dumps(items, indent=2), encoding="utf-8")
|
(out_dir / "prompts.json").write_text(json.dumps(items, indent=2), encoding="utf-8")
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user