File size: 4,932 Bytes
9d8d25c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env python3
# SPDX-License-Identifier: Apache-2.0
"""Client of the rdt2-base-p150 server (Python 3 standard library only; run it by path, no install needed).

    python3 client.py --url http://127.0.0.1:20000                                  # the shipped sample
    python3 client.py --left left.png --right right.png --instruction "Pick up the apple." --url ...
    python3 client.py --left l.jpg --right r.jpg --instruction "..." --state 0,0,...,0 --seed 7 --out req.json

Builds the ``POST /predict`` body ``{"images": [<base64 left_stereo>, <base64 right_stereo>], "instruction": ...,
"state": [20 floats] (optional), "seed": int (optional), "prompt_bucket": int (optional)}`` from image files (PNG or
JPEG, sent as they are) and prints the response (``--out``: write the request instead of / before sending it).
"""
from __future__ import annotations

import argparse
import base64
import json
import sys
import urllib.error
import urllib.request
from pathlib import Path
from typing import Any, Dict, List, Optional, Sequence, Tuple

HERE = Path(__file__).resolve().parent
SAMPLES = HERE.parent / "samples"
SAMPLE_LEFT = SAMPLES / "ur5e_bimanual_example_0_left_stereo.png"
SAMPLE_RIGHT = SAMPLES / "ur5e_bimanual_example_0_right_stereo.png"
SAMPLE_REFERENCE = SAMPLES / "ur5e_bimanual_example_0.reference.json"


def b64file(path: Any) -> str:
    return base64.b64encode(Path(path).read_bytes()).decode("ascii")


def build_request(left: Any, right: Any, instruction: str, state: Optional[Sequence[float]] = None,
                  seed: Optional[int] = None, prompt_bucket: Optional[int] = None) -> Dict[str, Any]:
    req: Dict[str, Any] = {"images": [b64file(left), b64file(right)], "instruction": instruction}
    if state is not None:
        req["state"] = [float(x) for x in state]
    if seed is not None:
        req["seed"] = int(seed)
    if prompt_bucket is not None:
        req["prompt_bucket"] = int(prompt_bucket)
    return req


def sample_request() -> Dict[str, Any]:
    """The request of the shipped sample (instruction, seed and state from its stored reference)."""
    ref = json.loads(SAMPLE_REFERENCE.read_text())
    r = ref["request"]
    return build_request(SAMPLE_LEFT, SAMPLE_RIGHT, r["instruction"], r.get("state"), r.get("seed"))


def post(url: str, payload: Dict[str, Any], timeout: float = 120.0) -> Tuple[int, Dict[str, Any]]:
    """POST ``payload`` to ``<url>/predict`` -> ``(status, json body)`` (HTTP errors are returned, not raised)."""
    data = json.dumps(payload).encode("utf-8")
    r = urllib.request.Request(url.rstrip("/") + "/predict", data=data, headers={"Content-Type": "application/json"})
    try:
        with urllib.request.urlopen(r, timeout=timeout) as resp:
            return resp.status, json.loads(resp.read().decode("utf-8"))
    except urllib.error.HTTPError as e:
        body = e.read().decode("utf-8", "replace")
        try:
            return e.code, json.loads(body)
        except ValueError:
            return e.code, {"detail": body}


def main(argv: Optional[List[str]] = None) -> int:
    ap = argparse.ArgumentParser(description=__doc__.split("\n\n")[0])
    ap.add_argument("--url", default=None, help="server base URL (omit with --out to only write the request)")
    ap.add_argument("--left", help="left_stereo image (PNG / JPEG); default: the shipped sample")
    ap.add_argument("--right", help="right_stereo image (PNG / JPEG)")
    ap.add_argument("--instruction", help="task instruction (default: the sample's)")
    ap.add_argument("--state", help="20 comma-separated floats (raw proprio state; default zeros)")
    ap.add_argument("--seed", type=int, help="noise seed (default: the server's fixed seed 0)")
    ap.add_argument("--prompt-bucket", type=int, help="416 / 448 / 512 (default: the smallest that holds the prompt)")
    ap.add_argument("--out", type=Path, help="write the request JSON here")
    a = ap.parse_args(argv)
    if a.left or a.right:
        if not (a.left and a.right and a.instruction):
            ap.error("--left, --right and --instruction go together")
        state = [float(x) for x in a.state.split(",")] if a.state else None
        req = build_request(a.left, a.right, a.instruction, state, a.seed, a.prompt_bucket)
    else:
        req = sample_request()
        if a.instruction:
            req["instruction"] = a.instruction
        if a.seed is not None:
            req["seed"] = a.seed
        if a.state:
            req["state"] = [float(x) for x in a.state.split(",")]
        if a.prompt_bucket is not None:
            req["prompt_bucket"] = a.prompt_bucket
    if a.out:
        a.out.write_text(json.dumps(req))
    if not a.url:
        if not a.out:
            ap.error("give --url and / or --out")
        return 0
    code, body = post(a.url, req)
    print(json.dumps(body, indent=1))
    return 0 if code == 200 else 1


if __name__ == "__main__":
    sys.exit(main())