Spaces:
Sleeping
Sleeping
Download tools/image_caption.py from vbertret/First_agent_template: direct link, hf CLI and curl.
- Browser
- Download file 830 Bytes
-
https://huggingface.co/spaces/vbertret/First_agent_template/resolve/main/tools/image_caption.py
- Command line
-
hf download hf://spaces/vbertret/First_agent_template/tools/image_caption.py
-
curl -L -o image_caption.py https://huggingface.co/spaces/vbertret/First_agent_template/resolve/main/tools/image_caption.py
830 Bytes
| from smolagents import Tool | |
| from huggingface_hub import InferenceClient | |
| from PIL import Image | |
| import requests | |
| from io import BytesIO | |
| class ImageCaptioningTool(Tool): | |
| description = "Cet outil génère une légende descriptive pour une image donnée." | |
| name = "image_captioner" | |
| inputs = {"image_url": {"type": "string", "description": "URL de l'image à décrire."}} | |
| output_type = "string" | |
| model_id = "Salesforce/blip-image-captioning-large" | |
| client = InferenceClient(model_id) | |
| def forward(self, image_url): | |
| response = requests.get(image_url) | |
| if response.status_code == 200: | |
| image = Image.open(BytesIO(response.content)) | |
| return self.client.image_to_text(image) | |
| else: | |
| return f"Erreur lors du téléchargement de l'image : {response.status_code}" |