| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | # ruff: noqa: E501 |
| 4 | """Example Python client for multimodal classification API using vLLM API server |
| 5 | NOTE: |
| 6 | start a supported multimodal classification model server with `vllm serve`, e.g. |
| 7 | vllm serve muziyongshixin/Qwen2.5-VL-7B-for-VideoCls \ |
| 8 | --runner pooling \ |
| 9 | --max-model-len 5000 \ |
| 10 | --limit-mm-per-prompt.video 1 \ |
| 11 | --hf-overrides '{"architectures": ["Qwen2_5_VLForSequenceClassification"]}' |
| 12 | """ |
| 13 | |
| 14 | import argparse |
| 15 | import pprint |
| 16 | |
| 17 | import requests |
| 18 | |
| 19 | from vllm.multimodal.utils import encode_image_url, fetch_image |
| 20 | |
| 21 | input_text = "This product was excellent and exceeded my expectations" |
| 22 | image_url = "https://vllm-public-assets.s3.us-west-2.amazonaws.com/multimodal_asset/cat_snow.jpg" |
| 23 | image_base64 = {"url": encode_image_url(fetch_image(image_url))} |
| 24 | video_url = "https://www.bogotobogo.com/python/OpenCV_Python/images/mean_shift_tracking/slow_traffic_small.mp4" |
| 25 | |
| 26 | |
| 27 | def parse_args(): |
| 28 | parse = argparse.ArgumentParser() |
| 29 | parse.add_argument("--host", type=str, default="localhost") |
| 30 | parse.add_argument("--port", type=int, default=8000) |
| 31 | return parse.parse_args() |
| 32 | |
| 33 | |
| 34 | def main(args): |
| 35 | base_url = f"http://{args.host}:{args.port}" |
| 36 | models_url = base_url + "/v1/models" |
| 37 | classify_url = base_url + "/classify" |
| 38 | |
| 39 | response = requests.get(models_url) |
| 40 | model_name = response.json()["data"][0]["id"] |
| 41 | |
| 42 | print("Text classification output:") |
| 43 | messages = [ |
| 44 | { |
| 45 | "role": "assistant", |
| 46 | "content": "Please classify this text request.", |
| 47 | }, |
| 48 | { |
| 49 | "role": "user", |
| 50 | "content": input_text, |
| 51 | }, |
| 52 | ] |
| 53 | response = requests.post( |
| 54 | classify_url, |
| 55 | json={"model": model_name, "messages": messages}, |
| 56 | ) |
| 57 | pprint.pprint(response.json()) |
| 58 | |
| 59 | print("Image url classification output:") |
| 60 | messages = [ |
| 61 | { |
| 62 | "role": "user", |
| 63 | "content": [ |
| 64 | {"type": "text", "text": "Please classify this image."}, |
| 65 | {"type": "image_url", "image_url": {"url": image_url}}, |
| 66 | ], |
| 67 | } |
| 68 | ] |
| 69 | response = requests.post( |
| 70 | classify_url, |
| 71 | json={"model": model_name, "messages": messages}, |
| 72 | ) |
| 73 | pprint.pprint(response.json()) |
| 74 | |
| 75 | print("Image base64 classification output:") |
| 76 | messages = [ |
| 77 | { |
| 78 | "role": "user", |
| 79 | "content": [ |
| 80 | {"type": "text", "text": "Please classify this image."}, |
| 81 | {"type": "image_url", "image_url": image_base64}, |
| 82 | ], |
| 83 | } |
| 84 | ] |
| 85 | response = requests.post( |
| 86 | classify_url, |
| 87 | json={"model": model_name, "messages": messages}, |
| 88 | ) |
| 89 | pprint.pprint(response.json()) |
| 90 | |
| 91 | print("Video url classification output:") |
| 92 | messages = [ |
| 93 | { |
| 94 | "role": "user", |
| 95 | "content": [ |
| 96 | {"type": "text", "text": "Please classify this video."}, |
| 97 | {"type": "video_url", "video_url": {"url": video_url}}, |
| 98 | ], |
| 99 | } |
| 100 | ] |
| 101 | response = requests.post( |
| 102 | classify_url, |
| 103 | json={"model": model_name, "messages": messages}, |
| 104 | ) |
| 105 | pprint.pprint(response.json()) |
| 106 | |
| 107 | |
| 108 | if __name__ == "__main__": |
| 109 | args = parse_args() |
| 110 | main(args) |