|
from typing import Dict, List, Any |
|
import json |
|
import torch |
|
from PIL import Image |
|
from transformers import Qwen2VLForConditionalGeneration, AutoProcessor |
|
|
|
class EndpointHandler: |
|
def __init__(self, model_name: str ="morthens/qwen2-vl-inference"): |
|
|
|
self.model = Qwen2VLForConditionalGeneration.from_pretrained( |
|
model_name, |
|
torch_dtype="auto", |
|
device_map="auto" |
|
) |
|
self.processor = AutoProcessor.from_pretrained(model_name) |
|
|
|
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]: |
|
|
|
image_path = data.get("image_path", "") |
|
messages = data.get("messages", []) |
|
|
|
|
|
try: |
|
image = Image.open(image_path) |
|
except FileNotFoundError: |
|
return [{"error": "Image file not found."}] |
|
except Exception as e: |
|
return [{"error": str(e)}] |
|
|
|
|
|
text_prompt = self.create_text_prompt(messages) |
|
|
|
|
|
inputs = self.processor( |
|
text=[text_prompt], |
|
images=[image], |
|
padding=True, |
|
return_tensors="pt" |
|
) |
|
|
|
|
|
inputs = inputs.to("cuda" if torch.cuda.is_available() else "cpu") |
|
|
|
|
|
output_ids = self.model.generate(**inputs, max_new_tokens=128) |
|
|
|
|
|
generated_ids = [ |
|
output_ids[len(input_ids):] |
|
for input_ids, output_ids in zip(inputs.input_ids, output_ids) |
|
] |
|
|
|
output_text = self.processor.batch_decode( |
|
generated_ids, |
|
skip_special_tokens=True, |
|
clean_up_tokenization_spaces=True |
|
) |
|
|
|
|
|
cleaned_data = self.clean_output(output_text[0]) |
|
|
|
try: |
|
json_data = json.loads(cleaned_data) |
|
except json.JSONDecodeError: |
|
return [{"error": "Failed to parse JSON output."}] |
|
|
|
return [json_data] |
|
|
|
def create_text_prompt(self, messages: List[Dict[str, Any]]) -> str: |
|
"""Extracts and formats text content from messages.""" |
|
text_content = "" |
|
for message in messages: |
|
for content in message.get('content', []): |
|
if content['type'] == 'text': |
|
text_content += content['text'] |
|
|
|
return self.processor.apply_chat_template(messages, add_generation_prompt=True) |
|
|
|
def clean_output(self, output: str) -> str: |
|
"""Cleans up the model's output for JSON parsing.""" |
|
return output.replace("```json\n", "").replace("```", "").strip() |