Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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
|
| 99 |
output_image_path = None
|
| 100 |
try:
|
| 101 |
-
#
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
|
|
|
| 121 |
|
| 122 |
except Exception as e:
|
| 123 |
-
print(f"Could not extract output image
|
| 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()
|