phi3v_example.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import os
  2. import subprocess
  3. from PIL import Image
  4. from aphrodite import LLM, SamplingParams
  5. from aphrodite.multimodal.image import ImagePixelData
  6. def run_phi3v():
  7. model_path = "microsoft/Phi-3-vision-128k-instruct"
  8. llm = LLM(
  9. model=model_path,
  10. trust_remote_code=True,
  11. max_model_len=4096,
  12. image_input_type="pixel_values",
  13. image_token_id=32044,
  14. image_input_shape="1,3,1008,1344",
  15. image_feature_size=1921,
  16. disable_image_processor=False,
  17. )
  18. image = Image.open("images/cherry_blossom.jpg")
  19. # single-image prompt
  20. prompt = "<|user|>\n<|image_1|>\nWhat is the season?<|end|>\n<|assistant|>\n" # noqa: E501
  21. prompt = prompt.replace("<|image_1|>", "<|image|>" * 1921 + "<s>")
  22. sampling_params = SamplingParams(temperature=0, max_tokens=64)
  23. outputs = llm.generate({
  24. "prompt": prompt,
  25. "sampling_params": sampling_params,
  26. "multi_modal_data": ImagePixelData(image),
  27. })
  28. for o in outputs:
  29. generated_text = o.outputs[0].text
  30. print(generated_text)
  31. if __name__ == "__main__":
  32. s3_bucket_path = "s3://air-example-data-2/vllm_opensource_llava/"
  33. local_directory = "images"
  34. # Make sure the local directory exists or create it
  35. os.makedirs(local_directory, exist_ok=True)
  36. # Use AWS CLI to sync the directory, assume anonymous access
  37. subprocess.check_call([
  38. "aws",
  39. "s3",
  40. "sync",
  41. s3_bucket_path,
  42. local_directory,
  43. "--no-sign-request",
  44. ])
  45. run_phi3v()