File size: 2,816 Bytes
9deea94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from typing import Dict, Any
from transformers import AutoProcessor, Qwen2VLForConditionalGeneration
import torch
from PIL import Image
import requests
from io import BytesIO
import json

# Check for GPU
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")


class EndpointHandler:
    def __init__(self, path: str = ""):
        """
        Initializes the handler for the Qwen2-VL model.
        
        Args:
            path (str): Path to the model weights and processor. Defaults to the current directory.
        """
        # Load the processor and model
        self.processor = AutoProcessor.from_pretrained(path)
        self.model = Qwen2VLForConditionalGeneration.from_pretrained(
            path,
            torch_dtype="auto",
            device_map="auto"
        )
        # Move the model to the appropriate device
        self.model.to(device)

    def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
        """
        Processes the input data and returns the model's prediction.
        
        Args:
            data (Dict[str, Any]): Input data containing `image_url` and `text`.
        
        Returns:
            Dict[str, Any]: The prediction or an error message.
        """
        image_url = data.get("image_url", "")
        text = data.get("text", "")

        # Load the image from the URL
        try:
            response = requests.get(image_url)
            response.raise_for_status()
            image = Image.open(BytesIO(response.content))
        except Exception as e:
            return {"error": f"Failed to fetch or process image: {str(e)}"}

        # Prepare the text prompt
        text_prompt = self.processor.apply_chat_template(
            [{"role": "user", "content": [{"type": "text", "text": text}]}],
            add_generation_prompt=True
        )

        # Preprocess the input
        inputs = self.processor(
            text=[text_prompt],
            images=[image],
            padding=True,
            return_tensors="pt"
        )

        # Move inputs to the correct device
        inputs = {key: value.to(device) for key, value in inputs.items()}

        # Perform inference
        output_ids = self.model.generate(**inputs, max_new_tokens=128)

        # Decode the generated text
        output_text = self.processor.batch_decode(
            output_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True
        )[0]

        # Clean and parse the JSON response
        cleaned_data = output_text.replace("```json\n", "").replace("```", "").strip()
        try:
            prediction = json.loads(cleaned_data)
        except json.JSONDecodeError as e:
            return {"error": f"Failed to parse JSON output: {str(e)}", "raw_output": cleaned_data}

        return {"prediction": prediction}