|
| 1 | +""" |
| 2 | +Usage: |
| 3 | +# Run a Pixtral model with SGLang: |
| 4 | +# HuggingFace: |
| 5 | +python -m sglang.launch_server --model-path mistral-community/pixtral-12b --port=30000 |
| 6 | +# ModelScope: |
| 7 | +python -m sglang.launch_server --model-path AI-ModelScope/pixtral-12b --port=30000 |
| 8 | +
|
| 9 | +# Then test it with: |
| 10 | +python pixtral_server.py |
| 11 | +
|
| 12 | +This script tests Pixtral model with both single and multiple images. |
| 13 | +""" |
| 14 | + |
| 15 | +import argparse |
| 16 | +import asyncio |
| 17 | +import json |
| 18 | + |
| 19 | +import aiohttp |
| 20 | +import requests |
| 21 | + |
| 22 | +IMAGE_TOKEN_SEP = "\n[IMG]" |
| 23 | +ROUTE = "/generate" |
| 24 | + |
| 25 | + |
| 26 | +async def send_request(url, data, delay=0): |
| 27 | + await asyncio.sleep(delay) |
| 28 | + async with aiohttp.ClientSession() as session: |
| 29 | + async with session.post(url, json=data) as resp: |
| 30 | + output = await resp.json() |
| 31 | + return output |
| 32 | + |
| 33 | + |
| 34 | +async def test_concurrent(args): |
| 35 | + url = f"{args.host}:{args.port}{ROUTE}" |
| 36 | + |
| 37 | + # Single image test |
| 38 | + if args.single_image: |
| 39 | + prompt = f"<s>[INST]Describe this image in detail.{IMAGE_TOKEN_SEP}[/INST]" |
| 40 | + image_url = "https://picsum.photos/id/237/400/300" |
| 41 | + modality = ["image"] |
| 42 | + # Multiple images test |
| 43 | + else: |
| 44 | + image_urls = [ |
| 45 | + "https://picsum.photos/id/237/400/300", |
| 46 | + "https://picsum.photos/id/27/500/500", |
| 47 | + ] |
| 48 | + prompt = f"<s>[INST]How many photos are there? Describe each in a very short sentence.{IMAGE_TOKEN_SEP * len(image_urls)}[/INST]" |
| 49 | + image_url = image_urls |
| 50 | + modality = ["multi-images"] |
| 51 | + |
| 52 | + response = await send_request( |
| 53 | + url, |
| 54 | + { |
| 55 | + "text": prompt, |
| 56 | + "image_data": image_url, |
| 57 | + "sampling_params": { |
| 58 | + "max_new_tokens": 100, |
| 59 | + "temperature": 0.7, |
| 60 | + "top_p": 0.9, |
| 61 | + }, |
| 62 | + "modalities": modality, |
| 63 | + }, |
| 64 | + ) |
| 65 | + |
| 66 | + print(f"Response: {response}") |
| 67 | + if "text" in response: |
| 68 | + print("\nOutput text:", response["text"]) |
| 69 | + |
| 70 | + |
| 71 | +def test_streaming(args): |
| 72 | + url = f"{args.host}:{args.port}/generate" |
| 73 | + |
| 74 | + # Single image test |
| 75 | + if args.single_image: |
| 76 | + prompt = f"<s>[INST]Describe this image in detail.{IMAGE_TOKEN_SEP}[/INST]" |
| 77 | + image_data = "https://picsum.photos/id/237/400/300" |
| 78 | + modality = ["image"] |
| 79 | + # Multiple images test |
| 80 | + else: |
| 81 | + image_urls = [ |
| 82 | + "https://picsum.photos/id/237/400/300", |
| 83 | + "https://picsum.photos/id/27/500/500", |
| 84 | + ] |
| 85 | + prompt = f"<s>[INST]How many photos are there? Describe each in a very short sentence.{IMAGE_TOKEN_SEP * len(image_urls)}[/INST]" |
| 86 | + image_data = image_urls |
| 87 | + modality = ["multi-images"] |
| 88 | + |
| 89 | + pload = { |
| 90 | + "text": prompt, |
| 91 | + "image_data": image_data, |
| 92 | + "sampling_params": {"max_new_tokens": 100, "temperature": 0.7, "top_p": 0.9}, |
| 93 | + "modalities": modality, |
| 94 | + "stream": True, |
| 95 | + } |
| 96 | + |
| 97 | + response = requests.post(url, json=pload, stream=True) |
| 98 | + |
| 99 | + print("Streaming response:") |
| 100 | + prev = 0 |
| 101 | + for chunk in response.iter_lines(decode_unicode=False): |
| 102 | + chunk = chunk.decode("utf-8") |
| 103 | + if chunk and chunk.startswith("data:"): |
| 104 | + if chunk == "data: [DONE]": |
| 105 | + break |
| 106 | + data = json.loads(chunk[5:].strip("\n")) |
| 107 | + output = data["text"].strip() |
| 108 | + print(output[prev:], end="", flush=True) |
| 109 | + prev = len(output) |
| 110 | + print("\n") |
| 111 | + |
| 112 | + |
| 113 | +if __name__ == "__main__": |
| 114 | + parser = argparse.ArgumentParser() |
| 115 | + parser.add_argument("--host", type=str, default="http://127.0.0.1") |
| 116 | + parser.add_argument("--port", type=int, default=30000) |
| 117 | + parser.add_argument( |
| 118 | + "--single-image", |
| 119 | + action="store_true", |
| 120 | + help="Test with single image instead of multiple images", |
| 121 | + ) |
| 122 | + parser.add_argument("--no-stream", action="store_true", help="Don't test streaming") |
| 123 | + args = parser.parse_args() |
| 124 | + |
| 125 | + asyncio.run(test_concurrent(args)) |
| 126 | + if not args.no_stream: |
| 127 | + test_streaming(args) |
0 commit comments