Spaces:
Sleeping
Sleeping
Update image_analyzer.py
Browse files- image_analyzer.py +34 -17
image_analyzer.py
CHANGED
@@ -1,36 +1,53 @@
|
|
1 |
-
import
|
2 |
import openai
|
3 |
from smolagents import Tool
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
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 (
|
14 |
},
|
15 |
"question": {
|
16 |
"type": "string",
|
17 |
-
"description": "
|
18 |
}
|
19 |
}
|
20 |
output_type = "string"
|
21 |
|
|
|
|
|
|
|
22 |
def forward(self, image_path: str, question: str) -> str:
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
26 |
messages=[
|
27 |
-
{
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
],
|
32 |
-
max_tokens=
|
33 |
)
|
34 |
-
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
|
|
|
1 |
+
import base64
|
2 |
import openai
|
3 |
from smolagents import Tool
|
4 |
|
5 |
+
class ImageAnalysisTool(Tool):
|
6 |
+
name = "image_analysis"
|
7 |
+
description = "Analyze the content of an image and answer a specific question about it."
|
|
|
|
|
8 |
inputs = {
|
9 |
"image_path": {
|
10 |
"type": "string",
|
11 |
+
"description": "Path to the image file (jpg, png, etc.)"
|
12 |
},
|
13 |
"question": {
|
14 |
"type": "string",
|
15 |
+
"description": "A question about the image content"
|
16 |
}
|
17 |
}
|
18 |
output_type = "string"
|
19 |
|
20 |
+
def __init__(self):
|
21 |
+
super().__init__()
|
22 |
+
|
23 |
def forward(self, image_path: str, question: str) -> str:
|
24 |
+
base64_image = self.encode_image(image_path)
|
25 |
+
try:
|
26 |
+
response = openai.ChatCompletion.create(
|
27 |
+
model="gpt-4-turbo",
|
28 |
messages=[
|
29 |
+
{
|
30 |
+
"role": "user",
|
31 |
+
"content": [
|
32 |
+
{"type": "text", "text": question},
|
33 |
+
{
|
34 |
+
"type": "image_url",
|
35 |
+
"image_url": {
|
36 |
+
"url": f"data:image/jpeg;base64,{base64_image}"
|
37 |
+
}
|
38 |
+
}
|
39 |
+
]
|
40 |
+
}
|
41 |
],
|
42 |
+
max_tokens=300
|
43 |
)
|
44 |
+
return response["choices"][0]["message"]["content"]
|
45 |
+
except Exception as e:
|
46 |
+
return f"Error analyzing image: {e}"
|
47 |
+
|
48 |
+
def encode_image(self, image_path):
|
49 |
+
with open(image_path, "rb") as image_file:
|
50 |
+
return base64.b64encode(image_file.read()).decode("utf-8")
|
51 |
+
|
52 |
|
53 |
|