Spaces:
Sleeping
Sleeping
Update tools/image_caption.py
Browse files- tools/image_caption.py +8 -2
tools/image_caption.py
CHANGED
|
@@ -1,6 +1,8 @@
|
|
| 1 |
from smolagents import Tool
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
| 3 |
import requests
|
|
|
|
| 4 |
|
| 5 |
class ImageCaptioningTool(Tool):
|
| 6 |
description = "Cet outil génère une légende descriptive pour une image donnée."
|
|
@@ -11,5 +13,9 @@ class ImageCaptioningTool(Tool):
|
|
| 11 |
client = InferenceClient(model_id)
|
| 12 |
|
| 13 |
def forward(self, image_url):
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from smolagents import Tool
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
+
from PIL import Image
|
| 4 |
import requests
|
| 5 |
+
from io import BytesIO
|
| 6 |
|
| 7 |
class ImageCaptioningTool(Tool):
|
| 8 |
description = "Cet outil génère une légende descriptive pour une image donnée."
|
|
|
|
| 13 |
client = InferenceClient(model_id)
|
| 14 |
|
| 15 |
def forward(self, image_url):
|
| 16 |
+
response = requests.get(image_url)
|
| 17 |
+
if response.status_code == 200:
|
| 18 |
+
image = Image.open(BytesIO(response.content))
|
| 19 |
+
return self.client.image_to_text(image)
|
| 20 |
+
else:
|
| 21 |
+
return f"Erreur lors du téléchargement de l'image : {response.status_code}"
|