-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrest_vlm.py
More file actions
94 lines (76 loc) · 3.16 KB
/
Copy pathrest_vlm.py
File metadata and controls
94 lines (76 loc) · 3.16 KB
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
"""VLM (Visual Language Model) inference via REST API.
Usage:
python examples/rest_vlm.py --image examples/images/coco_cat_remote.jpg --prompt "What do you see?"
python examples/rest_vlm.py --image examples/images/coco_cat_remote.jpg --prompt "List all objects you can identify in this image." --output_mode "detect"
# With extra generation options
python examples/rest_vlm.py --image examples/images/coco_cat_remote.jpg \
--prompt "List every object visible." \
--max-tokens 256 --temperature 0.3
"""
import argparse
import base64
from pathlib import Path
import requests
BASE_URL = "http://127.0.0.1:8110/v1"
DEFAULT_MODEL = "Qwen/Qwen3-VL-2B-Instruct"
def infer_vlm(
model: str,
image_path: Path,
prompt: str,
max_tokens: int | None = None,
temperature: float | None = None,
output_mode: str | None = None,
) -> dict:
"""POST /v1/infer for a VLM task and return the parsed response."""
image_b64 = base64.b64encode(image_path.read_bytes()).decode()
params = {"prompt": prompt}
if max_tokens is not None:
params["max_tokens"] = max_tokens
if temperature is not None:
params["temperature"] = temperature
if output_mode is not None:
params["output_mode"] = output_mode
payload = {
"model": model,
"task": "vlm",
"image": image_b64,
"params": params,
}
resp = requests.post(f"{BASE_URL}/infer", json=payload, timeout=120)
resp.raise_for_status()
return resp.json()
def main(
image_path: Path,
model: str,
prompt: str,
max_tokens: int | None,
temperature: float | None,
output_mode: str,
) -> None:
print("\n--- VLM Inference ---")
print(f" Model : {model}")
print(f" Prompt : {prompt!r}")
print(f" Output Mode : {output_mode}")
try:
result = infer_vlm(model, image_path, prompt, max_tokens, temperature, output_mode)
except requests.HTTPError as exc:
print(f" ERROR {exc.response.status_code}: {exc.response.text}")
return
answer = result.get("text") or "(no text output)"
print(f"\n Response:\n{answer}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="REST VLM inference example")
parser.add_argument("--image", type=Path, required=True, help="Path to an image file")
parser.add_argument("--prompt", default="Describe this image.", help="Question or instruction")
parser.add_argument("--model", default=DEFAULT_MODEL, help="VLM model ref")
parser.add_argument("--max-tokens", type=int, default=None, help="Max tokens to generate")
parser.add_argument("--temperature", type=float, default=None, help="Sampling temperature")
parser.add_argument("--output_mode", default="text", help="Output mode of the VLM")
parser.add_argument("--host", default="127.0.0.1")
parser.add_argument("--port", type=int, default=8110)
args = parser.parse_args()
BASE_URL = f"http://{args.host}:{args.port}/v1"
if not args.image.exists():
print(f"ERROR: image not found: {args.image}")
raise SystemExit(1)
main(args.image, args.model, args.prompt, args.max_tokens, args.temperature, args.output_mode)