()
| 255 | |
| 256 | |
| 257 | def main() -> int: |
| 258 | args = parse_args() |
| 259 | |
| 260 | _load_env_chain() |
| 261 | if "OPENAI_API_KEY" not in os.environ: |
| 262 | print( |
| 263 | "error: OPENAI_API_KEY not set. Add it to ~/.env or `export OPENAI_API_KEY=...`.", |
| 264 | file=sys.stderr, |
| 265 | ) |
| 266 | return 2 |
| 267 | |
| 268 | if args.mask and not args.image: |
| 269 | print("error: --mask requires --image (edits endpoint only)", file=sys.stderr) |
| 270 | return 2 |
| 271 | |
| 272 | ext = args.output_format or "png" |
| 273 | out_path = Path(args.file).expanduser().resolve() if args.file else default_output_path(args.prompt, ext) |
| 274 | |
| 275 | client = OpenAI() # auto-reads OPENAI_API_KEY |
| 276 | |
| 277 | try: |
| 278 | result = call_edit(client, args) if args.image else call_generate(client, args) |
| 279 | except APIError as e: |
| 280 | print(f"error: {type(e).__name__}: {e}", file=sys.stderr) |
| 281 | return 1 |
| 282 | |
| 283 | data = result.data or [] |
| 284 | if not data: |
| 285 | print(f"error: no image data in response: {result}", file=sys.stderr) |
| 286 | return 1 |
| 287 | |
| 288 | for p in write_outputs(data, out_path, args.n): |
| 289 | print(p) |
| 290 | return 0 |
| 291 | |
| 292 | |
| 293 | if __name__ == "__main__": |
no test coverage detected