Spaces:
Sleeping
Sleeping
File size: 1,246 Bytes
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 |
import os
import openai
from smolagents import Tool
openai.api_key = os.getenv("OPENAI_API_KEY")
class ImageAnalyzer(Tool):
name = "image_analyzer"
description = "Analyze the given image and describe or reason about its contents."
inputs = {
"image_path": {
"type": "string",
"description": "Path to the image file (e.g., a chessboard image)."
},
"question": {
"type": "string",
"description": "The question to answer about the image (e.g., best chess move)."
}
}
output_type = "string"
def forward(self, image_path: str, question: str) -> str:
with open(image_path, "rb") as image_file:
response = openai.chat.completions.create(
model="gpt-4-vision-preview",
messages=[
{"role": "user", "content": [
{"type": "text", "text": question},
{"type": "image_url", "image_url": {"url": "data:image/jpeg;base64," + image_file.read().encode("base64").decode()}}
]}
],
max_tokens=500
)
return response.choices[0].message.content.strip()
|