SemSorter commited on
Commit
693a18a
·
1 Parent(s): e085e6c

Strip SDK and use google.generativeai direct to spare 70MB of RAM for Render free tier deployment

Browse files
Files changed (1) hide show
  1. SemSorter/server/agent_bridge.py +44 -29
SemSorter/server/agent_bridge.py CHANGED
@@ -184,39 +184,54 @@ def get_bridge():
184
 
185
 
186
  def get_llm():
187
- """Return a configured gemini.LLM instance from the Vision-Agents SDK."""
188
  global _llm
189
  if _llm is None:
190
- from vision_agents.plugins.gemini.gemini_llm import GeminiLLM as GeminiLLMCls
191
- _llm = GeminiLLMCls("gemini-2.0-flash")
192
- _register_tools(_llm)
193
- logger.info("Gemini LLM ready")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
194
  return _llm
195
 
196
 
197
- def _register_tools(llm) -> None:
198
- """Register simulation control tools on the LLM."""
199
-
200
- @llm.register_function(description="Scan the conveyor belt for hazardous items.")
201
- async def scan_for_hazards() -> Dict[str, Any]:
202
- return await _trigger_scan_with_ui()
203
-
204
- @llm.register_function(
205
- description="Pick a specific item by sim name and place it in its bin. "
206
- "bin_type must be 'flammable' or 'chemical'.")
207
- async def pick_and_place_item(item_name: str, bin_type: str) -> Dict[str, Any]:
208
- return await _pick_place_impl(item_name, bin_type)
209
-
210
- @llm.register_function(description="Get current simulation state snapshot.")
211
- async def get_simulation_state() -> Dict[str, Any]:
212
- return _state_impl()
213
-
214
- @llm.register_function(
215
- description="Detect ALL hazardous items and sort them automatically.")
216
- async def sort_all_hazards() -> Dict[str, Any]:
217
- return await _trigger_sort_with_ui()
218
-
219
-
220
  # ── Tool implementations ──────────────────────────────────────────────────────
221
 
222
  def _build_demo_detections_from_sim() -> List[Dict]:
@@ -461,7 +476,7 @@ async def process_text_command(text: str) -> str:
461
  try:
462
  llm = get_llm()
463
  # Use the LLM's send_message method to get a response with tool-calling
464
- response_event = await llm.send_message(message=text)
465
  return response_event.text
466
  except Exception as exc:
467
  svc = _check_quota_error(exc)
 
184
 
185
 
186
  def get_llm():
187
+ """Return a configured google.generativeai chat session instead of Vision-Agents."""
188
  global _llm
189
  if _llm is None:
190
+ import google.generativeai as genai
191
+ import asyncio
192
+
193
+ # Define native Gemini tools
194
+ def scan_for_hazards() -> dict:
195
+ """Scan the conveyor belt for hazardous items."""
196
+ import app
197
+ loop = getattr(app, "_main_loop", None) or asyncio.get_event_loop()
198
+ return asyncio.run_coroutine_threadsafe(_trigger_scan_with_ui(), loop).result()
199
+
200
+ def pick_and_place_item(item_name: str, bin_type: str) -> dict:
201
+ """Pick a specific item by sim name and place it in its bin. bin_type must be 'flammable' or 'chemical'."""
202
+ import app
203
+ loop = getattr(app, "_main_loop", None) or asyncio.get_event_loop()
204
+ return asyncio.run_coroutine_threadsafe(_pick_place_impl(item_name, bin_type), loop).result()
205
+
206
+ def get_simulation_state() -> dict:
207
+ """Get current simulation state snapshot."""
208
+ return _state_impl()
209
+
210
+ def sort_all_hazards() -> dict:
211
+ """Detect ALL hazardous items and sort them automatically."""
212
+ import app
213
+ loop = getattr(app, "_main_loop", None) or asyncio.get_event_loop()
214
+ return asyncio.run_coroutine_threadsafe(_trigger_sort_with_ui(), loop).result()
215
+
216
+ api_key = os.environ.get("GOOGLE_API_KEY", "")
217
+ if api_key:
218
+ genai.configure(api_key=api_key)
219
+
220
+ model = genai.GenerativeModel(
221
+ model_name="gemini-2.0-flash",
222
+ tools=[scan_for_hazards, pick_and_place_item, get_simulation_state, sort_all_hazards],
223
+ system_instruction=(
224
+ "You are an AI assistant controlling SemSorter, a robotic waste sorting system. "
225
+ "You can scan the conveyor belt for hazardous items and pick/place them into bins. "
226
+ "Call the appropriate tools enthusiastically based on user requests."
227
+ )
228
+ )
229
+ _llm = model.start_chat(enable_automatic_function_calling=True)
230
+ import logging
231
+ logging.getLogger(__name__).info("Direct Gemini LLM ready (saved ~100MB RAM)")
232
  return _llm
233
 
234
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
235
  # ── Tool implementations ──────────────────────────────────────────────────────
236
 
237
  def _build_demo_detections_from_sim() -> List[Dict]:
 
476
  try:
477
  llm = get_llm()
478
  # Use the LLM's send_message method to get a response with tool-calling
479
+ response_event = await asyncio.to_thread(llm.send_message, text)
480
  return response_event.text
481
  except Exception as exc:
482
  svc = _check_quota_error(exc)