Views
No views yet
pip install "git+https://github.com/huggingface/optimum-intel.git" --extra-index-url https://download.pytorch.org/whl/cpu
pip install transformers==5.5.0
pip install torchvision Pillow --extra-index-url https://download.pytorch.org/whl/cpu1from optimum.intel.openvino import OVModelForVisualCausalLM
2from transformers import AutoProcessor
3from PIL import Image
4import requests
5
6model_id = "OpenVINO/gemma-4-E2B-it-fp16-ov"
7processor = AutoProcessor.from_pretrained(model_id)
8model = OVModelForVisualCausalLM.from_pretrained(model_id)
9
10url = "https://github.com/openvinotoolkit/openvino_notebooks/assets/29454499/d5fbbd1a-d484-415c-88cb-9986625b7b11"
11image = Image.open(requests.get(url, stream=True).raw)
12
13messages = [
14 {
15 "role": "user",
16 "content": [
17 {"type": "image", "image": image},
18 {"type": "text", "text": "What is unusual in this picture?"},
19 ],
20 }
21]
22
23text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
24inputs = processor(text=text, images=[image], return_tensors="pt")
25input_len = inputs["input_ids"].shape[-1]
26
27output = model.generate(**inputs, do_sample=False, max_new_tokens=100)
28response = processor.decode(output[0][input_len:], skip_special_tokens=True)
29print(response)