File size: 3,148 Bytes
c47ec10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python3
# Test script: directly call https://chatgpt.com/backend-api/codex/responses to generate one 2K image.
# Only edit ACCESS_TOKEN, then run: python codex_responses_image_test.py
# Fixed request parameters:
#   prompt: A highly detailed square 2K image of a quiet futuristic library at sunrise
#   responses model: gpt-5.5
#   image model: gpt-image-2
#   size: 2048x2048
#   quality: auto
#   output_format: png
#   output file: codex_4k.png

import base64
import json
import time
import urllib.request

ACCESS_TOKEN = ""


def parse_events(raw):
    ctype, text = raw.headers.get("content-type", ""), raw.read().decode("utf-8", "replace")
    if "application/json" in ctype:
        return [json.loads(text)]
    events, lines = [], []
    for line in text.splitlines() + [""]:
        if not line:
            if lines:
                data = "\n".join(lines).strip()
                if data and data != "[DONE]":
                    events.append(json.loads(data))
                lines = []
        elif line.startswith("data:"):
            lines.append(line[5:].lstrip())
    return events


def find_images(value):
    if isinstance(value, dict):
        if value.get("type") == "image_generation_call" and isinstance(value.get("result"), str):
            result = value["result"].strip()
            return [result.split(",", 1)[1] if result.startswith("data:image/") else result]
        return [image for item in value.values() for image in find_images(item)]
    if isinstance(value, list):
        return [image for item in value for image in find_images(item)]
    return []


def main():
    start_time = time.time()
    body = {
        "model": "gpt-5.5",
        "instructions": "Use the image_generation tool to create exactly one image for the user's request. Return the generated image result.",
        "store": False,
        "input": [{"role": "user", "content": [{"type": "input_text", "text": "A highly detailed square 2K image of a quiet futuristic library at sunrise"}]}],
        "tools": [{
            "type": "image_generation",
            "model": "gpt-image-2",
            "action": "generate",
            "size": "3840x2160",
            "quality": "auto",
            "output_format": "png"
        }],
        "tool_choice": {"type": "image_generation"},
        "stream": True
    }
    headers = {"Authorization": f"Bearer {ACCESS_TOKEN}", "Content-Type": "application/json"}

    req = urllib.request.Request("https://chatgpt.com/backend-api/codex/responses", json.dumps(body).encode(), headers, method="POST")
    try:
        images = find_images(parse_events(urllib.request.urlopen(req, timeout=1200)))
    except urllib.error.HTTPError as error:
        raise SystemExit(f"HTTP {error.code}: {error.read().decode('utf-8', 'replace')[:1000]}")

    if not images:
        raise SystemExit("No image result found in response")

    with open("codex_4k.png", "wb") as file:
        file.write(base64.b64decode(images[0]))
    print("saved codex_4k.png")
    end_time = time.time()
    print(f"total time: {end_time - start_time:.2f} seconds")


if __name__ == "__main__":
    main()