Image-Text-to-Text
Transformers
Safetensors
English
qwen2_vl
multimodal
vision
conversational
text-generation-inference
Instructions to use Gabriel/Qwen2-VL-2B-Instruct with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Gabriel/Qwen2-VL-2B-Instruct with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="Gabriel/Qwen2-VL-2B-Instruct") messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] pipe(text=messages)# Load model directly from transformers import AutoProcessor, AutoModelForMultimodalLM processor = AutoProcessor.from_pretrained("Gabriel/Qwen2-VL-2B-Instruct") model = AutoModelForMultimodalLM.from_pretrained("Gabriel/Qwen2-VL-2B-Instruct", device_map="auto") messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] inputs = processor.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=40) print(processor.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use Gabriel/Qwen2-VL-2B-Instruct with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "Gabriel/Qwen2-VL-2B-Instruct" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Gabriel/Qwen2-VL-2B-Instruct", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker
docker model run hf.co/Gabriel/Qwen2-VL-2B-Instruct
- SGLang
How to use Gabriel/Qwen2-VL-2B-Instruct with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "Gabriel/Qwen2-VL-2B-Instruct" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Gabriel/Qwen2-VL-2B-Instruct", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "Gabriel/Qwen2-VL-2B-Instruct" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Gabriel/Qwen2-VL-2B-Instruct", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }' - Docker Model Runner
How to use Gabriel/Qwen2-VL-2B-Instruct with Docker Model Runner:
docker model run hf.co/Gabriel/Qwen2-VL-2B-Instruct
Create handler.py
Browse files- handler.py +52 -0
handler.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Dict, Any
|
| 2 |
+
from transformers import AutoProcessor, Qwen2VLForConditionalGeneration
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import io
|
| 5 |
+
import base64
|
| 6 |
+
import requests
|
| 7 |
+
|
| 8 |
+
class EndpointHandler():
|
| 9 |
+
def __init__(self, path=""):
|
| 10 |
+
self.processor = AutoProcessor.from_pretrained(path)
|
| 11 |
+
self.model = Qwen2VLForConditionalGeneration.from_pretrained(path)
|
| 12 |
+
|
| 13 |
+
def __call__(self, data: Any) -> Dict[str, Any]:
|
| 14 |
+
|
| 15 |
+
image_input = data.get('image', None)
|
| 16 |
+
text_input = data.get('text', None)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
if isinstance(data, dict):
|
| 20 |
+
if image_input.startswith('http'):
|
| 21 |
+
image = Image.open(requests.get(image_input, stream=True).raw).convert('RGB')
|
| 22 |
+
else:
|
| 23 |
+
image_data = base64.b64decode(image_input)
|
| 24 |
+
image = Image.open(io.BytesIO(image_data)).convert('RGB')
|
| 25 |
+
else:
|
| 26 |
+
return {"error": "Invalid input data. Expected binary image data or a dictionary with 'image' key."}
|
| 27 |
+
|
| 28 |
+
messages = [
|
| 29 |
+
{
|
| 30 |
+
"role": "user",
|
| 31 |
+
"content": [
|
| 32 |
+
{"type": "image", "image": image},
|
| 33 |
+
{"type": "text", "text": text_input},
|
| 34 |
+
],
|
| 35 |
+
}
|
| 36 |
+
]
|
| 37 |
+
|
| 38 |
+
text = self.processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 39 |
+
inputs = self.processor(
|
| 40 |
+
text=[text],
|
| 41 |
+
images=[image],
|
| 42 |
+
padding=True,
|
| 43 |
+
return_tensors="pt",
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
generate_ids = self.model.generate(inputs.input_ids, max_length=30)
|
| 47 |
+
output_text = self.processor.batch_decode(
|
| 48 |
+
generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
| 49 |
+
)[0]
|
| 50 |
+
|
| 51 |
+
return {"generated_text": output_text}
|
| 52 |
+
|