File size: 1,329 Bytes
d75dae7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from smolagents import Tool
from transformers import BlipProcessor, BlipForConditionalGeneration
from PIL import Image
import torch


class ImageDescriberTool(Tool):
    name = "image_describer"
    description = """
        Analyzes image and provide what is represented on it.
        Supported image extensions: .png, .jpg, .jpeg, .bmp, .svg.
    """
    inputs = {
        "image_path": {
            "type": "string",
            "description": "The path to the image file",
        }
    }
    output_type = "string"

    def __init__(self):

        super().__init__()
        self.device = "cuda" if torch.cuda.is_available() else "cpu"
        model_name = "Salesforce/blip-image-captioning-large"
        self.processor = BlipProcessor.from_pretrained(model_name)
        self.model = BlipForConditionalGeneration.from_pretrained(model_name).to(self.device)

    def forward(self, image_path: str) -> str:
        try:
            image = Image.open(image_path).convert('RGB')
            inputs = self.processor(image, return_tensors="pt").to(self.device)
            out = self.model.generate(**inputs)
            img_description = self.processor.decode(out[0], skip_special_tokens=True)
            return img_description
        except Exception as e:
            return f"Error generating image description: {e}"