File size: 1,788 Bytes
7b6142f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Send request.t2i.json to a MiniMax-H3-Image server and write the PNG.

    python3 t2i.py [--url http://127.0.0.1:30000] [--prompt FILE] [--size WxH] [--out t2i.png]

--prompt and --size replace the prompt and the size in the request.

Uses only the Python standard library.
"""

import argparse
import base64
import json
import urllib.request
from pathlib import Path

# absolute(), not resolve(): in the Hugging Face cache this file is a symlink into blobs/.
HERE = Path(__file__).absolute().parent


def main() -> None:
    parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument("--url", default="http://127.0.0.1:30000", help="server base URL")
    parser.add_argument("--request", type=Path, default=HERE / "request.t2i.json", help="JSON request body")
    parser.add_argument("--prompt", type=Path, help="prompt file; replaces the prompt in the request")
    parser.add_argument("--size", help="WIDTHxHEIGHT; replaces the size in the request")
    parser.add_argument("--out", default="t2i.png")
    args = parser.parse_args()

    body = json.loads(args.request.read_text())
    if args.prompt:
        body["prompt"] = args.prompt.read_text()
    if args.size:
        body["size"] = args.size

    request = urllib.request.Request(
        f"{args.url.rstrip('/')}/v1/images/generations",
        data=json.dumps(body).encode(),
        headers={"Content-Type": "application/json"},
    )
    with urllib.request.urlopen(request, timeout=1800) as response:
        payload = json.load(response)
    with open(args.out, "wb") as image:
        image.write(base64.b64decode(payload["data"][0]["b64_json"]))
    print(f"wrote {args.out}")


if __name__ == "__main__":
    main()