"""A local stand-in for an OpenAI-compatible API, for the end-to-end tests (no network, no spend): POST /v1/images/edits multipart; returns a PNG of the requested size (the last input image, tinted) + usage POST /v1/images/generations JSON; returns a flat PNG of the requested size + usage POST /v1/chat/completions JSON; returns a six-section prompt naming (N = image parts: the pictures, then the contact sheet) Every request is appended to LOG (one JSON line: path, fields, number of images). While LOG.quota exists every call answers 429 insufficient_quota. Prints the port it listens on. python fake_openai.py LOG [PORT] """ import base64 import email.parser import email.policy import io import json import re import sys from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer from pathlib import Path from PIL import Image, ImageOps LOG = Path(sys.argv[1]) USAGE = {"input_tokens": 1500, "input_tokens_details": {"text_tokens": 500, "image_tokens": 1000}, "output_tokens": 2000, "total_tokens": 3500, "output_tokens_details": {"image_tokens": 2000, "text_tokens": 0}} def png(im): buf = io.BytesIO() im.save(buf, "PNG") return base64.b64encode(buf.getvalue()).decode() def prompt(n): pics = " ".join(f" is a frame of the target video." for k in range(n)) return (f"subject_definitions:\n