Fixed final_answer function
Browse files- Gradio_UI.py +33 -32
Gradio_UI.py
CHANGED
@@ -160,38 +160,39 @@ def stream_to_gradio(
|
|
160 |
|
161 |
# FIX: Process the final answer correctly after the loop
|
162 |
if final_answer_step:
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
)
|
192 |
-
|
193 |
-
|
194 |
-
|
|
|
195 |
|
196 |
|
197 |
class GradioUI:
|
|
|
160 |
|
161 |
# FIX: Process the final answer correctly after the loop
|
162 |
if final_answer_step:
|
163 |
+
# Extract the actual value from the FinalAnswerStep object
|
164 |
+
final_answer_value = getattr(final_answer_step, 'final_answer', final_answer_step)
|
165 |
+
|
166 |
+
# Check if the final answer is a string that looks like an image file path
|
167 |
+
if isinstance(final_answer_value, str) and final_answer_value.endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp', '.webp')):
|
168 |
+
# It's an image file path, create an AgentImage object
|
169 |
+
from smolagents.agent_types import AgentImage
|
170 |
+
processed_answer = AgentImage(value=final_answer_value)
|
171 |
+
else:
|
172 |
+
# Try the default conversion
|
173 |
+
processed_answer = handle_agent_output_types(final_answer_value)
|
174 |
+
|
175 |
+
if isinstance(processed_answer, AgentText):
|
176 |
+
yield gr.ChatMessage(
|
177 |
+
role="assistant",
|
178 |
+
content=f"**Final answer:**\n{processed_answer.to_string()}\n",
|
179 |
+
)
|
180 |
+
elif isinstance(processed_answer, AgentImage):
|
181 |
+
# For images, pass the file path directly - Gradio will handle it
|
182 |
+
image_path = processed_answer.to_string()
|
183 |
+
yield gr.ChatMessage(
|
184 |
+
role="assistant",
|
185 |
+
content=f"**Final answer:**\n{image_path}", # Just pass the path as a string
|
186 |
+
)
|
187 |
+
elif isinstance(processed_answer, AgentAudio):
|
188 |
+
# Same for audio files
|
189 |
+
yield gr.ChatMessage(
|
190 |
+
role="assistant",
|
191 |
+
content=f"**Final answer:**\n{processed_answer.to_string()}",
|
192 |
+
)
|
193 |
+
else:
|
194 |
+
# Fallback for any other type
|
195 |
+
yield gr.ChatMessage(role="assistant", content=f"**Final answer:** {str(processed_answer)}")
|
196 |
|
197 |
|
198 |
class GradioUI:
|