mrpe24's picture
implemented agent with tools
d75dae7
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}"