Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,9 +3,16 @@ import asyncio
|
|
| 3 |
import os
|
| 4 |
import glob
|
| 5 |
import torch
|
|
|
|
| 6 |
from pathlib import Path
|
| 7 |
from PIL import Image
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
# GenAI & ADK Imports
|
| 10 |
from google.adk.runners import InMemoryRunner
|
| 11 |
from google.genai import types
|
|
@@ -34,12 +41,14 @@ def load_models():
|
|
| 34 |
|
| 35 |
try:
|
| 36 |
# Note: Ensure you have access to facebook/sam3 or use a public alternative
|
|
|
|
| 37 |
MODEL_CACHE["model"] = Sam3Model.from_pretrained("facebook/sam3").to(device)
|
| 38 |
MODEL_CACHE["processor"] = Sam3Processor.from_pretrained("facebook/sam3")
|
| 39 |
print(f"β
SAM3 loaded on {device}")
|
| 40 |
except Exception as e:
|
| 41 |
-
print(f"β οΈ SAM3 load failed (using mock): {e}")
|
| 42 |
-
#
|
|
|
|
| 43 |
|
| 44 |
# Load immediately on startup
|
| 45 |
load_models()
|
|
@@ -73,6 +82,8 @@ async def run_analysis(image_path_str, user_prompt, progress=gr.Progress()):
|
|
| 73 |
|
| 74 |
# 2. Initialize Runner
|
| 75 |
runner = InMemoryRunner(agent=root_agent, app_name="cellemetry_demo")
|
|
|
|
|
|
|
| 76 |
session = await runner.session_service.create_session(
|
| 77 |
app_name="cellemetry_demo",
|
| 78 |
user_id="demo_user",
|
|
@@ -94,39 +105,50 @@ async def run_analysis(image_path_str, user_prompt, progress=gr.Progress()):
|
|
| 94 |
log_text = "\n".join(logs)
|
| 95 |
yield log_text, None, None
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
if
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
else:
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
yield "\n\n".join(logs), None, None
|
|
|
|
| 124 |
|
| 125 |
# 5. Retrieve Final Results
|
| 126 |
logs.append("\nπ **Analysis Complete.** gathering files...")
|
| 127 |
yield "\n\n".join(logs), None, None
|
| 128 |
|
| 129 |
-
# Collect output images
|
| 130 |
output_images = glob.glob("/tmp/out_*.png")
|
| 131 |
|
| 132 |
# Collect excel report
|
|
|
|
| 3 |
import os
|
| 4 |
import glob
|
| 5 |
import torch
|
| 6 |
+
import sys
|
| 7 |
from pathlib import Path
|
| 8 |
from PIL import Image
|
| 9 |
|
| 10 |
+
# --- CRITICAL FIX: Redirect stdin to avoid "Invalid file descriptor" crash ---
|
| 11 |
+
# The agent runner checks stdin for potential human-in-the-loop confirmation.
|
| 12 |
+
# In a web app (Gradio/Cloud Run), stdin is closed/invalid. Redirecting to
|
| 13 |
+
# devnull tricks the runner into proceeding without crashing.
|
| 14 |
+
sys.stdin = open(os.devnull, 'r')
|
| 15 |
+
|
| 16 |
# GenAI & ADK Imports
|
| 17 |
from google.adk.runners import InMemoryRunner
|
| 18 |
from google.genai import types
|
|
|
|
| 41 |
|
| 42 |
try:
|
| 43 |
# Note: Ensure you have access to facebook/sam3 or use a public alternative
|
| 44 |
+
# If running on CPU only, this might be slow!
|
| 45 |
MODEL_CACHE["model"] = Sam3Model.from_pretrained("facebook/sam3").to(device)
|
| 46 |
MODEL_CACHE["processor"] = Sam3Processor.from_pretrained("facebook/sam3")
|
| 47 |
print(f"β
SAM3 loaded on {device}")
|
| 48 |
except Exception as e:
|
| 49 |
+
print(f"β οΈ SAM3 load failed (using mock or fallback): {e}")
|
| 50 |
+
# We allow the app to continue; the agent tools will handle the missing model
|
| 51 |
+
# (check sam.py for the mock/fallback logic)
|
| 52 |
|
| 53 |
# Load immediately on startup
|
| 54 |
load_models()
|
|
|
|
| 82 |
|
| 83 |
# 2. Initialize Runner
|
| 84 |
runner = InMemoryRunner(agent=root_agent, app_name="cellemetry_demo")
|
| 85 |
+
|
| 86 |
+
# Create session
|
| 87 |
session = await runner.session_service.create_session(
|
| 88 |
app_name="cellemetry_demo",
|
| 89 |
user_id="demo_user",
|
|
|
|
| 105 |
log_text = "\n".join(logs)
|
| 106 |
yield log_text, None, None
|
| 107 |
|
| 108 |
+
try:
|
| 109 |
+
async for event in runner.run_async(
|
| 110 |
+
user_id="demo_user",
|
| 111 |
+
session_id=session.id,
|
| 112 |
+
new_message=content,
|
| 113 |
+
):
|
| 114 |
+
author = event.author
|
| 115 |
+
|
| 116 |
+
# Capture Tool Calls
|
| 117 |
+
if event.get_function_calls():
|
| 118 |
+
for fc in event.get_function_calls():
|
| 119 |
+
logs.append(f"π§ **{author}** calling tool: `{fc.name}`")
|
| 120 |
+
yield "\n\n".join(logs), None, None
|
| 121 |
+
|
| 122 |
+
# Capture Text (Streaming)
|
| 123 |
+
if event.content and event.content.parts:
|
| 124 |
+
for part in event.content.parts:
|
| 125 |
+
if hasattr(part, 'text') and part.text:
|
| 126 |
+
if event.partial:
|
| 127 |
+
# Update the last log line if it's the same thought being streamed
|
| 128 |
+
if logs and logs[-1].startswith(f"π¬ **{author}**"):
|
| 129 |
+
logs[-1] = f"π¬ **{author}**: {part.text}..."
|
| 130 |
+
else:
|
| 131 |
+
logs.append(f"π¬ **{author}**: {part.text}...")
|
| 132 |
else:
|
| 133 |
+
# Finalize the log line
|
| 134 |
+
if logs and logs[-1].startswith(f"π¬ **{author}**"):
|
| 135 |
+
logs[-1] = f"β
**{author}**: {part.text}"
|
| 136 |
+
else:
|
| 137 |
+
logs.append(f"β
**{author}**: {part.text}")
|
| 138 |
+
|
| 139 |
+
# Yield updated logs immediately
|
| 140 |
+
yield "\n\n".join(logs), None, None
|
| 141 |
+
|
| 142 |
+
except Exception as e:
|
| 143 |
+
logs.append(f"β **Error during execution**: {str(e)}")
|
| 144 |
yield "\n\n".join(logs), None, None
|
| 145 |
+
return
|
| 146 |
|
| 147 |
# 5. Retrieve Final Results
|
| 148 |
logs.append("\nπ **Analysis Complete.** gathering files...")
|
| 149 |
yield "\n\n".join(logs), None, None
|
| 150 |
|
| 151 |
+
# Collect output images (segmentation maps)
|
| 152 |
output_images = glob.glob("/tmp/out_*.png")
|
| 153 |
|
| 154 |
# Collect excel report
|