examples/context_ir.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Enhance a prompt with the MiniMax H3-Image-Context-IR API. |
| 3 | |
| 4 | export MINIMAX_API_BASE=https://api.minimax.cn # CN; Global: https://api.minimax.io |
| 5 | export MINIMAX_API_KEY=<API key> |
| 6 | python3 context_ir.py t2i # instruction.t2i.txt |
| 7 | python3 context_ir.py edit # instruction.edit.txt with reference1.png |
| 8 | |
| 9 | Sends the instruction (and, for edit, the reference images) to POST /v1/h3_image_context_ir |
| 10 | and writes the enhanced prompt to revised_prompt.txt. Unless --size is given, the API also |
| 11 | picks the aspect ratio; the matching size of about 16 megapixels is written to |
| 12 | revised_size.txt. Pass the files to t2i.py / edit.py with --prompt and --size. Uses only the |
| 13 | Python standard library. |
| 14 | """ |
| 15 | |
| 16 | import argparse |
| 17 | import base64 |
| 18 | import json |
| 19 | import math |
| 20 | import mimetypes |
| 21 | import os |
| 22 | import urllib.request |
| 23 | from pathlib import Path |
| 24 | |
| 25 | # absolute(), not resolve(): in the Hugging Face cache this file is a symlink into blobs/. |
| 26 | HERE = Path(__file__).absolute().parent |
| 27 | AREA = 4096 * 4096 |
| 28 | |
| 29 | |
| 30 | def data_uri(path: Path) -> str: |
| 31 | content_type = mimetypes.guess_type(path.name)[0] |
| 32 | if content_type is None: |
| 33 | raise ValueError(f"unknown image type: {path}") |
| 34 | return f"data:{content_type};base64,{base64.b64encode(path.read_bytes()).decode()}" |
| 35 | |
| 36 | |
| 37 | def size_for(aspect_ratio: str) -> str: |
| 38 | """About 16 megapixels at the ratio, both sides multiples of 32 (matches the README Sizes table).""" |
| 39 | x, y = (int(v) for v in aspect_ratio.split(":")) |
| 40 | width = round(math.sqrt(AREA * x / y) / 32) * 32 |
| 41 | height = round(math.sqrt(AREA * y / x) / 32) * 32 |
| 42 | return f"{width}x{height}" |
| 43 | |
| 44 | |
| 45 | def post(path: str, body: dict) -> dict: |
| 46 | request = urllib.request.Request( |
| 47 | f"{os.environ['MINIMAX_API_BASE'].rstrip('/')}{path}", |
| 48 | data=json.dumps(body).encode(), |
| 49 | headers={"Authorization": f"Bearer {os.environ['MINIMAX_API_KEY']}", "Content-Type": "application/json"}, |
| 50 | ) |
| 51 | with urllib.request.urlopen(request, timeout=600) as response: |
| 52 | return json.load(response) |
| 53 | |
| 54 | |
| 55 | def request_body(task: str, instruction: Path, images: list, size) -> dict: |
| 56 | body = {"model": "MiniMax-H3-Image", "prompt": instruction.read_text().strip()} |
| 57 | if task == "edit": |
| 58 | body["images"] = [{"image_url": data_uri(path)} for path in images or [HERE / "reference1.png"]] |
| 59 | if size: |
| 60 | body["size"] = size |
| 61 | return body |
| 62 | |
| 63 | |
| 64 | def add_task_arguments(parser: argparse.ArgumentParser) -> None: |
| 65 | parser.add_argument("task", choices=["t2i", "edit"]) |
| 66 | parser.add_argument("--instruction", type=Path, help="default: instruction.<task>.txt") |
| 67 | parser.add_argument("--image", action="append", type=Path, |
| 68 | help="edit only: reference image; repeat for <Picture 2>, ... (default: reference1.png)") |
| 69 | parser.add_argument("--size", help="WIDTHxHEIGHT of the image you will generate (default: chosen by the API)") |
| 70 | |
| 71 | |
| 72 | def main() -> None: |
| 73 | parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) |
| 74 | add_task_arguments(parser) |
| 75 | parser.add_argument("--out", default="revised_prompt.txt") |
| 76 | parser.add_argument("--size-out", default="revised_size.txt", help="written when the API picks the aspect ratio") |
| 77 | args = parser.parse_args() |
| 78 | |
| 79 | instruction = args.instruction or HERE / f"instruction.{args.task}.txt" |
| 80 | result = post("/v1/h3_image_context_ir", request_body(args.task, instruction, args.image, args.size)) |
| 81 | Path(args.out).write_text(result["revised_prompt"]) |
| 82 | summary = {"request_id": result.get("request_id"), "usage": result.get("usage")} |
| 83 | if "aspect_ratio" in result: |
| 84 | summary["aspect_ratio"] = result["aspect_ratio"] |
| 85 | summary["size"] = size_for(result["aspect_ratio"]) |
| 86 | Path(args.size_out).write_text(summary["size"]) |
| 87 | print(json.dumps(summary)) |
| 88 | |
| 89 | |
| 90 | if __name__ == "__main__": |
| 91 | main() |
| 92 | |