|
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()) |