Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -66,7 +66,7 @@ def initialize_app():
|
|
| 66 |
return True
|
| 67 |
|
| 68 |
|
| 69 |
-
def process_image_task(image_path: str, task_text: str, agent: CellposeAgent) -> str:
|
| 70 |
"""
|
| 71 |
Process a user task with the CellposeAgent.
|
| 72 |
|
|
@@ -76,20 +76,55 @@ def process_image_task(image_path: str, task_text: str, agent: CellposeAgent) ->
|
|
| 76 |
agent: Initialized CellposeAgent instance
|
| 77 |
|
| 78 |
Returns:
|
| 79 |
-
|
| 80 |
"""
|
| 81 |
if not image_path:
|
| 82 |
-
return "β οΈ Please upload an image first."
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
try:
|
| 88 |
-
result = agent.run(
|
| 89 |
get_client().flush()
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
except Exception as e:
|
| 92 |
-
return f"β Error processing task: {str(e)}"
|
| 93 |
|
| 94 |
|
| 95 |
def create_gradio_interface():
|
|
@@ -124,7 +159,7 @@ def create_gradio_interface():
|
|
| 124 |
# Task input
|
| 125 |
task_input = gr.Textbox(
|
| 126 |
label="Task / Question",
|
| 127 |
-
placeholder="e.g., 'What parameters would work best
|
| 128 |
lines=3
|
| 129 |
)
|
| 130 |
|
|
@@ -145,19 +180,26 @@ def create_gradio_interface():
|
|
| 145 |
)
|
| 146 |
|
| 147 |
with gr.Column(scale=1):
|
| 148 |
-
#
|
| 149 |
output = gr.Textbox(
|
| 150 |
label="Agent Response",
|
| 151 |
-
lines=
|
| 152 |
-
max_lines=
|
| 153 |
show_copy_button=True
|
| 154 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
|
| 156 |
# Event handler
|
| 157 |
submit_btn.click(
|
| 158 |
fn=lambda img, task: process_image_task(img, task, agent),
|
| 159 |
inputs=[image_input, task_input],
|
| 160 |
-
outputs=output
|
| 161 |
)
|
| 162 |
|
| 163 |
gr.Markdown(
|
|
@@ -178,6 +220,7 @@ def create_gradio_interface():
|
|
| 178 |
3. Visually analyzes your image to validate recommendations
|
| 179 |
4. Runs segmentation and checks quality
|
| 180 |
5. Refines parameters if needed (up to 2 iterations)
|
|
|
|
| 181 |
"""
|
| 182 |
)
|
| 183 |
|
|
|
|
| 66 |
return True
|
| 67 |
|
| 68 |
|
| 69 |
+
def process_image_task(image_path: str, task_text: str, agent: CellposeAgent) -> tuple[str, str | None]:
|
| 70 |
"""
|
| 71 |
Process a user task with the CellposeAgent.
|
| 72 |
|
|
|
|
| 76 |
agent: Initialized CellposeAgent instance
|
| 77 |
|
| 78 |
Returns:
|
| 79 |
+
tuple: (agent's text response, path to segmented image or None)
|
| 80 |
"""
|
| 81 |
if not image_path:
|
| 82 |
+
return "β οΈ Please upload an image first.", None
|
| 83 |
|
| 84 |
+
# Always include the full image path in the task
|
| 85 |
+
if not task_text or task_text.strip() == "":
|
| 86 |
+
task = f"What parameters would work best for my image {image_path}?"
|
| 87 |
+
else:
|
| 88 |
+
if image_path not in task_text:
|
| 89 |
+
task = f"For image {image_path}: {task_text}"
|
| 90 |
+
else:
|
| 91 |
+
task = task_text
|
| 92 |
|
| 93 |
try:
|
| 94 |
+
result = agent.run(task)
|
| 95 |
get_client().flush()
|
| 96 |
+
|
| 97 |
+
# Extract output image path from the agent's response if segmentation was run
|
| 98 |
+
output_image_path = None
|
| 99 |
+
try:
|
| 100 |
+
# Check if the response contains a segmentation output path
|
| 101 |
+
if "output_path" in result or "_cellpose_sam_overlay.png" in result:
|
| 102 |
+
# Parse the response to find the output path
|
| 103 |
+
import json
|
| 104 |
+
import re
|
| 105 |
+
|
| 106 |
+
# Try to find JSON in the response
|
| 107 |
+
json_match = re.search(r'\{[^{}]*"output_path"[^{}]*\}', result)
|
| 108 |
+
if json_match:
|
| 109 |
+
result_data = json.loads(json_match.group())
|
| 110 |
+
output_image_path = result_data.get("output_path")
|
| 111 |
+
else:
|
| 112 |
+
# Try to find the path in the text
|
| 113 |
+
path_match = re.search(r'([^\s]+_cellpose_sam_overlay\.png)', result)
|
| 114 |
+
if path_match:
|
| 115 |
+
output_image_path = path_match.group(1)
|
| 116 |
+
|
| 117 |
+
# Verify the file exists
|
| 118 |
+
if output_image_path and not Path(output_image_path).exists():
|
| 119 |
+
output_image_path = None
|
| 120 |
+
|
| 121 |
+
except Exception as e:
|
| 122 |
+
print(f"Could not extract output image path: {e}")
|
| 123 |
+
|
| 124 |
+
return result, output_image_path
|
| 125 |
+
|
| 126 |
except Exception as e:
|
| 127 |
+
return f"β Error processing task: {str(e)}", None
|
| 128 |
|
| 129 |
|
| 130 |
def create_gradio_interface():
|
|
|
|
| 159 |
# Task input
|
| 160 |
task_input = gr.Textbox(
|
| 161 |
label="Task / Question",
|
| 162 |
+
placeholder="e.g., 'What parameters would work best?' or 'Run segmentation' (image path added automatically)",
|
| 163 |
lines=3
|
| 164 |
)
|
| 165 |
|
|
|
|
| 180 |
)
|
| 181 |
|
| 182 |
with gr.Column(scale=1):
|
| 183 |
+
# Text output
|
| 184 |
output = gr.Textbox(
|
| 185 |
label="Agent Response",
|
| 186 |
+
lines=12,
|
| 187 |
+
max_lines=20,
|
| 188 |
show_copy_button=True
|
| 189 |
)
|
| 190 |
+
|
| 191 |
+
# Image output for segmentation results
|
| 192 |
+
output_image = gr.Image(
|
| 193 |
+
label="Segmentation Result",
|
| 194 |
+
type="filepath",
|
| 195 |
+
height=400
|
| 196 |
+
)
|
| 197 |
|
| 198 |
# Event handler
|
| 199 |
submit_btn.click(
|
| 200 |
fn=lambda img, task: process_image_task(img, task, agent),
|
| 201 |
inputs=[image_input, task_input],
|
| 202 |
+
outputs=[output, output_image]
|
| 203 |
)
|
| 204 |
|
| 205 |
gr.Markdown(
|
|
|
|
| 220 |
3. Visually analyzes your image to validate recommendations
|
| 221 |
4. Runs segmentation and checks quality
|
| 222 |
5. Refines parameters if needed (up to 2 iterations)
|
| 223 |
+
6. **Displays the segmented overlay image with colored cell masks**
|
| 224 |
"""
|
| 225 |
)
|
| 226 |
|