piyushdev commited on
Commit
dde47e9
·
verified ·
1 Parent(s): 6f24f52

Added one more fix for image issue

Browse files
Files changed (1) hide show
  1. Gradio_UI.py +8 -8
Gradio_UI.py CHANGED
@@ -61,6 +61,10 @@ def pull_messages_from_step(
61
  else:
62
  content = str(args).strip()
63
 
 
 
 
 
64
  if used_code:
65
  # Clean up the content by removing any end code tags
66
  content = re.sub(r"```.*?\n", "", content) # Remove existing code blocks
@@ -143,27 +147,23 @@ def stream_to_gradio(
143
  final_answer_step = step_log
144
  continue # Don't display the final answer step itself, process it at the end
145
 
146
- # FIX: Check if token counts exist before trying to use them
147
- if hasattr(agent.model, "last_input_token_count") and agent.model.last_input_token_count is not None:
148
- if isinstance(step_log, ActionStep):
149
- step_log.input_token_count = agent.model.last_input_token_count
150
- step_log.output_token_count = agent.model.last_output_token_count
151
-
152
  for message in pull_messages_from_step(
153
  step_log,
154
  ):
155
  yield message
156
 
157
- # Process the final answer correctly after the loop
158
  if final_answer_step:
159
  # Extract the actual value from the FinalAnswerStep object
160
  final_answer_value = getattr(final_answer_step, 'final_answer', final_answer_step)
161
 
162
  # Directly check if the final answer is a path to an image file
163
  if isinstance(final_answer_value, str) and final_answer_value.lower().endswith(('.png', '.jpg', '.jpeg', '.gif')):
 
 
164
  yield gr.ChatMessage(
165
  role="assistant",
166
- content=final_answer_value, # Pass the file path directly to Gradio
167
  )
168
  else:
169
  # Fallback for text or any other type of answer
 
61
  else:
62
  content = str(args).strip()
63
 
64
+ # If the content looks like an image file path, convert it to Markdown so Gradio shows the image
65
+ if isinstance(content, str) and content.lower().endswith(('.png', '.jpg', '.jpeg', '.gif')):
66
+ content = f"![generated image]({content})"
67
+
68
  if used_code:
69
  # Clean up the content by removing any end code tags
70
  content = re.sub(r"```.*?\n", "", content) # Remove existing code blocks
 
147
  final_answer_step = step_log
148
  continue # Don't display the final answer step itself, process it at the end
149
 
 
 
 
 
 
 
150
  for message in pull_messages_from_step(
151
  step_log,
152
  ):
153
  yield message
154
 
155
+ # FIX: Process the final answer correctly after the loop
156
  if final_answer_step:
157
  # Extract the actual value from the FinalAnswerStep object
158
  final_answer_value = getattr(final_answer_step, 'final_answer', final_answer_step)
159
 
160
  # Directly check if the final answer is a path to an image file
161
  if isinstance(final_answer_value, str) and final_answer_value.lower().endswith(('.png', '.jpg', '.jpeg', '.gif')):
162
+ # Display the image inline using Markdown so Gradio will render it instead of plain text
163
+ markdown_img = f"![generated image]({final_answer_value})"
164
  yield gr.ChatMessage(
165
  role="assistant",
166
+ content=markdown_img,
167
  )
168
  else:
169
  # Fallback for text or any other type of answer