Spaces:
Sleeping
Sleeping
make final answer work with gardio ui
Browse files- tools/final_answer.py +5 -26
tools/final_answer.py
CHANGED
@@ -15,9 +15,7 @@
|
|
15 |
|
16 |
from typing import Any, Optional
|
17 |
from smolagents.tools import Tool
|
18 |
-
import
|
19 |
-
from pathlib import Path
|
20 |
-
import mimetypes
|
21 |
|
22 |
class FinalAnswerTool(Tool):
|
23 |
name = "final_answer"
|
@@ -26,29 +24,10 @@ class FinalAnswerTool(Tool):
|
|
26 |
output_type = "any"
|
27 |
|
28 |
def forward(self, answer: Any) -> Any:
|
29 |
-
# Check if the answer is an image path
|
30 |
-
if isinstance(answer, str) and '/tmp/gradio/' in answer:
|
31 |
-
|
32 |
-
|
33 |
-
img_path = Path(answer)
|
34 |
-
if img_path.exists():
|
35 |
-
with open(img_path, 'rb') as img_file:
|
36 |
-
img_data = img_file.read()
|
37 |
-
|
38 |
-
# Determine the MIME type
|
39 |
-
mime_type = mimetypes.guess_type(answer)[0] or 'image/webp'
|
40 |
-
|
41 |
-
# Return both the original path and the image data
|
42 |
-
return {
|
43 |
-
"type": "image",
|
44 |
-
"path": answer,
|
45 |
-
"mime_type": mime_type,
|
46 |
-
"data": img_data
|
47 |
-
}
|
48 |
-
else:
|
49 |
-
return f"Image was generated but could not be accessed at: {answer}"
|
50 |
-
except Exception as e:
|
51 |
-
return f"Error processing image: {str(e)}"
|
52 |
|
53 |
# Return the original answer for non-image responses
|
54 |
return answer
|
|
|
15 |
|
16 |
from typing import Any, Optional
|
17 |
from smolagents.tools import Tool
|
18 |
+
from smolagents.agent_types import AgentImage # Import this class
|
|
|
|
|
19 |
|
20 |
class FinalAnswerTool(Tool):
|
21 |
name = "final_answer"
|
|
|
24 |
output_type = "any"
|
25 |
|
26 |
def forward(self, answer: Any) -> Any:
|
27 |
+
# Check if the answer is an image path
|
28 |
+
if isinstance(answer, str) and ('/tmp/gradio/' in answer or answer.endswith(('.png', '.jpg', '.jpeg', '.webp'))):
|
29 |
+
# Return as AgentImage for proper handling by stream_to_gradio
|
30 |
+
return AgentImage(answer)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
# Return the original answer for non-image responses
|
33 |
return answer
|