Delete image_analyzer.py
Browse files- image_analyzer.py +0 -77
image_analyzer.py
DELETED
|
@@ -1,77 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import base64
|
| 3 |
-
import requests
|
| 4 |
-
from smolagents import Tool
|
| 5 |
-
|
| 6 |
-
class ImageAnalysisTool(Tool):
|
| 7 |
-
name = "image_analysis"
|
| 8 |
-
description = "Analyze the content of an image and answer a specific question about it using Hugging Face Inference API."
|
| 9 |
-
inputs = {
|
| 10 |
-
"image_path": {
|
| 11 |
-
"type": "string",
|
| 12 |
-
"description": "Path to the image file (jpg, png, etc.)"
|
| 13 |
-
},
|
| 14 |
-
"question": {
|
| 15 |
-
"type": "string",
|
| 16 |
-
"description": "A question about the image content"
|
| 17 |
-
}
|
| 18 |
-
}
|
| 19 |
-
output_type = "string"
|
| 20 |
-
|
| 21 |
-
def __init__(self):
|
| 22 |
-
super().__init__()
|
| 23 |
-
api_token = os.getenv("HF_API_TOKEN")
|
| 24 |
-
if not api_token:
|
| 25 |
-
raise EnvironmentError("HF_API_TOKEN not found in environment variables.")
|
| 26 |
-
self.api_url = "https://api-inference.huggingface.co/models/microsoft/git-base-captioning"
|
| 27 |
-
self.headers = {
|
| 28 |
-
"Authorization": f"Bearer {api_token}",
|
| 29 |
-
"Content-Type": "application/json"
|
| 30 |
-
}
|
| 31 |
-
|
| 32 |
-
def forward(self, image_path: str, question: str) -> str:
|
| 33 |
-
try:
|
| 34 |
-
with open(image_path, "rb") as img_file:
|
| 35 |
-
image_bytes = img_file.read()
|
| 36 |
-
|
| 37 |
-
# Encode image to base64 string
|
| 38 |
-
img_b64 = base64.b64encode(image_bytes).decode("utf-8")
|
| 39 |
-
|
| 40 |
-
# Prepare payload for the API
|
| 41 |
-
payload = {
|
| 42 |
-
"inputs": img_b64
|
| 43 |
-
}
|
| 44 |
-
|
| 45 |
-
response = requests.post(
|
| 46 |
-
self.api_url,
|
| 47 |
-
headers=self.headers,
|
| 48 |
-
json=payload,
|
| 49 |
-
timeout=60
|
| 50 |
-
)
|
| 51 |
-
|
| 52 |
-
if response.status_code == 200:
|
| 53 |
-
result = response.json()
|
| 54 |
-
|
| 55 |
-
caption = None
|
| 56 |
-
# Try common keys for caption output
|
| 57 |
-
if isinstance(result, dict):
|
| 58 |
-
caption = result.get("generated_text") or result.get("caption") or result.get("text")
|
| 59 |
-
elif isinstance(result, list) and len(result) > 0 and isinstance(result[0], dict):
|
| 60 |
-
caption = result[0].get("generated_text") or result[0].get("caption") or result[0].get("text")
|
| 61 |
-
|
| 62 |
-
if not caption:
|
| 63 |
-
return "Error: No caption found in model response."
|
| 64 |
-
|
| 65 |
-
# Combine caption with the question to form a simple answer
|
| 66 |
-
answer = f"Caption: {caption}\nAnswer to question '{question}': {caption}"
|
| 67 |
-
return answer.strip()
|
| 68 |
-
|
| 69 |
-
else:
|
| 70 |
-
return f"Error analyzing image: {response.status_code} {response.text}"
|
| 71 |
-
|
| 72 |
-
except Exception as e:
|
| 73 |
-
return f"Error analyzing image: {e}"
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|