Spaces:
Sleeping
Sleeping
File size: 1,852 Bytes
02840f8 5c5f32d 0b0ce33 02840f8 0b0ce33 5c5f32d 0b0ce33 5c5f32d 0b0ce33 5c5f32d 0b0ce33 5c5f32d 02840f8 5c5f32d 02840f8 0b0ce33 02840f8 0b0ce33 02840f8 0b0ce33 5c5f32d 02840f8 0b0ce33 5c5f32d 0b0ce33 |
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
import base64
import requests
import openai
from smolagents import Tool
class ImageAnalysisTool(Tool):
name = "image_analysis"
description = "Analyze the content of an image and answer a specific question about it."
inputs = {
"url": {
"type": "string",
"description": "URL to the image"
},
"question": {
"type": "string",
"description": "Question about the image content"
}
}
output_type = "string"
def forward(self, url: str, question: str) -> str:
try:
# Download image
image_path = "/tmp/image_input.jpg"
r = requests.get(url)
with open(image_path, "wb") as f:
f.write(r.content)
# Encode & analyze
base64_image = self.encode_image(image_path)
response = openai.ChatCompletion.create(
model="gpt-4-turbo",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": question},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}"
}
}
]
}
],
max_tokens=300
)
return response["choices"][0]["message"]["content"].strip()
except Exception as e:
return f"Error analyzing image: {e}"
def encode_image(self, image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")
|