Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

phi3 vision #977

Merged
merged 9 commits into from
Nov 14, 2024
Merged

phi3 vision #977

merged 9 commits into from
Nov 14, 2024

Conversation

eaidova
Copy link
Collaborator

@eaidova eaidova commented Oct 29, 2024

What does this PR do?

from PIL import Image 
import requests 
from optimum.intel.openvino import OVModelForVisualCausalLM
from transformers import AutoProcessor, TextStreamer

model_id = "microsoft/Phi-3.5-vision-instruct"

model = OVModelForVisualCausalLM.from_pretrained(
  model_id, 
  trust_remote_code=True,
)

processor = AutoProcessor.from_pretrained(model_id, 
  trust_remote_code=True, 
) 

messages = [
    {"role": "user", "content": "<|image_1|>\nWhat is unusual on this picture?"},
]
url = "https://github.com/openvinotoolkit/openvino_notebooks/assets/29454499/d5fbbd1a-d484-415c-88cb-9986625b7b11"
image = Image.open(requests.get(url, stream=True).raw)

prompt = processor.tokenizer.apply_chat_template(
  messages, 
  tokenize=False, 
  add_generation_prompt=True
)

inputs = processor(prompt, [image], return_tensors="pt")

generation_args = { 
    "max_new_tokens": 50, 
    "temperature": 0.0, 
    "do_sample": False,
    "streamer": TextStreamer(processor.tokenizer, skip_prompt=True, skip_special_tokens=True)
} 

generate_ids = model.generate(**inputs, 
  eos_token_id=processor.tokenizer.eos_token_id, 
  **generation_args
)

# remove input tokens 
generate_ids = generate_ids[:, inputs['input_ids'].shape[1]:]
response = processor.batch_decode(generate_ids, 
  skip_special_tokens=True, 
  clean_up_tokenization_spaces=False)[0]

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you make sure to update the documentation with your changes?
  • Did you write any new necessary tests?

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@eaidova eaidova marked this pull request as ready for review November 4, 2024 17:09
@eaidova eaidova force-pushed the ea/phi3v branch 5 times, most recently from 4daa695 to 31eadc5 Compare November 5, 2024 14:04
Copy link
Collaborator

@nikita-savelyevv nikita-savelyevv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

optimum/exporters/openvino/convert.py Show resolved Hide resolved
optimum/exporters/openvino/model_configs.py Show resolved Hide resolved
optimum/exporters/openvino/model_configs.py Outdated Show resolved Hide resolved
optimum/intel/openvino/modeling_visual_language.py Outdated Show resolved Hide resolved
eaidova and others added 2 commits November 14, 2024 15:42
Co-authored-by: Nikita Savelyev <nikita.savelyev@intel.com>
@AlexKoff88 AlexKoff88 merged commit febc50e into huggingface:main Nov 14, 2024
20 of 24 checks passed
@eaidova eaidova deleted the ea/phi3v branch November 14, 2024 14:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants