Skip to content

Instantly share code, notes, and snippets.

@jasonforte
Created August 10, 2025 12:45
Show Gist options
  • Select an option

  • Save jasonforte/fdcf6d88e77364b9a985adb1ef0e6e3a to your computer and use it in GitHub Desktop.

Select an option

Save jasonforte/fdcf6d88e77364b9a985adb1ef0e6e3a to your computer and use it in GitHub Desktop.
Python Sample - Invoke Qwen 2.5 VL Instruct on Amazon Bedrock

Invoke Qwen2.5 VL Instruct via Amazon Bedrock

This is part of a YouTube tutorial series by MakeOps.

The file is a script that invokes Qwen2.5 VL via Amazon Bedrock to describe and image.

usage: main.py [-h] image

positional arguments:
  image

options:
  -h, --help  show this help message and exit

Dependencies

"boto3>=1.40.6",
"dotenv>=0.9.9",
"pillow>=11.3.0",
"torchvision>=0.23.0",
"transformers>=4.55.0",
import json
import os
import base64
import boto3
from io import BytesIO
from argparse import ArgumentParser
from dotenv import load_dotenv
from PIL import Image
from transformers import AutoProcessor
load_dotenv()
bedrock = boto3.client('bedrock-runtime', region_name='us-east-1')
QWEN_MODEL_ID = os.environ.get('QWEN_MODEL_ID')
def image_to_base64(img: Image, img_format='JPEG'):
'''Convert PIL image to Base64'''
buffer = BytesIO()
img.save(buffer, format=img_format)
img_bytes = buffer.getvalue()
return base64.b64encode(img_bytes).decode()
def invoke_qwen(prompt: str, image_path: str, temp=0.0, max_tokens=2048, top_p=0.9):
processor = AutoProcessor.from_pretrained('Qwen/Qwen2.5-VL-7B-Instruct', use_fast=True)
messages = [
{
"role": "user",
"content": [
{ "type": "image", "image": f"file://{image_path}" },
{ "type": "text", "text": prompt },
],
}
]
prompt = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
response = bedrock.invoke_model(
modelId=QWEN_MODEL_ID,
body=json.dumps({
'prompt': prompt,
'temperature': temp,
'max_gen_len': max_tokens,
'top_p': top_p,
'images': [image_to_base64(Image.open(image_path))]
}),
accept='application/json',
contentType='application/json'
)
response_body = json.load(response['body'])
return response_body['choices']
def main():
parser = ArgumentParser()
parser.add_argument('image')
args = parser.parse_args()
print(f'Using Qwen to Process Image modelId={QWEN_MODEL_ID}')
full_path = os.path.join(os.path.dirname(__file__), args.image)
print('full path', full_path)
response = invoke_qwen('Explain what you see in this image', full_path)
for response_item in response:
print('qwen >', response_item['text'])
if __name__ == "__main__":
exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment