import os import requests import torch from PIL import Image import soundfile from transformers import AutoModelForCausalLM, AutoProcessor, GenerationConfig model_path = '/home/wanglch/Phi4/Phi-4-multimodal-instruct' kwargs = {} kwargs['torch_dtype'] = torch.bfloat16 processor = AutoProcessor.from_pretrained(model_path, trust_remote_code=True) model = AutoModelForCausalLM.from_pretrained( model_path, trust_remote_code=True, torch_dtype='auto', _attn_implementation='flash_attention_2', ).cuda() generation_config = GenerationConfig.from_pretrained(model_path, 'generation_config.json') user_prompt = '<|user|>' assistant_prompt = '<|assistant|>' prompt_suffix = '<|end|>' #################################################### vision (single-turn) #################################################### # single-image prompt prompt = f'{user_prompt}<|image_1|>What is shown in this image?{prompt_suffix}{assistant_prompt}' url = 'https://www.ilankelman.org/stopsigns/australia.jpg' print(f'>>> Prompt\n{prompt}') image = Image.open(requests.get(url, stream=True).raw) inputs = processor(text=prompt, images=image, return_tensors='pt').to('cuda:0') generate_ids = model.generate( **inputs, max_new_tokens=1000, generation_config=generation_config, ) 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] print(f'>>> Response\n{response}') #################################################### vision (multi-turn) #################################################### # chat template chat = [ {'role': 'user', 'content': f'<|image_1|>What is shown in this image?'}, { 'role': 'assistant', 'content': "The image depicts a street scene with a prominent red stop sign in the foreground. The background showcases a building with traditional Chinese architecture, characterized by its red roof and ornate decorations. There are also several statues of lions, which are common in Chinese culture, positioned in front of the building. The street is lined with various shops and businesses, and there's a car passing by.", }, {'role': 'user', 'content': 'What is so special about this image'}, ] url = 'https://www.ilankelman.org/stopsigns/australia.jpg' image = Image.open(requests.get(url, stream=True).raw) prompt = processor.tokenizer.apply_chat_template(chat, tokenize=False, add_generation_prompt=True) # need to remove last <|endoftext|> if it is there, which is used for training, not inference. For training, make sure to add <|endoftext|> in the end. if prompt.endswith('<|endoftext|>'): prompt = prompt.rstrip('<|endoftext|>') print(f'>>> Prompt\n{prompt}') inputs = processor(prompt, [image], return_tensors='pt').to('cuda:0') generate_ids = model.generate( **inputs, max_new_tokens=1000, generation_config=generation_config, ) 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] print(f'>>> Response\n{response}')