Download code/tt_rdt2/server/client.py from changh95/rdt2-base-p150: direct link, hf CLI and curl.
- Browser
- Download file 4.93 kB
-
https://huggingface.co/changh95/rdt2-base-p150/resolve/main/code/tt_rdt2/server/client.py
- Command line
-
hf download hf://changh95/rdt2-base-p150/code/tt_rdt2/server/client.py
-
curl -L -o client.py https://huggingface.co/changh95/rdt2-base-p150/resolve/main/code/tt_rdt2/server/client.py
4.93 kB
| #!/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()) | |