"""Benchmark the local strata vision endpoint on captioning.""" import base64 import io import json import tarfile import time import urllib.request import numpy as np from PIL import Image URL = "http://127.0.0.1:8080/v1/chat/completions" MODEL = "qwen3.8-flash-next-gyro-gyro-s" def post(payload, timeout=300): data = json.dumps(payload).encode() req = urllib.request.Request(URL, data=data, headers={"Content-Type": "application/json"}) t0 = time.time() with urllib.request.urlopen(req, timeout=timeout) as r: body = json.loads(r.read().decode()) return body, time.time() - t0 def b64_jpeg(im, side=512): im = im.convert("RGB") w, h = im.size s = side / max(w, h) if s < 1: im = im.resize((int(w * s), int(h * s)), Image.BICUBIC) buf = io.BytesIO() im.save(buf, format="JPEG", quality=88) return base64.b64encode(buf.getvalue()).decode() def main(): blobs = [] with tarfile.open("../data_mm/shards/cc3m-0000.tar") as tf: for m in tf: if m.isfile() and m.name.endswith(".txt"): cap = tf.extractfile(m).read().decode("utf-8", "ignore").strip() if "car" in cap.lower(): key = m.name[:-4] try: jm = tf.getmember(key + ".jpg") except KeyError: continue blobs.append(Image.open(io.BytesIO(tf.extractfile(jm).read()))) if len(blobs) >= 4: break prompt = ("Describe this image in one detailed sentence. " "Include the main subject's colour, what it is doing, and the setting. " "Answer with the sentence only, no preamble.") print(f"{'run':>4} {'wall_s':>7} {'ptok':>5} {'ctok':>5} {'tok/s':>7} caption") for i, im in enumerate(blobs): b64 = b64_jpeg(im) payload = { "model": MODEL, "messages": [{ "role": "user", "content": [ {"type": "text", "text": prompt}, {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{b64}"}}, ], }], "max_tokens": 80, "temperature": 0.2, "chat_template_kwargs": {"enable_thinking": False}, } try: body, dt = post(payload) except Exception as e: print(f"{i:>4} ERROR {e}") continue ch = body["choices"][0]["message"] txt = (ch.get("content") or "").strip().replace("\n", " ") usage = body.get("usage", {}) pt, ct = usage.get("prompt_tokens", 0), usage.get("completion_tokens", 0) tps = ct / dt if dt > 0 else 0 print(f"{i:>4} {dt:7.2f} {pt:5d} {ct:5d} {tps:7.1f} {txt[:110]}") if ch.get("reasoning_content"): print(f" (reasoning: {ch['reasoning_content'][:80]})") if __name__ == "__main__": main()