Image-Text-to-Text
Transformers
Safetensors
multilingual
phi3_v
text-generation
nlp
code
vision
conversational
custom_code
Instructions to use microsoft/Phi-3.5-vision-instruct with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use microsoft/Phi-3.5-vision-instruct with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="microsoft/Phi-3.5-vision-instruct", trust_remote_code=True) 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 AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("microsoft/Phi-3.5-vision-instruct", trust_remote_code=True, dtype="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps
- vLLM
How to use microsoft/Phi-3.5-vision-instruct with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "microsoft/Phi-3.5-vision-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": "microsoft/Phi-3.5-vision-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/microsoft/Phi-3.5-vision-instruct
- SGLang
How to use microsoft/Phi-3.5-vision-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 "microsoft/Phi-3.5-vision-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": "microsoft/Phi-3.5-vision-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 "microsoft/Phi-3.5-vision-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": "microsoft/Phi-3.5-vision-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 microsoft/Phi-3.5-vision-instruct with Docker Model Runner:
docker model run hf.co/microsoft/Phi-3.5-vision-instruct
Update processing_phi3_v.py
Browse filesThis model was not able to process list of numpy arrays (and probably with pytorch tensors)
you can try yourself in the example code on this model card. Just use 'np.array(Image.open(requests.get(url, stream=True).raw)) '

I solved this issue implementing a function to convert any type of valid image to PIL Image object.
- processing_phi3_v.py +22 -4
processing_phi3_v.py
CHANGED
|
@@ -56,7 +56,7 @@ logger = logging.get_logger(__name__)
|
|
| 56 |
if is_vision_available():
|
| 57 |
from PIL import Image
|
| 58 |
|
| 59 |
-
import torch
|
| 60 |
import torchvision
|
| 61 |
|
| 62 |
def padding_336(b):
|
|
@@ -205,6 +205,22 @@ class Phi3VImageProcessor(BaseImageProcessor):
|
|
| 205 |
num_img_tokens = int((new_height // 336 * new_width // 336 + 1) * 144 + 1 + (new_height // 336 + 1) * 12)
|
| 206 |
return num_img_tokens
|
| 207 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
def preprocess(
|
| 209 |
self,
|
| 210 |
images: ImageInput,
|
|
@@ -245,8 +261,8 @@ class Phi3VImageProcessor(BaseImageProcessor):
|
|
| 245 |
"torch.Tensor, tf.Tensor or jax.ndarray."
|
| 246 |
)
|
| 247 |
|
| 248 |
-
if do_convert_rgb:
|
| 249 |
-
images = [convert_to_rgb(image) for image in images]
|
| 250 |
|
| 251 |
image_sizes = []
|
| 252 |
img_processor = torchvision.transforms.Compose([
|
|
@@ -256,7 +272,9 @@ class Phi3VImageProcessor(BaseImageProcessor):
|
|
| 256 |
|
| 257 |
# PIL images
|
| 258 |
# HD_transform pad images to size of multiiply of 336, 336
|
| 259 |
-
# convert
|
|
|
|
|
|
|
| 260 |
images = [image.convert('RGB') for image in images]
|
| 261 |
elems = [HD_transform(im, hd_num = self.num_crops) for im in images]
|
| 262 |
# tensor transform and normalize
|
|
|
|
| 56 |
if is_vision_available():
|
| 57 |
from PIL import Image
|
| 58 |
|
| 59 |
+
import torch # why is this imported twice?
|
| 60 |
import torchvision
|
| 61 |
|
| 62 |
def padding_336(b):
|
|
|
|
| 205 |
num_img_tokens = int((new_height // 336 * new_width // 336 + 1) * 144 + 1 + (new_height // 336 + 1) * 12)
|
| 206 |
return num_img_tokens
|
| 207 |
|
| 208 |
+
def convert_PIL(self, image):
|
| 209 |
+
"""
|
| 210 |
+
Convert an image to a PIL Image object if it is not already one.
|
| 211 |
+
|
| 212 |
+
Args:
|
| 213 |
+
image (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`): The image to be converted. Can be a numpy array or a torch tensor or PIL object.
|
| 214 |
+
|
| 215 |
+
Returns:
|
| 216 |
+
A PIL Image object.
|
| 217 |
+
"""
|
| 218 |
+
if not isinstance(image, Image.Image):
|
| 219 |
+
return torchvision.transforms.functional.to_pil_image(image)
|
| 220 |
+
else:
|
| 221 |
+
return image
|
| 222 |
+
|
| 223 |
+
|
| 224 |
def preprocess(
|
| 225 |
self,
|
| 226 |
images: ImageInput,
|
|
|
|
| 261 |
"torch.Tensor, tf.Tensor or jax.ndarray."
|
| 262 |
)
|
| 263 |
|
| 264 |
+
# if do_convert_rgb:
|
| 265 |
+
# images = [convert_to_rgb(image) for image in images]
|
| 266 |
|
| 267 |
image_sizes = []
|
| 268 |
img_processor = torchvision.transforms.Compose([
|
|
|
|
| 272 |
|
| 273 |
# PIL images
|
| 274 |
# HD_transform pad images to size of multiiply of 336, 336
|
| 275 |
+
# check and convert if the images are in PIL format
|
| 276 |
+
images = [convert_PIL(image) for image in images]
|
| 277 |
+
# convert to RGB first (I think the argument "do_convert_rgb is useless, since it is forced here")
|
| 278 |
images = [image.convert('RGB') for image in images]
|
| 279 |
elems = [HD_transform(im, hd_num = self.num_crops) for im in images]
|
| 280 |
# tensor transform and normalize
|