hmgill commited on
Commit
63f35ec
Β·
verified Β·
1 Parent(s): 92e2d37

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -28
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
- # Allow app to start even if model fails (will fall back to mock logic if implemented)
 
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
- async for event in runner.run_async(
98
- user_id="demo_user",
99
- session_id=session.id,
100
- new_message=content,
101
- ):
102
- author = event.author
103
-
104
- # Capture Tool Calls
105
- if event.get_function_calls():
106
- for fc in event.get_function_calls():
107
- logs.append(f"πŸ”§ **{author}** calling tool: `{fc.name}`")
108
-
109
- # Capture Text (Streaming)
110
- if event.content and event.content.parts:
111
- for part in event.content.parts:
112
- if hasattr(part, 'text') and part.text:
113
- if event.partial:
114
- # Update the last log line if it's the same thought
115
- if logs[-1].startswith(f"πŸ’¬ **{author}**"):
116
- logs[-1] = f"πŸ’¬ **{author}**: {part.text}..."
 
 
 
 
117
  else:
118
- logs.append(f"πŸ’¬ **{author}**: {part.text}...")
119
- else:
120
- logs.append(f"βœ… **{author}**: {part.text}")
121
-
122
- # Yield updated logs immediately
 
 
 
 
 
 
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