songtianhui
commited on
Commit
·
01d6309
1
Parent(s):
46f0dc4
update example code
Browse files
README.md
CHANGED
|
@@ -80,36 +80,72 @@ Without introducing any complex architectures or special patterns, we show how e
|
|
| 80 |
|
| 81 |
# Model Usage
|
| 82 |
|
| 83 |
-
## Inference
|
| 84 |
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
```python
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
)
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
image_path = "./figures/octopus.png"
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
generated_ids_trimmed = [
|
| 107 |
-
out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
| 108 |
-
]
|
| 109 |
-
response = processor.batch_decode(
|
| 110 |
-
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
| 111 |
-
)[0]
|
| 112 |
-
print(response)
|
| 113 |
```
|
| 114 |
|
| 115 |
|
|
|
|
| 80 |
|
| 81 |
# Model Usage
|
| 82 |
|
| 83 |
+
## Inference
|
| 84 |
|
| 85 |
+
We recommend using vLLM for production deployment. Requires `vllm>=0.12.0` with `--trust-remote-code`.
|
| 86 |
+
|
| 87 |
+
First, start the vLLM server:
|
| 88 |
+
|
| 89 |
+
```
|
| 90 |
+
vllm serve sthui/SimpleSeg-Qwen2.5-VL \
|
| 91 |
+
--trust-remote-code \
|
| 92 |
+
--tensor-parallel-size 4 \
|
| 93 |
+
--served-model-name SimpleSeg-Qwen2.5-VL \
|
| 94 |
+
--host 0.0.0.0 \
|
| 95 |
+
--port 8000
|
| 96 |
+
```
|
| 97 |
+
|
| 98 |
+
Then run the following code to inference:
|
| 99 |
|
| 100 |
```python
|
| 101 |
+
|
| 102 |
+
import base64
|
| 103 |
+
from openai import OpenAI
|
| 104 |
+
|
| 105 |
+
# vLLM server configuration
|
| 106 |
+
VLLM_BASE_URL = "http://localhost:8000/v1"
|
| 107 |
+
MODEL_NAME = "SimpleSeg-Qwen2.5-VL" # Should match --served-model-name in vllm serve
|
| 108 |
+
|
| 109 |
+
def encode_image(image_path: str) -> str:
|
| 110 |
+
"""Encode image to base64 string."""
|
| 111 |
+
with open(image_path, "rb") as f:
|
| 112 |
+
return base64.b64encode(f.read()).decode()
|
| 113 |
+
|
| 114 |
+
def inference(image_path: str, instruction: str) -> str:
|
| 115 |
+
"""Run GUI grounding inference via vLLM."""
|
| 116 |
+
client = OpenAI(base_url=VLLM_BASE_URL, api_key="EMPTY")
|
| 117 |
+
|
| 118 |
+
messages = [
|
| 119 |
+
{
|
| 120 |
+
"role": "user",
|
| 121 |
+
"content": [
|
| 122 |
+
{
|
| 123 |
+
"type": "image_url",
|
| 124 |
+
"image_url": {"url": f"data:image/png;base64,{encode_image(image_path)}"}
|
| 125 |
+
},
|
| 126 |
+
{"type": "text", "text": instruction},
|
| 127 |
+
],
|
| 128 |
+
},
|
| 129 |
+
]
|
| 130 |
+
|
| 131 |
+
response = client.chat.completions.create(
|
| 132 |
+
model=MODEL_NAME,
|
| 133 |
+
messages=messages,
|
| 134 |
+
max_tokens=4096,
|
| 135 |
+
temperature=0,
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
return response.choices[0].message.content
|
| 139 |
+
|
| 140 |
+
# Example usage
|
| 141 |
image_path = "./figures/octopus.png"
|
| 142 |
+
instruction = "Output the polygon coordinates of octopus in the image."
|
| 143 |
+
|
| 144 |
+
response = inference(image_path, instruction)
|
| 145 |
+
print("Model output:", response)
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
```
|
| 150 |
|
| 151 |
|