Spaces:
Sleeping
Sleeping
Create image_analyzer.py
Browse files- image_analyzer.py +36 -0
image_analyzer.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import openai
|
| 3 |
+
from smolagents import Tool
|
| 4 |
+
|
| 5 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 6 |
+
|
| 7 |
+
class ImageAnalyzer(Tool):
|
| 8 |
+
name = "image_analyzer"
|
| 9 |
+
description = "Analyze the given image and describe or reason about its contents."
|
| 10 |
+
inputs = {
|
| 11 |
+
"image_path": {
|
| 12 |
+
"type": "string",
|
| 13 |
+
"description": "Path to the image file (e.g., a chessboard image)."
|
| 14 |
+
},
|
| 15 |
+
"question": {
|
| 16 |
+
"type": "string",
|
| 17 |
+
"description": "The question to answer about the image (e.g., best chess move)."
|
| 18 |
+
}
|
| 19 |
+
}
|
| 20 |
+
output_type = "string"
|
| 21 |
+
|
| 22 |
+
def forward(self, image_path: str, question: str) -> str:
|
| 23 |
+
with open(image_path, "rb") as image_file:
|
| 24 |
+
response = openai.chat.completions.create(
|
| 25 |
+
model="gpt-4-vision-preview",
|
| 26 |
+
messages=[
|
| 27 |
+
{"role": "user", "content": [
|
| 28 |
+
{"type": "text", "text": question},
|
| 29 |
+
{"type": "image_url", "image_url": {"url": "data:image/jpeg;base64," + image_file.read().encode("base64").decode()}}
|
| 30 |
+
]}
|
| 31 |
+
],
|
| 32 |
+
max_tokens=500
|
| 33 |
+
)
|
| 34 |
+
return response.choices[0].message.content.strip()
|
| 35 |
+
|
| 36 |
+
|