hmgill commited on
Commit
6954f98
Β·
verified Β·
1 Parent(s): e15c90f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -23
app.py CHANGED
@@ -2,10 +2,12 @@
2
  Gradio web interface for CellposeAgent
3
  """
4
  import os
 
5
  import gradio as gr
6
  from pathlib import Path
7
  from langfuse import get_client
8
  from openinference.instrumentation.smolagents import SmolagentsInstrumentor
 
9
 
10
  from config import settings
11
  from agents.agent import CellposeAgent
@@ -95,32 +97,33 @@ def process_image_task(image_path: str, task_text: str, agent: CellposeAgent) ->
95
  result = agent.run(task)
96
  get_client().flush()
97
 
98
- # Extract output image path from the agent's response if segmentation was run
99
  output_image_path = None
100
  try:
101
- # Check if the response contains a segmentation output path
102
- if "output_path" in result or "_cellpose_sam_overlay.png" in result:
103
- # Parse the response to find the output path
104
- import json
105
- import re
106
-
107
- # Try to find JSON in the response
108
- json_match = re.search(r'\{[^{}]*"output_path"[^{}]*\}', result)
109
- if json_match:
110
- result_data = json.loads(json_match.group())
111
- output_image_path = result_data.get("output_path")
112
- else:
113
- # Try to find the path in the text
114
- path_match = re.search(r'([^\s]+_cellpose_sam_overlay\.png)', result)
115
- if path_match:
116
- output_image_path = path_match.group(1)
117
-
118
- # Verify the file exists
119
- if output_image_path and not Path(output_image_path).exists():
120
- output_image_path = None
 
121
 
122
  except Exception as e:
123
- print(f"Could not extract output image path: {e}")
124
 
125
  return result, output_image_path
126
 
@@ -235,4 +238,4 @@ def main():
235
 
236
 
237
  if __name__ == "__main__":
238
- main()
 
2
  Gradio web interface for CellposeAgent
3
  """
4
  import os
5
+ import json
6
  import gradio as gr
7
  from pathlib import Path
8
  from langfuse import get_client
9
  from openinference.instrumentation.smolagents import SmolagentsInstrumentor
10
+ from smolagents.agents import ActionStep
11
 
12
  from config import settings
13
  from agents.agent import CellposeAgent
 
97
  result = agent.run(task)
98
  get_client().flush()
99
 
100
+ # Extract output image path from agent's memory (more reliable than parsing text)
101
  output_image_path = None
102
  try:
103
+ # Search through agent's memory steps in reverse order (most recent first)
104
+ for step in reversed(agent.agent.memory.steps):
105
+ if isinstance(step, ActionStep) and step.observations:
106
+ try:
107
+ obs_data = json.loads(step.observations)
108
+
109
+ # Check if this step contains segmentation output
110
+ if obs_data.get("status") == "success" and "output_path" in obs_data:
111
+ candidate_path = obs_data["output_path"]
112
+
113
+ # Verify the file exists
114
+ if Path(candidate_path).exists():
115
+ output_image_path = candidate_path
116
+ print(f"βœ“ Found segmentation output: {output_image_path}")
117
+ break
118
+
119
+ except (json.JSONDecodeError, Exception) as parse_error:
120
+ continue
121
+
122
+ if output_image_path is None:
123
+ print("ℹ️ No segmentation output found in agent memory")
124
 
125
  except Exception as e:
126
+ print(f"Warning: Could not extract output image from agent memory: {e}")
127
 
128
  return result, output_image_path
129
 
 
238
 
239
 
240
  if __name__ == "__main__":
241
+ main()